This repository has been archived by the owner on Mar 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgotcha.go
168 lines (149 loc) · 3.78 KB
/
gotcha.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package gotcha
import (
"sync"
"time"
"github.com/bxcodec/gotcha/cache"
"github.com/bxcodec/gotcha/internal"
"github.com/bxcodec/gotcha/internal/lfu"
"github.com/bxcodec/gotcha/internal/lru"
)
var (
// DefaultCache use for default cache client
DefaultCache = New()
)
// New will create a new cache client. If the options not set, the cache will use the default options
func New(options ...*cache.Option) (c cache.Cache) {
option := mergeOptions(options...)
if option.MaxSizeItem == 0 {
// Use default
option.MaxSizeItem = cache.DefaultSize
}
if option.AlgorithmType == "" {
// Use LRU Default
option.AlgorithmType = cache.LRUAlgorithm
}
if option.ExpiryTime == 0 {
// Use default expiry time
option.ExpiryTime = cache.DefaultExpiryTime
}
c = &Cache{
repo: NewRepository(*option),
mutex: &sync.RWMutex{},
}
return
}
// NewOption return an empty option
func NewOption() (op *cache.Option) {
return &cache.Option{}
}
func mergeOptions(options ...*cache.Option) (opts *cache.Option) {
opts = new(cache.Option)
for _, op := range options {
if op.AlgorithmType != "" {
opts.AlgorithmType = op.AlgorithmType
}
if op.ExpiryTime != 0 {
opts.ExpiryTime = op.ExpiryTime
}
if op.MaxMemory != 0 {
opts.MaxMemory = op.MaxMemory
}
if op.MaxSizeItem != 0 {
opts.MaxSizeItem = op.MaxSizeItem
}
}
return
}
// Set will set an item to cache using default option
func Set(key string, value interface{}) (err error) {
return DefaultCache.Set(key, value)
}
// Get will get an item from cache using default option
func Get(key string) (value interface{}, err error) {
return DefaultCache.Get(key)
}
// Delete will delete an item from the cache using default option
func Delete(key string) (err error) {
return DefaultCache.Delete(key)
}
// GetKeys will get all keys from the cache using default option
func GetKeys() (keys []string, err error) {
return DefaultCache.GetKeys()
}
// ClearCache will Clear the cache using default option
func ClearCache() (err error) {
return DefaultCache.ClearCache()
}
// NewRepository return the implementations of repository cache
func NewRepository(option cache.Option) internal.Repository {
var repo internal.Repository
switch option.AlgorithmType {
case cache.LRUAlgorithm:
repo = lru.New(option.MaxSizeItem, option.MaxMemory, option.ExpiryTime)
case cache.LFUAlgorithm:
repo = lfu.New(option.MaxSizeItem, option.MaxMemory, option.ExpiryTime)
}
return repo
}
// Cache represent the Cache handler
type Cache struct {
mutex *sync.RWMutex
repo internal.Repository
}
// Set used for setting the item to cache
// TODO: (bxcodec)
// Add Test for this function
func (c *Cache) Set(key string, value interface{}) (err error) {
document := &cache.Document{
Key: key,
Value: value,
StoredTime: time.Now().Unix(),
}
c.mutex.Lock()
defer c.mutex.Unlock()
err = c.repo.Set(document)
return
}
// Get will retrieve the item from cache
// TODO: (bxcodec)
// Add Test for this function
func (c *Cache) Get(key string) (value interface{}, err error) {
c.mutex.RLock()
doc, err := c.repo.Get(key)
c.mutex.RUnlock()
if err != nil {
return
}
value = doc.Value
return
}
// Delete will remove the item from cache
// TODO: (bxcodec)
// Add Test for this function
func (c *Cache) Delete(key string) (err error) {
c.mutex.Lock()
_, err = c.repo.Delete(key)
c.mutex.Unlock()
if err != nil {
return
}
return
}
// GetKeys will retrieve all keys from cache
// TODO: (bxcodec)
// Add Test for this function
func (c *Cache) GetKeys() (keys []string, err error) {
c.mutex.RLock()
keys, err = c.repo.Keys()
c.mutex.RUnlock()
return keys, err
}
// ClearCache will cleanup all the cache
// TODO: (bxcodec)
// Add Test for this function
func (c *Cache) ClearCache() (err error) {
c.mutex.Lock()
err = c.repo.Clear()
c.mutex.Unlock()
return
}