package blog4go

import (
"sync"
"time"
)

const (
// PrefixTimeFormat  时间格式前缀
PrefixTimeFormat = "[2006/01/02:15:04:05]"

// DateFormat 时间格式
DateFormat = "2006-01-02"
)

// timeFormatCacheType是一个时间格式的缓存
type timeFormatCacheType struct {
//当前时间
now time.Time
//当前时间格式
date string
//当前格式化的时间
format []byte
//昨天
dateYesterday string
//读写锁
lock *sync.RWMutex
}

//全局时间的缓存  对于写日志
var timeCache = timeFormatCacheType{}

func init() {
timeCache.lock = new(sync.RWMutex)  //初始化锁
timeCache.now = time.Now() //当前时间
timeCache.date = timeCache.now.Format(DateFormat)  //当前时间的格式化
timeCache.format = []byte(timeCache.now.Format(PrefixTimeFormat)) //当前时间格式化转为字节数组
timeCache.dateYesterday = timeCache.now.Add(-24 * time.Hour).Format(DateFormat) // 获取当前时间的昨天

//每秒跟新一下timeCache 中的时间  当独开启一个协成
go func() {
// tick 创建一个跟新时间周期
t := time.Tick(1 * time.Second)

//UpdateTimeCache  Loop:
for {
select {
case <-t:
timeCache.fresh()
}
}
}()
}

//获取当前时间
func (timeCache *timeFormatCacheType) Now() time.Time {
timeCache.lock.RLock()
defer timeCache.lock.RUnlock()
return timeCache.now
}

//获取当前时间
func (timeCache *timeFormatCacheType) Date() string {
timeCache.lock.RLock()
defer timeCache.lock.RUnlock()
return timeCache.date
}

//获取昨天时间
func (timeCache *timeFormatCacheType) DateYesterday() string {
timeCache.lock.RLock()
defer timeCache.lock.RUnlock()
return timeCache.dateYesterday
}

//当前时间格式化函数
func (timeCache *timeFormatCacheType) Format() []byte {
timeCache.lock.RLock()
defer timeCache.lock.RUnlock()
return timeCache.format
}

// 刷新 timeCache的时间
func (timeCache *timeFormatCacheType) fresh() {
timeCache.lock.Lock()
defer timeCache.lock.Unlock()

// get current time and update timeCache
       //获取当期时间  并且更新timeCache  缓存时间
now := time.Now()
timeCache.now = now
timeCache.format = []byte(now.Format(PrefixTimeFormat))
date := now.Format(DateFormat)
if date != timeCache.date {
timeCache.dateYesterday = timeCache.date
timeCache.date = now.Format(DateFormat)
}
}

timeCache.go的更多相关文章

  1. Zabbix监控nginx-rtmp status(json版)

    与前面的文章 zabbix监控nginx-rtmp status(html版)区别只在于取值的页面不一样 http://127.0.0.1:81/control/get/all_streams sta ...

  2. Zabbix监控nginx-rtmp status(html版)

    nginx-rtmp开启stats # nginx(--add-module=nginx-rtmp-module-master) nginx.conf: server { listen ; locat ...

  3. Cache Helper类

    using System; using System.Collections.Generic; using System.Web; using System.Collections; using Sy ...

  4. ASP.NET MVC 过滤器开发与使用

    ASP.NET MVC 中给我们提供了内置的过滤器,通过过滤器,我们可以在控制器内的方法前后,添加必须的业务逻辑,如权限验证,身份验证,错误处理等. 今天,我们主要介绍3个过滤器:OutputCach ...

  5. libevent简单分析

    一看名字就知道是围绕eventloop转的. 那首先肯定是eventloop是个什么?一般都是IO事件,timer事件的管理器. 那首先看如何new出来一个eventloop: 1.因为libeven ...

  6. baseFileWriter.go

    package blog4go import ( "fmt" "os" "sync" "time" ) const ( ...

  7. blog4go.go

    package blog4go import ( "bufio" "errors" "fmt" "io" "o ...

  8. socketWriter.go

    package blog4go import ( "bytes" "fmt" "net" "sync" ) // Soc ...

  9. 各种Helper代码

    1.读取XML文件 /// <summary> /// 读取XML配置文件类 /// </summary> public class XmlHelper { private s ...

随机推荐

  1. ruby直接字符串压缩与解压缩

    ruby2.1.3的核心类中包含了Zlib库,其中的Zlib模块包含了对字符串压缩和解压的方法: irb(main):180:0> Zlib.class => Module irb(mai ...

  2. onload="fixImage(this, 200, 200)"

    function fixImage(img, w, h) { var newImg = new Image(); //获得图片的原始尺寸 newImg.src = img.src; var lh; / ...

  3. 面试题:JQuery有几种选择器?

    很多种,大概归纳为9种. (1)基本 #id element .class * selector1,selector2,selectorN (2)层次选择器: ancestor descendant ...

  4. 阿里REDIS优化

  5. JavaScript中将对象数组中的某个属性值,批量替换成另一个数值

    原文链接 https://segmentfault.com/q/1010000010352622 希望将下列数组中的sh替换成沪,sz替换成深 var stooges = [ {label:1,val ...

  6. quicksort(java版)

    相信大家都知道几种排序算法,比如说冒泡排序,选择排序,插入排序等等,这些个算法都不是很难,自己多多理解理解就能掌握了,而今天我们要谈的就是重头戏就是快速排序. 引用大牛的思想来对排序算法解释一下.(文 ...

  7. 【读书笔记】C++Primer---第三章

    1.由于为了与C语言兼容,字符串字面值与标准库string类型不是同一种类型: 2.以下代码中,cin有几点需要注意:a.读取并忽略开头所有的空白字符(如空格.换行符.制表符):b.读取字符直至再次遇 ...

  8. Web开发问题记录

    1.先说一个CSS的:CSS中带有中文(比如字体定义)的属性定义最好放在该选择器定义诸项的最后一条,为什么----编码格式问题. 2.其实自己也可以用自己写的DispatcherServlet+jsp ...

  9. Assets.xcassets误删后的恢复

    一般要是remove Reference,undo就ok了.如果undo没法搞得话,就得手动搞. 1.首先去废纸篓把被删除的目录恢复到原处,也就是项目目录. 2.右键项目主目录名称,选择"A ...

  10. 2013-9 OWASP论坛

    Broken We Application Project   ------这个PPT需要下载 OWASP BWA----- A Virtual machine---收集 Broken Web App ...