Skip to content

Commit f62f5cc

Browse files
committed
给 Reporter 增加缓存类型方法
2 parents 6ae9fc1 + ec5e877 commit f62f5cc

File tree

12 files changed

+98
-34
lines changed

12 files changed

+98
-34
lines changed

FUTURE.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
* [x] 提供定时缓存时间的机制,可选快速时钟
1818
* [x] 增加缓存名字配置,主要用于区分每个监控数据的来源
1919
* [x] 给 Reporter 增加缓存分片数量方法,主要用于监控缓存分片数量
20-
* [ ] 给 Reporter 增加缓存类型方法,主要用于监控不同类型的缓存情况
20+
* [x] 给 Reporter 增加缓存类型方法,主要用于监控不同类型缓存的使用情况
2121
* [ ] ~~增加对不存在的数据做防穿透的机制~~
2222
经过实践,这个更适合业务方自己处理,所以这边就先去掉了
23+
* [ ] 完善监控上报器,提供更多缓存信息查询的方法
2324
* [ ] 提供一个清空并设置全量值的方法,方便定时数据的全量替换
2425

2526
### v0.3.x

HISTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## ✒ 历史版本的特性介绍 (Features in old versions)
22

3+
### v0.4.10
4+
5+
> 此版本发布于 2023-05-10
6+
7+
* 给 Reporter 增加缓存类型方法,主要用于监控不同类型缓存的使用情况
8+
39
### v0.4.9
410

511
> 此版本发布于 2023-05-09

README.en.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ func main() {
9797
// Use NewCacheWithReport to create a cache with report.
9898
cache, reporter := cachego.NewCacheWithReport(cachego.WithCacheName("test"))
9999
fmt.Println(reporter.CacheName())
100+
fmt.Println(reporter.CacheType())
100101
}
101102
```
102103

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ func main() {
9696
// Use NewCacheWithReport to create a cache with report.
9797
cache, reporter := cachego.NewCacheWithReport(cachego.WithCacheName("test"))
9898
fmt.Println(reporter.CacheName())
99+
fmt.Println(reporter.CacheType())
99100
}
100101
```
101102

_examples/basic.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,5 @@ func main() {
7373
// Use NewCacheWithReport to create a cache with report.
7474
cache, reporter := cachego.NewCacheWithReport(cachego.WithCacheName("test"))
7575
fmt.Println(reporter.CacheName())
76+
fmt.Println(reporter.CacheType())
7677
}

_examples/report.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,21 @@ func reportLoad(reporter *cachego.Reporter, key string, value interface{}, ttl t
4040
}
4141

