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. EventQueue.invokeLater(new Runnable())

    public class EventQueueextends ObjectEventQueue 是一个与平台无关的类,它将来自于底层同位体类和受信任的应用程序类的事件列入队列. 它封装了异步事件指派机 ...

  2. 百度编辑器上传视频音频的bug

    前言:UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于MIT协议,允许自由使用和修改代码,百度Ueditor 支持多种后台语言上传 ...

  3. pslq常用操作

    1,登录后默认自动选中My Objects  默认情况下,PLSQL Developer登录后,Brower里会选择All objects,如果你登录的用户是dba,要展开tables目录,正常情况都 ...

  4. AngularJS + RequireJS

    http://www.startersquad.com/blog/AngularJS-requirejs/ While delivering software projects for startup ...

  5. HDU-5705

    Clock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Problem De ...

  6. Scala编程入门---Map与Tuple

    创建Map //创建一个不可变的Map val ages = Map("Leo" -> 30,"Jen" ->25,"Jack" ...

  7. 浏览器选择最新IE渲染

    <meta http-equiv="X-UA-Compatible" content="IE=edge" />

  8. python笔记:#014#综合应用

    综合应用 -- 名片管理系统 目标 综合应用已经学习过的知识点: 变量 流程控制 函数 模块 开发 名片管理系统 系统需求 程序启动,显示名片管理系统欢迎界面,并显示功能菜单 ************ ...

  9. birt4.6部署到tomcat及启动服务报错解决方法

    一.下载birt-runtime-4.6.0-20160607.zip包 解压后birt-runtime-4.6.0-20160607\WebViewerExample将WebViewerExampl ...

  10. Java基础:JVM垃圾回收算法

    众所周知,Java的垃圾回收是不需要程序员去手动操控的,而是由JVM去完成.本文介绍JVM进行垃圾回收的各种算法. 1. 如何确定某个对象是垃圾 1.1. 引用计数法 1.2. 可达性分析 2. 典型 ...