Skip to content

Commit

Permalink
给 Reporter 增加缓存类型方法
Browse files Browse the repository at this point in the history
  • Loading branch information
FishGoddess committed May 10, 2023
1 parent e79c0c5 commit ec5e877
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 34 deletions.
3 changes: 2 additions & 1 deletion FUTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
* [x] 提供定时缓存时间的机制,可选快速时钟
* [x] 增加缓存名字配置,主要用于区分每个监控数据的来源
* [x] 给 Reporter 增加缓存分片数量方法,主要用于监控缓存分片数量
* [ ] 给 Reporter 增加缓存类型方法,主要用于监控不同类型的缓存情况
* [x] 给 Reporter 增加缓存类型方法,主要用于监控不同类型缓存的使用情况
* [ ] ~~增加对不存在的数据做防穿透的机制~~
经过实践,这个更适合业务方自己处理,所以这边就先去掉了
* [ ] 完善监控上报器,提供更多缓存信息查询的方法
* [ ] 提供一个清空并设置全量值的方法,方便定时数据的全量替换

### v0.3.x
Expand Down
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## ✒ 历史版本的特性介绍 (Features in old versions)

### v0.4.10

> 此版本发布于 2023-05-10
* 给 Reporter 增加缓存类型方法,主要用于监控不同类型缓存的使用情况

### v0.4.9

> 此版本发布于 2023-05-09
Expand Down
1 change: 1 addition & 0 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
```

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
```

Expand Down
1 change: 1 addition & 0 deletions _examples/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
26 changes: 14 additions & 12 deletions _examples/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -76,17 +81,14 @@ 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())
fmt.Println("CountLoad:", reporter.CountLoad())
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()
}
28 changes: 21 additions & 7 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import "time"

type config struct {
cacheName string
cacheType cacheType
cacheType CacheType
shardings int
singleflight bool
gcDuration time.Duration
Expand Down
29 changes: 16 additions & 13 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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),
Expand All @@ -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())
Expand All @@ -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 (
Expand Down Expand Up @@ -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"
6 changes: 6 additions & 0 deletions report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit ec5e877

Please sign in to comment.