Skip to content

Commit c363efe

Browse files
committed
增加缓存名称配置
1 parent 0d28fc6 commit c363efe

File tree

12 files changed

+75
-2
lines changed

12 files changed

+75
-2
lines changed

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.8
4+
5+
> 此版本发布于 2023-03-13
6+
7+
* 增加缓存名字配置,主要用于区分每个监控数据的来源
8+
39
### v0.4.7
410

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

README.en.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ func main() {
9393
// Also, try WithLFU if you want to use lfu to evict data.
9494
cache = cachego.NewCache(cachego.WithLRU(100))
9595
cache = cachego.NewCache(cachego.WithLFU(100))
96+
97+
// Use NewCacheWithReport to create a cache with report.
98+
cache, reporter := cachego.NewCacheWithReport(cachego.WithCacheName("test"))
99+
fmt.Println(reporter.CacheName())
96100
}
97101
```
98102

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ func main() {
9292
// Also, try WithLFU if you want to use lfu to evict data.
9393
cache = cachego.NewCache(cachego.WithLRU(100))
9494
cache = cachego.NewCache(cachego.WithLFU(100))
95+
96+
// Use NewCacheWithReport to create a cache with report.
97+
cache, reporter := cachego.NewCacheWithReport(cachego.WithCacheName("test"))
98+
fmt.Println(reporter.CacheName())
9599
}
96100
```
97101

_examples/basic.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,8 @@ func main() {
6969
// Also, try WithLFU if you want to use lfu to evict data.
7070
cache = cachego.NewCache(cachego.WithLRU(100))
7171
cache = cachego.NewCache(cachego.WithLFU(100))
72+
73+
// Use NewCacheWithReport to create a cache with report.
74+
cache, reporter := cachego.NewCacheWithReport(cachego.WithCacheName("test"))
75+
fmt.Println(reporter.CacheName())
7276
}

_examples/report.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,9 @@ func main() {
8484
fmt.Println("CacheSize:", reporter.CacheSize())
8585
fmt.Println("MissedRate:", reporter.MissedRate())
8686
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()
8792
}

config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package cachego
1717
import "time"
1818

1919
type config struct {
20+
cacheName string
2021
cacheType cacheType
2122
shardings int
2223
singleflight bool
@@ -41,6 +42,7 @@ type config struct {
4142

4243
func newDefaultConfig() *config {
4344
return &config{
45+
cacheName: "",
4446
cacheType: standard,
4547
shardings: 0,
4648
singleflight: true,

doc.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ Package cachego provides an easy way to use foundation for your caching operatio
6565
cache = cachego.NewCache(cachego.WithLRU(100))
6666
cache = cachego.NewCache(cachego.WithLFU(100))
6767
68+
// Use NewCacheWithReport to create a cache with report.
69+
cache, reporter := cachego.NewCacheWithReport(cachego.WithCacheName("test"))
70+
fmt.Println(reporter.CacheName())
71+
6872
2. ttl:
6973
7074
cache := cachego.NewCache()
@@ -392,6 +396,11 @@ Package cachego provides an easy way to use foundation for your caching operatio
392396
fmt.Println("MissedRate:", reporter.MissedRate())
393397
fmt.Println("HitRate:", reporter.HitRate())
394398
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+
395404
9. task:
396405
397406
var (
@@ -468,4 +477,4 @@ Package cachego provides an easy way to use foundation for your caching operatio
468477
package cachego // import "github.com/FishGoddess/cachego"
469478

470479
// Version is the version string representation of cachego.
471-
const Version = "v0.4.7"
480+
const Version = "v0.4.8"

option.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ func applyOptions(conf *config, opts []Option) {
3131
}
3232
}
3333

34+
// WithCacheName returns an option setting the cacheName of config.
35+
func WithCacheName(cacheName string) Option {
36+
return func(conf *config) {
37+
conf.cacheName = cacheName
38+
}
39+
}
40+
3441
// WithLRU returns an option setting the type of cache to lru.
3542
// Notice that lru cache must have max entries limit, so you have to specify a maxEntries.
3643
func WithLRU(maxEntries int) Option {

option_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ import (
1919
"time"
2020
)
2121

22+
// go test -v -cover -run=^TestWithCacheName$
23+
func TestWithCacheName(t *testing.T) {
24+
got := &config{cacheName: ""}
25+
expect := &config{cacheName: "-"}
26+
27+
WithCacheName("-").applyTo(got)
28+
if !isConfigEquals(got, expect) {
29+
t.Errorf("got %+v != expect %+v", got, expect)
30+
}
31+
}
32+
2233
// go test -v -cover -run=^TestWithLRU$
2334
func TestWithLRU(t *testing.T) {
2435
got := &config{cacheType: standard, maxEntries: 0}

pkg/clock/clock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (c *Clock) start() {
8787
atomic.AddInt64(&c.now, int64(duration))
8888
}
8989

90-
time.Sleep(100 * time.Millisecond)
90+
time.Sleep(duration)
9191
atomic.StoreInt64(&c.now, time.Now().UnixNano())
9292
}
9393
}

0 commit comments

Comments
 (0)