Skip to content

Commit b57d3a2

Browse files
authored
Merge pull request #11 from FishGoddess/develop
v0.6.0-alpha
2 parents a2285c6 + 0426d61 commit b57d3a2

34 files changed

+322
-252
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
- name: Setup
1313
uses: actions/setup-go@v4
1414
with:
15-
go-version: "1.17"
15+
go-version: "1.20"
1616
- run: go version
1717

1818
- name: Checkout

FUTURE.md

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

3+
### v0.6.x
4+
5+
* [ ] 梳理代码,优化代码风格,精简部分代码和注释
6+
* [ ] 完善监控上报器,提供更多缓存信息查询的方法
7+
38
### v0.5.x
49

510
* [ ] ~~提供一个清空并设置全量值的方法,方便定时数据的全量替换~~
611
目前还找不到一个合适的设计去加入这个功能,并且也不是非常刚需,通过业务手段可以处理,所以先不加
7-
* [ ] 完善监控上报器,提供更多缓存信息查询的方法
812

913
### v0.4.x
1014

HISTORY.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
## ✒ 历史版本的特性介绍 (Features in old versions)
22

3+
### v0.6.0-alpha
4+
5+
> 此版本发布于 2024-01-13
6+
7+
* 受小徒弟的灵感激发,进行 loader 代码的调整
8+
* 把 cache 结构去掉,精简这部分设计
9+
310
### v0.5.0
411

512
> 此版本发布于 2023-11-30

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
all: test bench
44

55
test:
6-
go test -cover ./...
6+
go test -cover -count=1 -test.cpu=1 ./...
77

88
bench:
9-
go test -v -bench=. -benchtime=1s ./_examples/performance_test.go
9+
go test -v ./_examples/performance_test.go -bench=. -benchtime=1s
1010

1111
fmt:
1212
go fmt ./...

README.en.md

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

