-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
141 lines (112 loc) · 3.13 KB
/
Copy pathcache.go
File metadata and controls
141 lines (112 loc) · 3.13 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package tagcache
import (
"fmt"
)
const _VERSION = "0.1.0"
func Version() string {
return _VERSION
}
var _ Cache = new(Engine)
// Cache is the interface that operates the cache data.
type CacheStore interface {
// Set Sets value into cache with key and expire time.
Set(key, val string, timeout int64) error
// MSet(items map[string]string, timeout int64) error
// Get gets cached value by given key.
Get(key string) string
// Get Multi keys
// MGet(keys []string) []string
// Delete deletes cached value by given key.
Delete(key string) error
// Incr increases cached int-type value by given key as a counter.
Incr(key string) (int64, error)
// Decr decreases cached int-type value by given key as a counter.
Decr(key string) (int64, error)
// Flush deletes all cached data.
Flush() error
// StartAndGC starts GC routine based on config string settings.
StartAndGC(opt Options) error
// update expire time
Touch(key string, expire int64) error
Info() string
}
type StoreFactory interface {
CreateStore(opt Options) (CacheStore, error)
}
type Cache interface {
CacheStore
Tags(tags []string) Cache
}
type Options struct {
// Name of adapter. Default is "memory".
Adapter string
// Adapter configuration, it's corresponding to adapter.
AdapterConfig string
// key prefix Default is ""
Section string
}
func New(opt Options) (Cache, error) {
adapter, ok := adapters[opt.Adapter]
if !ok {
return nil, fmt.Errorf("cache: unknown adapter '%s'(forgot to import?)", opt.Adapter)
}
store, err := adapter.CreateStore(opt)
if err != nil {
return nil, fmt.Errorf("adapter.CreateStore err %v", err)
}
engine := &Engine{}
engine.Opt = opt
engine.store = store
return engine, nil
}
type Engine struct {
Opt Options
store CacheStore
}
func (this *Engine) Set(key, val string, timeout int64) error {
return this.store.Set(key, val, timeout)
}
// func (this *Engine) MSet(items map[string]string, timeout int64) error {
// return this.store.MSet(items, timeout)
// }
func (this *Engine) Get(key string) string {
return this.store.Get(key)
}
// func (this *Engine) MGet(keys []string) []string {
// return this.store.MGet(keys)
// }
func (this *Engine) Delete(key string) error {
return this.store.Delete(key)
}
func (this *Engine) Incr(key string) (int64, error) {
return this.store.Incr(key)
}
func (this *Engine) Decr(key string) (int64, error) {
return this.store.Decr(key)
}
func (this *Engine) Flush() error {
return this.store.Flush()
}
func (this *Engine) StartAndGC(opt Options) error {
return this.store.StartAndGC(opt)
}
func (this *Engine) Touch(key string, expire int64) error {
return this.store.Touch(key, expire)
}
func (this *Engine) Tags(tags []string) Cache {
return NewTagCache(this.store, tags...)
}
func (this *Engine) Info() string {
return this.store.Info()
}
var adapters = make(map[string]StoreFactory)
// Register registers a adapter.
func Register(name string, adapter StoreFactory) {
if adapter == nil {
panic("cache: cannot register adapter with nil value")
}
if _, dup := adapters[name]; dup {
panic(fmt.Errorf("cache: cannot register adapter '%s' twice", name))
}
adapters[name] = adapter
}