@@ -16,7 +16,6 @@ package cachego
1616
1717import (
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-
4429var (
4530 newCaches = map [CacheType ]func (conf * config ) Cache {
4631 standard : newStandardCache ,
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.
7739type 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
13872func newCache (withReport bool , opts ... Option ) (cache Cache , reporter * Reporter ) {
@@ -180,3 +114,20 @@ func NewCache(opts ...Option) (cache Cache) {
180114func 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