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. asp.net core选项配置的研究

    asp.net-core选项模块是全新,可拓展的框架,其作用在整个.net-core框架中,就像依赖注入一样无处不在,是一个很重要的组件. 其实配置模块与选项模块是紧密相连的,我们可以使用Config ...

  2. java web--DOM

    Dom总结dom:文档对象模型的简称.dom的解析:与XML一样遵循同样的规范 将标记型文档解析成一棵DOM树,并将树中的内容都封装成节点对象. 如果html文档过大,同样会造成解析过慢,怎么使用sa ...

  3. mysql cluster部署

    一.mysql cluster的基本概念 1.mysql cluster的组成            管理(MGM)节点:这类节点的作用是管理MySQL Cluster内的其他节点,如提供配置数据.启 ...

  4. Netstat状态分类

    用netstat -an命令查看!再stat下面有一些英文,简单说一下这些英文具体都代表什么: LISTEN:(Listening for a connection.)侦听来自远方的TCP端口的连接请 ...

  5. Socket.io文字直播聊天室的简单代码

    直接上代码吧,被注释掉的主要是调试代码,和技术选型的测试代码 var app = require('express')(); var server = require('http').Server(a ...

  6. Phaser文档访问不了,下载英文版文档到本地,已经共享在国内网站上面

    点击链接查看, http://www.simuhunluo.top/Phaser/ 可以找到你所需要的类.

  7. C#文件和字节流的转换方法

    1.读取文件,并转换为字节流 FileStream fs = new FileStream(filename,FileMode.Open,FileAccess.Read); byte[] infbyt ...

  8. IOC框架:Unity

    Unity 是一个轻量级.可扩展的依赖注入容器,支持构造函数.属性和方法调用注入. 在进行项目之前通过Nuget安装Unity 简单的例子 定义一个接口 namespace UnityTest { / ...

  9. webpack的css压缩不兼容IOS8问题探索

    webpack使用postcss的autoprefixer插件,并在压缩css时使用了cssnano,处理不当的情况下会导致压缩css后,部分兼容前缀(比如-webkit-)被删除的问题. postc ...

  10. MapReduce的架构及原理

    MapReduce是一种分布式计算模型,是Hadoop的主要组成之一,承担大批量数据的计算功能.MapReduce分为两个阶段:Map和Reduce. 一.MapReduce的架构演变 客户端向Job ...