-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gcache_cache.go
88 lines (76 loc) · 2.4 KB
/
gcache_cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/dxasu/gf.
package gcache
import (
"context"
"time"
"github.com/dxasu/gf/container/gvar"
"github.com/dxasu/gf/os/gtimer"
"github.com/dxasu/gf/util/gconv"
)
// Cache struct.
type Cache struct {
adapter Adapter // Adapter for cache features.
ctx context.Context // Context for operations.
}
// New creates and returns a new cache object using default memory adapter.
// Note that the LRU feature is only available using memory adapter.
func New(lruCap ...int) *Cache {
memAdapter := newAdapterMemory(lruCap...)
c := &Cache{
adapter: memAdapter,
}
// Here may be a "timer leak" if adapter is manually changed from memory adapter.
// Do not worry about this, as adapter is less changed and it dose nothing if it's not used.
gtimer.AddSingleton(time.Second, memAdapter.syncEventAndClearExpired)
return c
}
// Clone returns a shallow copy of current object.
func (c *Cache) Clone() *Cache {
return &Cache{
adapter: c.adapter,
ctx: c.ctx,
}
}
// Ctx is a chaining function, which shallowly clones current object and sets the context
// for next operation.
func (c *Cache) Ctx(ctx context.Context) *Cache {
newCache := c.Clone()
newCache.ctx = ctx
return newCache
}
// SetAdapter changes the adapter for this cache.
// Be very note that, this setting function is not concurrent-safe, which means you should not call
// this setting function concurrently in multiple goroutines.
func (c *Cache) SetAdapter(adapter Adapter) {
c.adapter = adapter
}
// GetVar retrieves and returns the value of <key> as gvar.Var.
func (c *Cache) GetVar(key interface{}) (*gvar.Var, error) {
v, err := c.Get(key)
return gvar.New(v), err
}
// Removes deletes <keys> in the cache.
// Deprecated, use Remove instead.
func (c *Cache) Removes(keys []interface{}) error {
_, err := c.Remove(keys...)
return err
}
// KeyStrings returns all keys in the cache as string slice.
func (c *Cache) KeyStrings() ([]string, error) {
keys, err := c.Keys()
if err != nil {
return nil, err
}
return gconv.Strings(keys), nil
}
// KeyStrings returns all keys in the cache as string slice.
func (c *Cache) getCtx() context.Context {
if c.ctx == nil {
return context.Background()
}
return c.ctx
}