Skip to content

Commit 3b75556

Browse files
authored
Merge pull request #8 from FishGoddess/develop
给 Reporter 增加缓存分片数量方法
2 parents c363efe + 591c9a4 commit 3b75556

File tree

8 files changed

+47
-7
lines changed

8 files changed

+47
-7
lines changed

FUTURE.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
* [x] 增加 report 机制用于监控缓存的情况
1616
* [x] 提取 now 和 hash 到缓存级别配置
1717
* [x] 提供定时缓存时间的机制,可选快速时钟
18-
* [ ] 增加对不存在的数据做防穿透的机制
18+
* [x] 增加缓存名字配置,主要用于区分每个监控数据的来源
19+
* [x] 给 Reporter 增加缓存分片数量方法,主要用于监控缓存分片数量
20+
* [ ] 给 Reporter 增加缓存类型方法,主要用于监控不同类型的缓存情况
21+
* [ ] ~~增加对不存在的数据做防穿透的机制~~
22+
经过实践,这个更适合业务方自己处理,所以这边就先去掉了
1923
* [ ] 提供一个清空并设置全量值的方法,方便定时数据的全量替换
2024

2125
### 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.9
4+
5+
> 此版本发布于 2023-05-09
6+
7+
* 给 Reporter 增加缓存分片数量方法,主要用于监控缓存分片数量
8+
39
### v0.4.8
410

511
> 此版本发布于 2023-03-13

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, so just use it if you want! 👏🏻
11+
> 96w/s by using v0.3.x, 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-
> 目前 v0.3.x 版本已经在多个线上服务中运行稳定,服务日常请求过万 qps,瞬时最高抵御过 96w/s qps 的冲击,欢迎使用!👏🏻
10+
> 目前已经在多个线上服务中运行稳定,服务日常请求过万 qps,v0.3.x 版本最高抵御过 96w/s qps 的冲击,欢迎使用!👏🏻
1111
1212
[Read me in English](./README.en.md).
1313

entry_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ import (
2020
"time"
2121
)
2222

23+
const (
24+
testDurationGap = 10 * time.Microsecond
25+
)
26+
2327
// go test -v -cover -run=^TestNewEntry$
2428
func TestNewEntry(t *testing.T) {
2529
e := newEntry("key", "value", 0, now)
@@ -60,7 +64,7 @@ func TestNewEntry(t *testing.T) {
6064
}
6165

6266
// Keep one us for code running.
63-
if expiration < e.expiration || e.expiration < expiration-time.Microsecond.Nanoseconds() {
67+
if expiration < e.expiration || e.expiration < expiration-testDurationGap.Nanoseconds() {
6468
t.Errorf("e.expiration %d != expiration %d", e.expiration, expiration)
6569
}
6670
}
@@ -102,7 +106,7 @@ func TestEntrySetup(t *testing.T) {
102106
}
103107

104108
// Keep one us for code running.
105-
if expiration < e.expiration || e.expiration < expiration-time.Microsecond.Nanoseconds() {
109+
if expiration < e.expiration || e.expiration < expiration-testDurationGap.Nanoseconds() {
106110
t.Errorf("e.expiration %d != expiration %d", e.expiration, expiration)
107111
}
108112
}

global_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestNow(t *testing.T) {
4242
got := now()
4343
expect := time.Now().UnixNano()
4444

45-
if got > expect || got < expect-time.Microsecond.Nanoseconds() {
45+
if got > expect || got < expect-testDurationGap.Nanoseconds() {
4646
t.Errorf("got %d != expect %d", got, expect)
4747
}
4848
}

report.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ func (r *Reporter) CacheName() string {
5252
return r.conf.cacheName
5353
}
5454

55+
// CacheShardings returns the shardings of cache.
56+
// You can use WithShardings to set cache's shardings.
57+
// Zero shardings means cache is non-sharding.
58+
func (r *Reporter) CacheShardings() int {
59+
return r.conf.shardings
60+
}
61+
5562
// CountMissed returns the missed count.
5663
func (r *Reporter) CountMissed() uint64 {
5764
return atomic.LoadUint64(&r.missedCount)

report_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ import (
2121
)
2222

2323
const (
24-
testCacheName = "test"
24+
testCacheName = "test"
25+
testCacheShardings = 16
2526
)
2627

2728
func newTestReportableCache() (*reportableCache, *Reporter) {
2829
conf := newDefaultConfig()
2930
conf.cacheName = testCacheName
31+
conf.shardings = testCacheShardings
3032
conf.maxEntries = maxTestEntries
33+
3134
cache, reporter := report(conf, newStandardCache(conf))
3235
return cache.(*reportableCache), reporter
3336
}
@@ -203,11 +206,27 @@ func TestReportableCacheReportLoad(t *testing.T) {
203206
// go test -v -cover -run=^TestReporterCacheName$
204207
func TestReporterCacheName(t *testing.T) {
205208
_, reporter := newTestReportableCache()
209+
if reporter.CacheName() != reporter.conf.cacheName {
210+
t.Errorf("CacheName %s is wrong compared with conf", reporter.CacheName())
211+
}
212+
206213
if reporter.CacheName() != testCacheName {
207214
t.Errorf("CacheName %s is wrong", reporter.CacheName())
208215
}
209216
}
210217

218+
// go test -v -cover -run=^TestReporterCacheShardings$
219+
func TestReporterCacheShardings(t *testing.T) {
220+
_, reporter := newTestReportableCache()
221+
if reporter.CacheShardings() != reporter.conf.shardings {
222+
t.Errorf("CacheShardings %d is wrong compared with conf", reporter.CacheShardings())
223+
}
224+
225+
if reporter.CacheShardings() != testCacheShardings {
226+
t.Errorf("CacheShardings %d is wrong", reporter.CacheShardings())
227+
}
228+
}
229+
211230
// go test -v -cover -run=^TestReporterCacheSize$
212231
func TestReporterCacheSize(t *testing.T) {
213232
cache, reporter := newTestReportableCache()

0 commit comments

Comments
 (0)