-
Notifications
You must be signed in to change notification settings - Fork 0
/
memCache.go
132 lines (110 loc) · 2.6 KB
/
memCache.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
package simplegocache
import (
"time"
"github.com/pkg/errors"
)
// MemCache - memory cache object, implements Cache interface
type MemCache struct {
TTL int64
MaxElements int
MaxElementSize int
Entries map[string]Entry
}
// Entry - A cache entry
type Entry struct {
Content []byte
Created int64
}
// NewMemCache - Initiates a new memory based cache
func NewMemCache(ttl int64, maxElements int, maxElementSize int) *MemCache {
return &MemCache{
TTL: ttl,
MaxElements: maxElements,
MaxElementSize: maxElementSize,
Entries: make(map[string]Entry, maxElements),
}
}
// AddToCache - Add []byte to cache with key
func (c *MemCache) AddToCache(content []byte, key string) error {
if c.InCache(key) {
return errors.New(ErrorAlreadyExists)
}
if c.tooLarge(&content) {
return errors.New(ErrorTooLarge)
}
if c.tooLong() {
return errors.New(ErrorTooManyEntries)
}
c.Entries[key] = Entry{
Content: content,
Created: time.Now().Unix(),
}
return nil
}
// ReadFromCache - Read the value of a given key from cache
func (c *MemCache) ReadFromCache(key string) ([]byte, error) {
if c.InCache(key) {
return c.Entries[key].Content, nil
}
return nil, errors.New(ErrorNoEntry)
}
// InCache - Check if a key is registered in cache
func (c *MemCache) InCache(key string) bool {
if _, ok := c.Entries[key]; ok {
if c.expired(key) {
c.DeleteFromCache(key)
return false
}
return true
}
return false
}
// DeleteFromCache - Delete an object from cache
func (c *MemCache) DeleteFromCache(key string) {
delete(c.Entries, key)
}
// UpdateCache - Update the contents of a key already in cache
func (c *MemCache) UpdateCache(content []byte, key string) error {
if c.tooLarge(&content) {
return errors.New(ErrorTooLarge)
}
if c.tooLong() {
return errors.New(ErrorTooManyEntries)
}
c.Entries[key] = Entry{
Content: content,
Created: time.Now().Unix(),
}
return nil
}
// Prune - prunes items that have expired
func (c *MemCache) Prune() {
for k := range c.Entries {
if c.expired(k) {
c.DeleteFromCache(k)
}
}
}
// Close - Remove cached files
func (c *MemCache) Close() {
for k := range c.Entries {
c.DeleteFromCache(k)
}
}
// expired - checks if a cache entry has expired
func (c *MemCache) expired(key string) bool {
if c.Entries[key].Created+c.TTL < time.Now().Unix() {
return true
}
return false
}
func (c *MemCache) tooLarge(content *[]byte) bool {
return len(*content) > c.MaxElementSize
}
func (c *MemCache) tooLong() bool {
if len(c.Entries) >= c.MaxElements {
c.Prune()
return len(c.Entries) >= c.MaxElements
}
return false
}