1. Cache2go 是什么
-
Cache2go:有心跳机制的并发安全的单机版 golang 内存数据库
2. 项目结构
- 功能实现相关源码文件主要有 3 个:cache.go、cacheitem.go、cachetable.go
3. 关键数据结构
-
项目中涉及主要数据类型如下:
- CacheItem:缓存表项
- CacheTable:缓存表
3.1 CacheItem
- CacheItem 类型是用来表示一个单独的缓存条目:
// CacheItem 是一个单独的缓存条目 // 参数 data 包含用户设置在缓存里的值. type CacheItem struct { sync.RWMutex // 该项的键 key interface{} // 该项的值 data interface{} // 不能被访问后的存活时间 lifeSpan time.Duration // 被创建的时间戳 createdOn time.Time // 上一次被访问的时间戳 accessedOn time.Time // 多久被访问一次 accessCount int64 // 被删除时触发的回调方法 aboutToExpire []func(key interface{}) }