4242
func main() {
43-
// We provide some reporting points for monitor cache.
44-
// ReportMissed reports the missed key getting from cache.
45-
// ReportHit reports the hit entry getting from cache.
46-
// ReportGC reports the status of cache gc.
47-
// ReportLoad reports the result of loading.
48-
// Use NewCacheWithReport to create a cache with report.
43+
// We provide some ways to report the status of cache.
44+
// Use NewCacheWithReport to create a cache with reporting features.
4945
cache, reporter := cachego.NewCacheWithReport(
46+
// Sometimes you may have several caches in one service.
47+
// You can set each name by WithCacheName and get the name from reporter.
48+
cachego.WithCacheName("test"),
49+
50+
// For testing...
5051
cachego.WithMaxEntries(3),
5152
cachego.WithGC(100*time.Millisecond),
5253

54+
// ReportMissed reports the missed key getting from cache.
55+
// ReportHit reports the hit entry getting from cache.
56+
// ReportGC reports the status of cache gc.
57+
// ReportLoad reports the result of loading.
5358
cachego.WithReportMissed(reportMissed),
5459
cachego.WithReportHit(reportHit),
5560
cachego.WithReportGC(reportGC),
@@ -76,17 +81,14 @@ func main() {
7681

7782
fmt.Println(value, err)
7883

79-
// These are some methods of reporter.
84+
// These are some useful methods of reporter.
85+
fmt.Println("CacheName:", reporter.CacheName())
86+
fmt.Println("CacheType:", reporter.CacheType())
8087
fmt.Println("CountMissed:", reporter.CountMissed())
8188
fmt.Println("CountHit:", reporter.CountHit())
8289
fmt.Println("CountGC:", reporter.CountGC())
8390
fmt.Println("CountLoad:", reporter.CountLoad())
8491
fmt.Println("CacheSize:", reporter.CacheSize())
8592
fmt.Println("MissedRate:", reporter.MissedRate())
8693
fmt.Println("HitRate:", reporter.HitRate())
87-
88-
// Sometimes you may have several caches in one service.
89-
// You can set each name by WithCacheName and get the name from reporter.
90-
cachego.WithCacheName("test")
91-
reporter.CacheName()
9294
}

cache.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,44 @@ const (
2828
)
2929

3030
const (
31-
_ cacheType = iota
32-
3331
// standard cache is a simple cache with locked map.
3432
// It evicts entries randomly if cache size reaches to max entries.
35-
standard
33+
standard CacheType = "standard"
3634

3735
// lru cache is a cache using lru to evict entries.
3836
// More details see https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU).
39-
lru
37+
lru CacheType = "lru"
4038

4139
// lfu cache is a cache using lfu to evict entries.
4240
// More details see https://en.wikipedia.org/wiki/Cache_replacement_policies#Least-frequently_used_(LFU).
43-
lfu
41+
lfu CacheType = "lfu"
4442
)
4543

4644
var (
47-
newCaches = map[cacheType]func(conf *config) Cache{
45+
newCaches = map[CacheType]func(conf *config) Cache{
4846
standard: newStandardCache,
4947
lru: newLRUCache,
5048
lfu: newLFUCache,
5149
}
5250
)
5351

54-
type cacheType = uint8
52+
// CacheType is the type of cache.
53+
type CacheType string
54+
55+
// IsStandard returns if cache type is standard.
56+
func (ct CacheType) IsStandard() bool {
57+
return ct == standard
58+
}
59+
60+
// IsLRU returns if cache type is lru.
61+
func (ct CacheType) IsLRU() bool {
62+
return ct == lru
63+
}
64+
65+
// IsLFU returns if cache type is lfu.
66+
func (ct CacheType) IsLFU() bool {
67+
return ct == lfu
68+
}
5569

5670
// Cache is the core interface of cachego.
5771
// We provide some implements including standard cache and sharding cache.

cache_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ const (
2525
maxTestEntries = 10
2626
)
2727

28+
// go test -v -cover -run=^TestCacheType$
29+
func TestCacheType(t *testing.T) {
30+
if !standard.IsStandard() {
31+
t.Error("!standard.IsStandard()")
32+
}
33+
34+
if !lru.IsLRU() {
35+
t.Error("!standard.IsLRU()")
36+
}
37+
38+
if !lfu.IsLFU() {
39+
t.Error("!standard.IsLFU()")
40+
}
41+
}
42+
2843
type testCache struct {
2944
cache
3045
count int32

config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import "time"
1818

1919
type config struct {
2020
cacheName string
21-
cacheType cacheType
21+
cacheType CacheType
2222
shardings int
2323
singleflight bool
2424
gcDuration time.Duration

doc.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Package cachego provides an easy way to use foundation for your caching operatio
6868
// Use NewCacheWithReport to create a cache with report.
6969
cache, reporter := cachego.NewCacheWithReport(cachego.WithCacheName("test"))
7070
fmt.Println(reporter.CacheName())
71+
fmt.Println(reporter.CacheType())
7172
7273
2. ttl:
7374
@@ -351,16 +352,21 @@ Package cachego provides an easy way to use foundation for your caching operatio
351352
fmt.Printf("report: load key %s value %+v ttl %s, err %+v, load count %d\n", key, value, ttl, err, reporter.CountLoad())
352353
}
353354
354-
// We provide some reporting points for monitor cache.
355-
// ReportMissed reports the missed key getting from cache.
356-
// ReportHit reports the hit entry getting from cache.
357-
// ReportGC reports the status of cache gc.
358-
// ReportLoad reports the result of loading.
359-
// Use NewCacheWithReport to create a cache with report.
355+
// We provide some ways to report the status of cache.
356+
// Use NewCacheWithReport to create a cache with reporting features.
360357
cache, reporter := cachego.NewCacheWithReport(
358+
// Sometimes you may have several caches in one service.
359+
// You can set each name by WithCacheName and get the name from reporter.
360+
cachego.WithCacheName("test"),
361+
362+
// For testing...
361363
cachego.WithMaxEntries(3),
362364
cachego.WithGC(100*time.Millisecond),
363365
366+
// ReportMissed reports the missed key getting from cache.
367+
// ReportHit reports the hit entry getting from cache.
368+
// ReportGC reports the status of cache gc.
369+
// ReportLoad reports the result of loading.
364370
cachego.WithReportMissed(reportMissed),
365371
cachego.WithReportHit(reportHit),
366372
cachego.WithReportGC(reportGC),
@@ -387,7 +393,9 @@ Package cachego provides an easy way to use foundation for your caching operatio
387393
388394
fmt.Println(value, err)
389395
390-
// These are some methods of reporter.
396+
// These are some useful methods of reporter.
397+
fmt.Println("CacheName:", reporter.CacheName())
398+
fmt.Println("CacheType:", reporter.CacheType())
391399
fmt.Println("CountMissed:", reporter.CountMissed())
392400
fmt.Println("CountHit:", reporter.CountHit())
393401
fmt.Println("CountGC:", reporter.CountGC())
@@ -396,11 +404,6 @@ Package cachego provides an easy way to use foundation for your caching operatio
396404
fmt.Println("MissedRate:", reporter.MissedRate())
397405
fmt.Println("HitRate:", reporter.HitRate())
398406
399-
// Sometimes you may have several caches in one service.
400-
// You can set each name by WithCacheName and get the name from reporter.
401-
cachego.WithCacheName("test")
402-
reporter.CacheName()
403-
404407
9. task:
405408
406409
var (
@@ -477,4 +480,4 @@ Package cachego provides an easy way to use foundation for your caching operatio
477480
package cachego // import "github.com/FishGoddess/cachego"
478481

479482
// Version is the version string representation of cachego.
480-
const Version = "v0.4.9"
483+
const Version = "v0.4.10"

0 commit comments

Comments
 (0)