From c363efe4101259b01980702689a0a616f334e15d Mon Sep 17 00:00:00 2001 From: FishGoddess <1149062639@qq.com> Date: Mon, 13 Mar 2023 16:02:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BC=93=E5=AD=98=E5=90=8D?= =?UTF-8?q?=E7=A7=B0=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HISTORY.md | 6 ++++++ README.en.md | 4 ++++ README.md | 4 ++++ _examples/basic.go | 4 ++++ _examples/report.go | 5 +++++ config.go | 2 ++ doc.go | 11 ++++++++++- option.go | 7 +++++++ option_test.go | 11 +++++++++++ pkg/clock/clock.go | 2 +- report.go | 8 ++++++++ report_test.go | 13 +++++++++++++ 12 files changed, 75 insertions(+), 2 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 7e9e5a0..82065c2 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,11 @@ ## ✒ 历史版本的特性介绍 (Features in old versions) +### v0.4.8 + +> 此版本发布于 2023-03-13 + +* 增加缓存名字配置,主要用于区分每个监控数据的来源 + ### v0.4.7 > 此版本发布于 2023-03-08 diff --git a/README.en.md b/README.en.md index 76c0ab3..c5c4a58 100644 --- a/README.en.md +++ b/README.en.md @@ -93,6 +93,10 @@ func main() { // Also, try WithLFU if you want to use lfu to evict data. cache = cachego.NewCache(cachego.WithLRU(100)) cache = cachego.NewCache(cachego.WithLFU(100)) + + // Use NewCacheWithReport to create a cache with report. + cache, reporter := cachego.NewCacheWithReport(cachego.WithCacheName("test")) + fmt.Println(reporter.CacheName()) } ``` diff --git a/README.md b/README.md index 2e138ce..6c9b184 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,10 @@ func main() { // Also, try WithLFU if you want to use lfu to evict data. cache = cachego.NewCache(cachego.WithLRU(100)) cache = cachego.NewCache(cachego.WithLFU(100)) + + // Use NewCacheWithReport to create a cache with report. + cache, reporter := cachego.NewCacheWithReport(cachego.WithCacheName("test")) + fmt.Println(reporter.CacheName()) } ``` diff --git a/_examples/basic.go b/_examples/basic.go index dd030f3..4b047c3 100644 --- a/_examples/basic.go +++ b/_examples/basic.go @@ -69,4 +69,8 @@ func main() { // Also, try WithLFU if you want to use lfu to evict data. cache = cachego.NewCache(cachego.WithLRU(100)) cache = cachego.NewCache(cachego.WithLFU(100)) + + // Use NewCacheWithReport to create a cache with report. + cache, reporter := cachego.NewCacheWithReport(cachego.WithCacheName("test")) + fmt.Println(reporter.CacheName()) } diff --git a/_examples/report.go b/_examples/report.go index b34afcb..548b6e3 100644 --- a/_examples/report.go +++ b/_examples/report.go @@ -84,4 +84,9 @@ 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/config.go b/config.go index 4a4b5b7..586360c 100644 --- a/config.go +++ b/config.go @@ -17,6 +17,7 @@ package cachego import "time" type config struct { + cacheName string cacheType cacheType shardings int singleflight bool @@ -41,6 +42,7 @@ type config struct { func newDefaultConfig() *config { return &config{ + cacheName: "", cacheType: standard, shardings: 0, singleflight: true, diff --git a/doc.go b/doc.go index 89e1b7e..653d83a 100644 --- a/doc.go +++ b/doc.go @@ -65,6 +65,10 @@ Package cachego provides an easy way to use foundation for your caching operatio cache = cachego.NewCache(cachego.WithLRU(100)) cache = cachego.NewCache(cachego.WithLFU(100)) + // Use NewCacheWithReport to create a cache with report. + cache, reporter := cachego.NewCacheWithReport(cachego.WithCacheName("test")) + fmt.Println(reporter.CacheName()) + 2. ttl: cache := cachego.NewCache() @@ -392,6 +396,11 @@ 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 ( @@ -468,4 +477,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.7" +const Version = "v0.4.8" diff --git a/option.go b/option.go index 6a9b1fc..192dda2 100644 --- a/option.go +++ b/option.go @@ -31,6 +31,13 @@ func applyOptions(conf *config, opts []Option) { } } +// WithCacheName returns an option setting the cacheName of config. +func WithCacheName(cacheName string) Option { + return func(conf *config) { + conf.cacheName = cacheName + } +} + // WithLRU returns an option setting the type of cache to lru. // Notice that lru cache must have max entries limit, so you have to specify a maxEntries. func WithLRU(maxEntries int) Option { diff --git a/option_test.go b/option_test.go index 534682d..610a577 100644 --- a/option_test.go +++ b/option_test.go @@ -19,6 +19,17 @@ import ( "time" ) +// go test -v -cover -run=^TestWithCacheName$ +func TestWithCacheName(t *testing.T) { + got := &config{cacheName: ""} + expect := &config{cacheName: "-"} + + WithCacheName("-").applyTo(got) + if !isConfigEquals(got, expect) { + t.Errorf("got %+v != expect %+v", got, expect) + } +} + // go test -v -cover -run=^TestWithLRU$ func TestWithLRU(t *testing.T) { got := &config{cacheType: standard, maxEntries: 0} diff --git a/pkg/clock/clock.go b/pkg/clock/clock.go index cfc8038..6a3345e 100644 --- a/pkg/clock/clock.go +++ b/pkg/clock/clock.go @@ -87,7 +87,7 @@ func (c *Clock) start() { atomic.AddInt64(&c.now, int64(duration)) } - time.Sleep(100 * time.Millisecond) + time.Sleep(duration) atomic.StoreInt64(&c.now, time.Now().UnixNano()) } } diff --git a/report.go b/report.go index 990d9c0..a5601e8 100644 --- a/report.go +++ b/report.go @@ -21,6 +21,7 @@ import ( // Reporter stores some values for reporting. type Reporter struct { + conf *config cache Cache missedCount uint64 @@ -45,6 +46,12 @@ func (r *Reporter) increaseLoadCount() { atomic.AddUint64(&r.loadCount, 1) } +// CacheName returns the name of cache. +// You can use WithCacheName to set cache's name. +func (r *Reporter) CacheName() string { + return r.conf.cacheName +} + // CountMissed returns the missed count. func (r *Reporter) CountMissed() uint64 { return atomic.LoadUint64(&r.missedCount) @@ -103,6 +110,7 @@ type reportableCache struct { func report(conf *config, cache Cache) (Cache, *Reporter) { reporter := &Reporter{ + conf: conf, cache: cache, hitCount: 0, missedCount: 0, diff --git a/report_test.go b/report_test.go index 4cbfc6b..a974dc9 100644 --- a/report_test.go +++ b/report_test.go @@ -20,8 +20,13 @@ import ( "time" ) +const ( + testCacheName = "test" +) + func newTestReportableCache() (*reportableCache, *Reporter) { conf := newDefaultConfig() + conf.cacheName = testCacheName conf.maxEntries = maxTestEntries cache, reporter := report(conf, newStandardCache(conf)) return cache.(*reportableCache), reporter @@ -195,6 +200,14 @@ func TestReportableCacheReportLoad(t *testing.T) { } } +// go test -v -cover -run=^TestReporterCacheName$ +func TestReporterCacheName(t *testing.T) { + _, reporter := newTestReportableCache() + if reporter.CacheName() != testCacheName { + t.Errorf("CacheName %s is wrong", reporter.CacheName()) + } +} + // go test -v -cover -run=^TestReporterCacheSize$ func TestReporterCacheSize(t *testing.T) { cache, reporter := newTestReportableCache()