3 Star 0 Fork 0

Gitee 极速下载/gcache

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/bluele/gcache
克隆/下载
clock.go 800 Bytes
一键复制 编辑 原始数据 按行查看 历史
pcman312 提交于 2017-06-10 09:55 +08:00 . Add clock interface, real and fake clocks (#33)
package gcache
import (
"sync"
"time"
)
type Clock interface {
Now() time.Time
}
type RealClock struct{}
func NewRealClock() Clock {
return RealClock{}
}
func (rc RealClock) Now() time.Time {
t := time.Now()
return t
}
type FakeClock interface {
Clock
Advance(d time.Duration)
}
func NewFakeClock() FakeClock {
return &fakeclock{
// Taken from github.com/jonboulle/clockwork: use a fixture that does not fulfill Time.IsZero()
now: time.Date(1984, time.April, 4, 0, 0, 0, 0, time.UTC),
}
}
type fakeclock struct {
now time.Time
mutex sync.RWMutex
}
func (fc *fakeclock) Now() time.Time {
fc.mutex.RLock()
defer fc.mutex.RUnlock()
t := fc.now
return t
}
func (fc *fakeclock) Advance(d time.Duration) {
fc.mutex.Lock()
defer fc.mutex.Unlock()
fc.now = fc.now.Add(d)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mirrors/gcache.git
git@gitee.com:mirrors/gcache.git
mirrors
gcache
gcache
master

搜索帮助