diff --git a/FUTURE.md b/FUTURE.md index 6428367..9c19e96 100644 --- a/FUTURE.md +++ b/FUTURE.md @@ -17,9 +17,10 @@ * [x] 提供定时缓存时间的机制,可选快速时钟 * [x] 增加缓存名字配置,主要用于区分每个监控数据的来源 * [x] 给 Reporter 增加缓存分片数量方法,主要用于监控缓存分片数量 -* [ ] 给 Reporter 增加缓存类型方法,主要用于监控不同类型的缓存情况 +* [x] 给 Reporter 增加缓存类型方法,主要用于监控不同类型缓存的使用情况 * [ ] ~~增加对不存在的数据做防穿透的机制~~ 经过实践,这个更适合业务方自己处理,所以这边就先去掉了 +* [ ] 完善监控上报器,提供更多缓存信息查询的方法 * [ ] 提供一个清空并设置全量值的方法,方便定时数据的全量替换 ### v0.3.x diff --git a/HISTORY.md b/HISTORY.md index 73acbdf..ad99fe5 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,11 @@ ## ✒ 历史版本的特性介绍 (Features in old versions) +### v0.4.10 + +> 此版本发布于 2023-05-10 + +* 给 Reporter 增加缓存类型方法,主要用于监控不同类型缓存的使用情况 + ### v0.4.9 > 此版本发布于 2023-05-09 diff --git a/README.en.md b/README.en.md index aa857f4..f937341 100644 --- a/README.en.md +++ b/README.en.md @@ -97,6 +97,7 @@ func main() { // Use NewCacheWithReport to create a cache with report. cache, reporter := cachego.NewCacheWithReport(cachego.WithCacheName("test")) fmt.Println(reporter.CacheName()) + fmt.Println(reporter.CacheType()) } ``` diff --git a/README.md b/README.md index ee1c6e8..f25a21d 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,7 @@ func main() { // Use NewCacheWithReport to create a cache with report. cache, reporter := cachego.NewCacheWithReport(cachego.WithCacheName("test")) fmt.Println(reporter.CacheName()) + fmt.Println(reporter.CacheType()) } ``` diff --git a/_examples/basic.go b/_examples/basic.go index 4b047c3..47b9f67 100644 --- a/_examples/basic.go +++ b/_examples/basic.go @@ -73,4 +73,5 @@ func main() { // Use NewCacheWithReport to create a cache with report. cache, reporter := cachego.NewCacheWithReport(cachego.WithCacheName("test")) fmt.Println(reporter.CacheName()) + fmt.Println(reporter.CacheType()) } diff --git a/_examples/report.go b/_examples/report.go index 548b6e3..c7c69bc 100644 --- a/_examples/report.go +++ b/_examples/report.go @@ -40,16 +40,21 @@ func reportLoad(reporter *cachego.Reporter, key string, value interface{}, ttl t } func main() { - // We provide some reporting points for monitor cache. - // ReportMissed reports the missed key getting from cache. - // ReportHit reports the hit entry getting from cache. - // ReportGC reports the status of cache gc. - // ReportLoad reports the result of loading. - // Use NewCacheWithReport to create a cache with report. + // We provide some ways to report the status of cache. + // Use NewCacheWithReport to create a cache with reporting features. cache, reporter := cachego.NewCacheWithReport( + // Sometimes you may have several caches in one service. + // You can set each name by WithCacheName and get the name from reporter. + cachego.WithCacheName("test"), + + // For testing... cachego.WithMaxEntries(3), cachego.WithGC(100*time.Millisecond), + // ReportMissed reports the missed key getting from cache. + // ReportHit reports the hit entry getting from cache. + // ReportGC reports the status of cache gc. + // ReportLoad reports the result of loading. cachego.WithReportMissed(reportMissed), cachego.WithReportHit(reportHit), cachego.WithReportGC(reportGC), @@ -76,7 +81,9 @@ func main() { fmt.Println(value, err) - // These are some methods of reporter. + // These are some useful methods of reporter. + fmt.Println("CacheName:", reporter.CacheName()) + fmt.Println("CacheType:", reporter.CacheType()) fmt.Println("CountMissed:", reporter.CountMissed()) fmt.Println("CountHit:", reporter.CountHit()) fmt.Println("CountGC:", reporter.CountGC()) @@ -84,9 +91,4 @@ func main() { fmt.Println("CacheSize:", reporter.CacheSize()) fmt.Println("MissedRate:", reporter.MissedRate()) fmt.Println("HitRate:", reporter.HitRate()) - - // Sometimes you may have several caches in one service. - // You can set each name by WithCacheName and get the name from reporter. - cachego.WithCacheName("test") - reporter.CacheName() } diff --git a/cache.go b/cache.go index 62ff27b..5c7abca 100644 --- a/cache.go +++ b/cache.go @@ -28,30 +28,44 @@ const ( ) const ( - _ cacheType = iota - // standard cache is a simple cache with locked map. // It evicts entries randomly if cache size reaches to max entries. - standard + standard CacheType = "standard" // lru cache is a cache using lru to evict entries. // More details see https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU). - lru + lru CacheType = "lru" // lfu cache is a cache using lfu to evict entries. // More details see https://en.wikipedia.org/wiki/Cache_replacement_policies#Least-frequently_used_(LFU). - lfu + lfu CacheType = "lfu" ) var ( - newCaches = map[cacheType]func(conf *config) Cache{ + newCaches = map[CacheType]func(conf *config) Cache{ standard: newStandardCache, lru: newLRUCache, lfu: newLFUCache, } ) -type cacheType = uint8 +// CacheType is the type of cache. +type CacheType string + +// IsStandard returns if cache type is standard. +func (ct CacheType) IsStandard() bool { + return ct == standard +} + +// IsLRU returns if cache type is lru. +func (ct CacheType) IsLRU() bool { + return ct == lru +} + +// IsLFU returns if cache type is lfu. +func (ct CacheType) IsLFU() bool { + return ct == lfu +} // Cache is the core interface of cachego. // We provide some implements including standard cache and sharding cache. diff --git a/cache_test.go b/cache_test.go index 7e644ba..fa62a99 100644 --- a/cache_test.go +++ b/cache_test.go @@ -25,6 +25,21 @@ const ( maxTestEntries = 10 ) +// go test -v -cover -run=^TestCacheType$ +func TestCacheType(t *testing.T) { + if !standard.IsStandard() { + t.Error("!standard.IsStandard()") + } + + if !lru.IsLRU() { + t.Error("!standard.IsLRU()") + } + + if !lfu.IsLFU() { + t.Error("!standard.IsLFU()") + } +} + type testCache struct { cache count int32 diff --git a/config.go b/config.go index 586360c..2069c69 100644 --- a/config.go +++ b/config.go @@ -18,7 +18,7 @@ import "time" type config struct { cacheName string - cacheType cacheType + cacheType CacheType shardings int singleflight bool gcDuration time.Duration diff --git a/doc.go b/doc.go index afc9052..0d88732 100644 --- a/doc.go +++ b/doc.go @@ -68,6 +68,7 @@ Package cachego provides an easy way to use foundation for your caching operatio // Use NewCacheWithReport to create a cache with report. cache, reporter := cachego.NewCacheWithReport(cachego.WithCacheName("test")) fmt.Println(reporter.CacheName()) + fmt.Println(reporter.CacheType()) 2. ttl: @@ -351,16 +352,21 @@ Package cachego provides an easy way to use foundation for your caching operatio fmt.Printf("report: load key %s value %+v ttl %s, err %+v, load count %d\n", key, value, ttl, err, reporter.CountLoad()) } - // We provide some reporting points for monitor cache. - // ReportMissed reports the missed key getting from cache. - // ReportHit reports the hit entry getting from cache. - // ReportGC reports the status of cache gc. - // ReportLoad reports the result of loading. - // Use NewCacheWithReport to create a cache with report. + // We provide some ways to report the status of cache. + // Use NewCacheWithReport to create a cache with reporting features. cache, reporter := cachego.NewCacheWithReport( + // Sometimes you may have several caches in one service. + // You can set each name by WithCacheName and get the name from reporter. + cachego.WithCacheName("test"), + + // For testing... cachego.WithMaxEntries(3), cachego.WithGC(100*time.Millisecond), + // ReportMissed reports the missed key getting from cache. + // ReportHit reports the hit entry getting from cache. + // ReportGC reports the status of cache gc. + // ReportLoad reports the result of loading. cachego.WithReportMissed(reportMissed), cachego.WithReportHit(reportHit), cachego.WithReportGC(reportGC), @@ -387,7 +393,9 @@ Package cachego provides an easy way to use foundation for your caching operatio fmt.Println(value, err) - // These are some methods of reporter. + // These are some useful methods of reporter. + fmt.Println("CacheName:", reporter.CacheName()) + fmt.Println("CacheType:", reporter.CacheType()) fmt.Println("CountMissed:", reporter.CountMissed()) fmt.Println("CountHit:", reporter.CountHit()) fmt.Println("CountGC:", reporter.CountGC()) @@ -396,11 +404,6 @@ Package cachego provides an easy way to use foundation for your caching operatio fmt.Println("MissedRate:", reporter.MissedRate()) fmt.Println("HitRate:", reporter.HitRate()) - // Sometimes you may have several caches in one service. - // You can set each name by WithCacheName and get the name from reporter. - cachego.WithCacheName("test") - reporter.CacheName() - 9. task: var ( @@ -477,4 +480,4 @@ Package cachego provides an easy way to use foundation for your caching operatio package cachego // import "github.com/FishGoddess/cachego" // Version is the version string representation of cachego. -const Version = "v0.4.9" +const Version = "v0.4.10" diff --git a/report.go b/report.go index 16373a4..052e520 100644 --- a/report.go +++ b/report.go @@ -52,6 +52,12 @@ func (r *Reporter) CacheName() string { return r.conf.cacheName } +// CacheType returns the type of cache. +// See CacheType. +func (r *Reporter) CacheType() CacheType { + return r.conf.cacheType +} + // CacheShardings returns the shardings of cache. // You can use WithShardings to set cache's shardings. // Zero shardings means cache is non-sharding. diff --git a/report_test.go b/report_test.go index 9d449f8..66873e2 100644 --- a/report_test.go +++ b/report_test.go @@ -22,12 +22,14 @@ import ( const ( testCacheName = "test" + testCacheType = lru testCacheShardings = 16 ) func newTestReportableCache() (*reportableCache, *Reporter) { conf := newDefaultConfig() conf.cacheName = testCacheName + conf.cacheType = testCacheType conf.shardings = testCacheShardings conf.maxEntries = maxTestEntries @@ -215,6 +217,18 @@ func TestReporterCacheName(t *testing.T) { } } +// go test -v -cover -run=^TestReporterCacheType$ +func TestReporterCacheType(t *testing.T) { + _, reporter := newTestReportableCache() + if reporter.CacheType() != reporter.conf.cacheType { + t.Errorf("CacheType %s is wrong compared with conf", reporter.CacheType()) + } + + if reporter.CacheType() != testCacheType { + t.Errorf("CacheType %s is wrong", reporter.CacheType()) + } +} + // go test -v -cover -run=^TestReporterCacheShardings$ func TestReporterCacheShardings(t *testing.T) { _, reporter := newTestReportableCache()