Skip to content

Commit 5f72f64

Browse files
committed
给 Reporter 增加缓存 gc 运行间隔方法
2 parents 6a8d8b5 + 1cb7be3 commit 5f72f64

File tree

7 files changed

+42
-12
lines changed

7 files changed

+42
-12
lines changed

FUTURE.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## ✒ 未来版本的新特性 (Features in future versions)
22

3+
### v0.5.x
4+
5+
* [ ] 提供一个清空并设置全量值的方法,方便定时数据的全量替换
6+
37
### v0.4.x
48

59
* [x] 设计 Cache 接口,Get 方法用 bool 判断,单个锁结构
@@ -21,7 +25,6 @@
2125
* [ ] ~~增加对不存在的数据做防穿透的机制~~
2226
经过实践,这个更适合业务方自己处理,所以这边就先去掉了
2327
* [ ] 完善监控上报器,提供更多缓存信息查询的方法
24-
* [ ] 提供一个清空并设置全量值的方法,方便定时数据的全量替换
2528

2629
### v0.3.x
2730

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.12
4+
5+
> 此版本发布于 2023-06-12
6+
7+
* 给 Reporter 增加缓存 gc 运行间隔方法,主要用于监控不同缓存的 gc 运行间隔配置情况
8+
39
### v0.4.11
410

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

README.en.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
**cachego** is an api friendly memory-based cache for [GoLang](https://golang.org) applications.
99

1010
> It has been used by many services in production, all services are running stable, and the highest qps in services is
11-
> 96w/s by using v0.3.x, so just use it if you want! 👏🏻
11+
> 96w/s, so just use it if you want! 👏🏻
1212
1313
[阅读中文版的 Read me](./README.md).
1414

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
**cachego** 是一个拥有分片机制的轻量级内存缓存库,API 友好,支持多种数据淘汰机制,可以应用于所有的 [GoLang](https://golang.org) 应用程序中。
99

10-
> 目前已经在多个线上服务中运行稳定,服务日常请求过万 qps,v0.3.x 版本最高抵御过 96w/s qps 的冲击,欢迎使用!👏🏻
10+
> 目前已经在多个线上服务中运行稳定,服务日常请求过万 qps,最高抵御过 96w/s qps 的冲击,欢迎使用!👏🏻
1111
1212
[Read me in English](./README.en.md).
1313

doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,4 +480,4 @@ Package cachego provides an easy way to use foundation for your caching operatio
480480
package cachego // import "github.com/FishGoddess/cachego"
481481

482482
// Version is the version string representation of cachego.
483-
const Version = "v0.4.11"
483+
const Version = "v0.4.12"

report.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ func (r *Reporter) CacheShardings() int {
6565
return r.conf.shardings
6666
}
6767

68+
// CacheGC returns the gc duration of cache.
69+
// You can use WithGC to set cache's gc duration.
70+
// Zero duration means cache disables gc.
71+
func (r *Reporter) CacheGC() time.Duration {
72+
return r.conf.gcDuration
73+
}
74+
75+
// CacheSize returns the size of cache.
76+
func (r *Reporter) CacheSize() int {
77+
return r.cache.Size()
78+
}
79+
6880
// CountMissed returns the missed count.
6981
func (r *Reporter) CountMissed() uint64 {
7082
return atomic.LoadUint64(&r.missedCount)
@@ -85,11 +97,6 @@ func (r *Reporter) CountLoad() uint64 {
8597
return atomic.LoadUint64(&r.loadCount)
8698
}
8799

88-
// CacheSize returns the size of cache.
89-
func (r *Reporter) CacheSize() int {
90-
return r.cache.Size()
91-
}
92-
93100
// MissedRate returns the missed rate.
94101
func (r *Reporter) MissedRate() float64 {
95102
hit := r.CountHit()

report_test.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,18 @@ import (
2121
)
2222

2323
const (
24-
testCacheName = "test"
25-
testCacheType = lru
26-
testCacheShardings = 16
24+
testCacheName = "test"
25+
testCacheType = lru
26+
testCacheShardings = 16
27+
testCacheGCDuration = 10 * time.Minute
2728
)
2829

2930
func newTestReportableCache() (*reportableCache, *Reporter) {
3031
conf := newDefaultConfig()
3132
conf.cacheName = testCacheName
3233
conf.cacheType = testCacheType
3334
conf.shardings = testCacheShardings
35+
conf.gcDuration = testCacheGCDuration
3436
conf.maxEntries = maxTestEntries
3537

3638
cache, reporter := report(conf, newStandardCache(conf))
@@ -241,6 +243,18 @@ func TestReporterCacheShardings(t *testing.T) {
241243
}
242244
}
243245

246+
// go test -v -cover -run=^TestReporterCacheGC$
247+
func TestReporterCacheGC(t *testing.T) {
248+
_, reporter := newTestReportableCache()
249+
if reporter.CacheGC() != reporter.conf.gcDuration {
250+
t.Errorf("CacheGC %d is wrong compared with conf", reporter.CacheGC())
251+
}
252+
253+
if reporter.CacheGC() != testCacheGCDuration {
254+
t.Errorf("CacheGC %d is wrong", reporter.CacheGC())
255+
}
256+
}
257+
244258
// go test -v -cover -run=^TestReporterCacheSize$
245259
func TestReporterCacheSize(t *testing.T) {
246260
cache, reporter := newTestReportableCache()

0 commit comments

Comments
 (0)