Skip to content

Commit

Permalink
给 Reporter 增加缓存 gc 运行间隔方法
Browse files Browse the repository at this point in the history
  • Loading branch information
FishGoddess committed Jun 12, 2023
1 parent 9e4ccec commit 1cb7be3
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 12 deletions.
5 changes: 4 additions & 1 deletion FUTURE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## ✒ 未来版本的新特性 (Features in future versions)

### v0.5.x

* [ ] 提供一个清空并设置全量值的方法,方便定时数据的全量替换

### v0.4.x

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

### 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.12

> 此版本发布于 2023-06-12
* 给 Reporter 增加缓存 gc 运行间隔方法,主要用于监控不同缓存的 gc 运行间隔配置情况

### v0.4.11

> 此版本发布于 2023-05-10
Expand Down
2 changes: 1 addition & 1 deletion README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
**cachego** is an api friendly memory-based cache for [GoLang](https://golang.org) applications.

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

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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

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

Expand Down
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,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.11"
const Version = "v0.4.12"
17 changes: 12 additions & 5 deletions report.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ func (r *Reporter) CacheShardings() int {
return r.conf.shardings
}

// CacheGC returns the gc duration of cache.
// You can use WithGC to set cache's gc duration.
// Zero duration means cache disables gc.
func (r *Reporter) CacheGC() time.Duration {
return r.conf.gcDuration
}

// CacheSize returns the size of cache.
func (r *Reporter) CacheSize() int {
return r.cache.Size()
}

// CountMissed returns the missed count.
func (r *Reporter) CountMissed() uint64 {
return atomic.LoadUint64(&r.missedCount)
Expand All @@ -85,11 +97,6 @@ func (r *Reporter) CountLoad() uint64 {
return atomic.LoadUint64(&r.loadCount)
}

// CacheSize returns the size of cache.
func (r *Reporter) CacheSize() int {
return r.cache.Size()
}

// MissedRate returns the missed rate.
func (r *Reporter) MissedRate() float64 {
hit := r.CountHit()
Expand Down
20 changes: 17 additions & 3 deletions report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ import (
)

const (
testCacheName = "test"
testCacheType = lru
testCacheShardings = 16
testCacheName = "test"
testCacheType = lru
testCacheShardings = 16
testCacheGCDuration = 10 * time.Minute
)

func newTestReportableCache() (*reportableCache, *Reporter) {
conf := newDefaultConfig()
conf.cacheName = testCacheName
conf.cacheType = testCacheType
conf.shardings = testCacheShardings
conf.gcDuration = testCacheGCDuration
conf.maxEntries = maxTestEntries

cache, reporter := report(conf, newStandardCache(conf))
Expand Down Expand Up @@ -241,6 +243,18 @@ func TestReporterCacheShardings(t *testing.T) {
}
}

// go test -v -cover -run=^TestReporterCacheGC$
func TestReporterCacheGC(t *testing.T) {
_, reporter := newTestReportableCache()
if reporter.CacheGC() != reporter.conf.gcDuration {
t.Errorf("CacheGC %d is wrong compared with conf", reporter.CacheGC())
}

if reporter.CacheGC() != testCacheGCDuration {
t.Errorf("CacheGC %d is wrong", reporter.CacheGC())
}
}

// go test -v -cover -run=^TestReporterCacheSize$
func TestReporterCacheSize(t *testing.T) {
cache, reporter := newTestReportableCache()
Expand Down

0 comments on commit 1cb7be3

Please sign in to comment.