-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsache.go
39 lines (34 loc) · 787 Bytes
/
sache.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
package sache
import "github.com/satheesh1997/sache/policies"
type (
Sache struct {
EvictionPolicy policies.LRUEvictionPolicy
Storage HashMap
}
)
func (cache *Sache) Put(key string, value string) {
if cache.Storage.IsFull() {
keyToRemove := cache.EvictionPolicy.EvictKey()
cache.Storage.Remove(keyToRemove)
}
cache.EvictionPolicy.KeyAccessed(key)
cache.Storage.Insert(key, value)
}
func (cache *Sache) Get(key string) string {
value := cache.Storage.Read(key)
if value != "" {
cache.EvictionPolicy.KeyAccessed(key)
}
return value
}
func New(storageSize int) Sache {
cache := Sache{
EvictionPolicy: policies.NewLRUEvictionPolicy(),
Storage: HashMap{
Data: make(map[string]string),
Size: 0,
MaxSize: storageSize,
},
}
return cache
}