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. Oracle经常用到的一些函数

    1.数字函数 NVL( string1, replace_with) :如果string1为NULL,则NVL函数返回replace_with的值,否则返回string1的值,如果两个参数都为NULL ...

  2. The table name must be enclosed in double quotation marks or sqare bracket while accessing EXCEL by

      1  Preface DB Query Analyzer is presented by Master Gen feng, Ma from Chinese Mainland. It has Eng ...

  3. C# 压缩PDF图片

    文档中包含图片的话,会使得整个文档比较大,占用存储空间且不利于快速.高效的传输文件.针对一些包含大量高质图片的PDF文档,若是对图片进行压缩,可以有效减少文档的占用空间.并且,在文档传输过程中也可以减 ...

  4. $("li")是对象类型不是数组类型

  5. 进程间通信——IPC之共享内存

        共享内存是三个IPC机制中的一个.它允许两个不相关的进程访问同一个逻辑内存.共享内存是在两个正在进行的进程之间传递数据的一种非常有效的方式.   大多数的共享内存的实现,都把由不同进程之间共享 ...

  6. Python_PyMySQL数据库操作

    连接数据库: conn=pymysql.connect(host=,user=',charset='utf8') 建立游标: cur = conn.cursor() 创建一个名字叫 lj 的数据库: ...

  7. windows环境下zookeeper安装和使用

    一.简介        zooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用提供一 ...

  8. Python中urllib.urlencode中文字符的一个问题

    Django项目在访问Restful service时调用urllib.urlencode编码中文字符串时碰到下面这个错误. v = quote_plus(str(v)) UnicodeEncodeE ...

  9. 体育Bank2016会议笔记

    补注:会议全称应该是体育Bank2016体育投融资总裁年会 新华社体育部徐仁基 演讲主题:帮郭川找到大海-->帮民众找到自己真正的体育爱好 激发和培养体育市场是重中之重 将体育培养成生活习惯.生 ...

  10. Ruby中如何复制对象 (deep clone)(转载)

    Ruby中如何复制对象 (deep clone) 用Ruby复制一个对象(object)也许没有你想像的那么容易. 今天我google了半天, 做个总结吧. 先从最简单的开始, b = a 是复制吗? ...