Skip to content

Commit

Permalink
增加缓存名称配置
Browse files Browse the repository at this point in the history
  • Loading branch information
FishGoddess committed Mar 13, 2023
1 parent 0d28fc6 commit c363efe
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 2 deletions.
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.8

> 此版本发布于 2023-03-13
* 增加缓存名字配置,主要用于区分每个监控数据的来源

### v0.4.7

> 此版本发布于 2023-03-08
Expand Down
4 changes: 4 additions & 0 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ func main() {
// Also, try WithLFU if you want to use lfu to evict data.
cache = cachego.NewCache(cachego.WithLRU(100))
cache = cachego.NewCache(cachego.WithLFU(100))

// Use NewCacheWithReport to create a cache with report.
cache, reporter := cachego.NewCacheWithReport(cachego.WithCacheName("test"))
fmt.Println(reporter.CacheName())
}
```

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ func main() {
// Also, try WithLFU if you want to use lfu to evict data.
cache = cachego.NewCache(cachego.WithLRU(100))
cache = cachego.NewCache(cachego.WithLFU(100))

// Use NewCacheWithReport to create a cache with report.
cache, reporter := cachego.NewCacheWithReport(cachego.WithCacheName("test"))
fmt.Println(reporter.CacheName())
}
```

Expand Down
4 changes: 4 additions & 0 deletions _examples/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,8 @@ func main() {
// Also, try WithLFU if you want to use lfu to evict data.
cache = cachego.NewCache(cachego.WithLRU(100))
cache = cachego.NewCache(cachego.WithLFU(100))

// Use NewCacheWithReport to create a cache with report.
cache, reporter := cachego.NewCacheWithReport(cachego.WithCacheName("test"))
fmt.Println(reporter.CacheName())
}
5 changes: 5 additions & 0 deletions _examples/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,9 @@ func main() {
fmt.Println("CacheSize:", reporter.CacheSize())
fmt.Println("MissedRate:", reporter.MissedRate())
fmt.Println("HitRate:", reporter.HitRate())

// Sometimes you may have several caches in one service.
// You can set each name by WithCacheName and get the name from reporter.
cachego.WithCacheName("test")
reporter.CacheName()
}
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cachego
import "time"

type config struct {
cacheName string
cacheType cacheType
shardings int
singleflight bool
Expand All @@ -41,6 +42,7 @@ type config struct {

func newDefaultConfig() *config {
return &config{
cacheName: "",
cacheType: standard,
shardings: 0,
singleflight: true,
Expand Down
11 changes: 10 additions & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ Package cachego provides an easy way to use foundation for your caching operatio
cache = cachego.NewCache(cachego.WithLRU(100))
cache = cachego.NewCache(cachego.WithLFU(100))
// Use NewCacheWithReport to create a cache with report.
cache, reporter := cachego.NewCacheWithReport(cachego.WithCacheName("test"))
fmt.Println(reporter.CacheName())
2. ttl:
cache := cachego.NewCache()
Expand Down Expand Up @@ -392,6 +396,11 @@ Package cachego provides an easy way to use foundation for your caching operatio
fmt.Println("MissedRate:", reporter.MissedRate())
fmt.Println("HitRate:", reporter.HitRate())
// Sometimes you may have several caches in one service.
// You can set each name by WithCacheName and get the name from reporter.
cachego.WithCacheName("test")
reporter.CacheName()
9. task:
var (
Expand Down Expand Up @@ -468,4 +477,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.7"
const Version = "v0.4.8"
7 changes: 7 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ func applyOptions(conf *config, opts []Option) {
}
}

// WithCacheName returns an option setting the cacheName of config.
func WithCacheName(cacheName string) Option {
return func(conf *config) {
conf.cacheName = cacheName
}
}

// WithLRU returns an option setting the type of cache to lru.
// Notice that lru cache must have max entries limit, so you have to specify a maxEntries.
func WithLRU(maxEntries int) Option {
Expand Down
11 changes: 11 additions & 0 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ import (
"time"
)

// go test -v -cover -run=^TestWithCacheName$
func TestWithCacheName(t *testing.T) {
got := &config{cacheName: ""}
expect := &config{cacheName: "-"}

WithCacheName("-").applyTo(got)
if !isConfigEquals(got, expect) {
t.Errorf("got %+v != expect %+v", got, expect)
}
}

// go test -v -cover -run=^TestWithLRU$
func TestWithLRU(t *testing.T) {
got := &config{cacheType: standard, maxEntries: 0}
Expand Down
2 changes: 1 addition & 1 deletion pkg/clock/clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (c *Clock) start() {
atomic.AddInt64(&c.now, int64(duration))
}

time.Sleep(100 * time.Millisecond)
time.Sleep(duration)
atomic.StoreInt64(&c.now, time.Now().UnixNano())
}
}
Expand Down
8 changes: 8 additions & 0 deletions report.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

// Reporter stores some values for reporting.
type Reporter struct {
conf *config
cache Cache

missedCount uint64
Expand All @@ -45,6 +46,12 @@ func (r *Reporter) increaseLoadCount() {
atomic.AddUint64(&r.loadCount, 1)
}

// CacheName returns the name of cache.
// You can use WithCacheName to set cache's name.
func (r *Reporter) CacheName() string {
return r.conf.cacheName
}

// CountMissed returns the missed count.
func (r *Reporter) CountMissed() uint64 {
return atomic.LoadUint64(&r.missedCount)
Expand Down Expand Up @@ -103,6 +110,7 @@ type reportableCache struct {

func report(conf *config, cache Cache) (Cache, *Reporter) {
reporter := &Reporter{
conf: conf,
cache: cache,
hitCount: 0,
missedCount: 0,
Expand Down
13 changes: 13 additions & 0 deletions report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ import (
"time"
)

const (
testCacheName = "test"
)

func newTestReportableCache() (*reportableCache, *Reporter) {
conf := newDefaultConfig()
conf.cacheName = testCacheName
conf.maxEntries = maxTestEntries
cache, reporter := report(conf, newStandardCache(conf))
return cache.(*reportableCache), reporter
Expand Down Expand Up @@ -195,6 +200,14 @@ func TestReportableCacheReportLoad(t *testing.T) {
}
}

// go test -v -cover -run=^TestReporterCacheName$
func TestReporterCacheName(t *testing.T) {
_, reporter := newTestReportableCache()
if reporter.CacheName() != testCacheName {
t.Errorf("CacheName %s is wrong", reporter.CacheName())
}
}

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

0 comments on commit c363efe

Please sign in to comment.