33
[![Go Doc](_icons/godoc.svg)](https://pkg.go.dev/github.com/FishGoddess/cachego)
44
[![License](_icons/license.svg)](https://www.apache.org/licenses/LICENSE-2.0.html)
5-
[![License](_icons/coverage.svg)](_icons/coverage.svg)
5+
[![Coverage](_icons/coverage.svg)](_icons/coverage.svg)
66
![Test](https://github.com/FishGoddess/cachego/actions/workflows/test.yml/badge.svg)
77

88
**cachego** is an api friendly memory-based cache for [GoLang](https://golang.org) applications.

README.md

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

33
[![Go Doc](_icons/godoc.svg)](https://pkg.go.dev/github.com/FishGoddess/cachego)
44
[![License](_icons/license.svg)](https://www.apache.org/licenses/LICENSE-2.0.html)
5-
[![License](_icons/coverage.svg)](_icons/coverage.svg)
5+
[![Coverage](_icons/coverage.svg)](_icons/coverage.svg)
66
![Test](https://github.com/FishGoddess/cachego/actions/workflows/test.yml/badge.svg)
77

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

_examples/task.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,5 @@ func main() {
5454
// Duration is the duration between two loop of fn, optional.
5555
// Run will start a new goroutine and run the task loop.
5656
// The task will stop if context is done.
57-
task.New(printContextValue).
58-
Before(beforePrint).
59-
After(afterPrint).
60-
Context(ctx).
61-
Duration(time.Second).
62-
Run()
57+
task.New(printContextValue).Before(beforePrint).After(afterPrint).Context(ctx).Duration(time.Second).Run()
6358
}

_icons/coverage.svg

Lines changed: 2 additions & 2 deletions
Loading

_icons/jetbrains.png

-122 KB
Binary file not shown.

cache.go

Lines changed: 21 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package cachego
1616

1717
import (
1818
"context"
19-
"sync"
2019
"time"
2120

2221
"github.com/FishGoddess/cachego/pkg/task"
@@ -27,20 +26,6 @@ const (
2726
NoTTL = 0
2827
)
2928

30-
const (
31-
// standard cache is a simple cache with locked map.
32-
// It evicts entries randomly if cache size reaches to max entries.
33-
standard CacheType = "standard"
34-
35-
// lru cache is a cache using lru to evict entries.
36-
// More details see https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU).
37-
lru CacheType = "lru"
38-
39-
// lfu cache is a cache using lfu to evict entries.
40-
// More details see https://en.wikipedia.org/wiki/Cache_replacement_policies#Least-frequently_used_(LFU).
41-
lfu CacheType = "lfu"
42-
)
43-
4429
var (
4530
newCaches = map[CacheType]func(conf *config) Cache{
4631
standard: newStandardCache,
@@ -49,29 +34,6 @@ var (
4934
}
5035
)
5136

52-
// CacheType is the type of cache.
53-
type CacheType string
54-
55-
// String returns the cache type in string form.
56-
func (ct CacheType) String() string {
57-
return string(ct)
58-
}
59-
60-
// IsStandard returns if cache type is standard.
61-
func (ct CacheType) IsStandard() bool {
62-
return ct == standard
63-
}
64-
65-
// IsLRU returns if cache type is lru.
66-
func (ct CacheType) IsLRU() bool {
67-
return ct == lru
68-
}
69-
70-
// IsLFU returns if cache type is lfu.
71-
func (ct CacheType) IsLFU() bool {
72-
return ct == lfu
73-
}
74-
7537
// Cache is the core interface of cachego.
7638
// We provide some implements including standard cache and sharding cache.
7739
type Cache interface {
@@ -101,38 +63,10 @@ type Cache interface {
10163
// Reset resets cache to initial status which is like a new cache.
10264
Reset()
10365

104-
// Loader loads a value to cache.
105-
// See Loader interface.
106-
Loader
107-
}
108-
109-
type cache struct {
110-
*config
111-
Loader
112-
113-
lock sync.RWMutex
114-
}
115-
116-
func (c *cache) setup(conf *config, cache Cache) {
117-
c.config = conf
118-
c.Loader = NewLoader(cache, conf.singleflight)
119-
}
120-
121-
// RunGCTask runs a gc task in a new goroutine and returns a cancel function to cancel the task.
122-
// However, you don't need to call it manually for most time, instead, use options is a better choice.
123-
// Making it a public function is for more customizations in some situations.
124-
// For example, using options to run gc task is un-cancelable, so you can use it to run gc task by your own
125-
// and get a cancel function to cancel the gc task.
126-
func RunGCTask(cache Cache, duration time.Duration) (cancel func()) {
127-
fn := func(ctx context.Context) {
128-
cache.GC()
129-
}
130-
131-
ctx := context.Background()
132-
ctx, cancel = context.WithCancel(ctx)
133-
134-
go task.New(fn).Context(ctx).Duration(duration).Run()
135-
return cancel
66+
// Load loads a key with ttl to cache and returns an error if failed.
67+
// We recommend you use this method to load missed keys to cache,
68+
// because it may use singleflight to reduce the times calling load function.
69+
Load(key string, ttl time.Duration, load func() (value interface{}, err error)) (value interface{}, err error)
13670
}
13771

13872
func newCache(withReport bool, opts ...Option) (cache Cache, reporter *Reporter) {
@@ -180,3 +114,20 @@ func NewCache(opts ...Option) (cache Cache) {
180114
func NewCacheWithReport(opts ...Option) (cache Cache, reporter *Reporter) {
181115
return newCache(true, opts...)
182116
}
117+
118+
// RunGCTask runs a gc task in a new goroutine and returns a cancel function to cancel the task.
119+
// However, you don't need to call it manually for most time, instead, use options is a better choice.
120+
// Making it a public function is for more customizations in some situations.
121+
// For example, using options to run gc task is un-cancelable, so you can use it to run gc task by your own
122+
// and get a cancel function to cancel the gc task.
123+
func RunGCTask(cache Cache, duration time.Duration) (cancel func()) {
124+
fn := func(ctx context.Context) {
125+
cache.GC()
126+
}
127+
128+
ctx := context.Background()
129+
ctx, cancel = context.WithCancel(ctx)
130+
131+
go task.New(fn).Context(ctx).Duration(duration).Run()
132+
return cancel
133+
}

0 commit comments

Comments
 (0)