Passed
Pull Request — main (#71)
by Yume
01:15
created

cache.*cacheItem.Size   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
package cache
2
3
import "C"
4
import (
5
	"fmt"
6
	"github.com/memnix/memnixrest/pkg/models"
7
	"github.com/robfig/cron/v3"
8
	"sync"
9
)
10
11
type Cache struct {
12
	// Cache map
13
	c    map[uint]cacheItem
14
	mu   sync.RWMutex
15
	cron *cron.Cron
16
}
17
18
type cacheItem struct {
19
	Object map[uint]models.MemDate
20
}
21
22
func (c *Cache) Flush() {
23
	c.mu.Lock()
24
	defer c.mu.Unlock()
25
	c.c = make(map[uint]cacheItem)
26
}
27
28
func (c *Cache) Set(key uint, value map[uint]models.MemDate) {
29
	c.mu.Lock()
30
	defer c.mu.Unlock()
31
	c.c[key] = cacheItem{
32
		Object: value,
33
	}
34
}
35
36
func (c *Cache) SetSlice(key uint, value []models.MemDate) {
37
	c.mu.Lock()
38
	defer c.mu.Unlock()
39
	c.c[key] = cacheItem{
40
		Object: make(map[uint]models.MemDate, len(value)),
41
	}
42
	for _, v := range value {
43
		c.c[key].Object[v.ID] = v
44
	}
45
}
46
47
func (c *Cache) AppendSlice(key uint, value []models.MemDate) {
48
	c.mu.Lock()
49
	defer c.mu.Unlock()
50
	_, found := c.c[key]
51
	if !found {
52
		c.c[key] = cacheItem{
53
			Object: make(map[uint]models.MemDate, len(value)),
54
		}
55
	}
56
	for _, v := range value {
57
		c.c[key].Object[v.ID] = v
58
	}
59
}
60
61
// Items in cache
62
func (c *Cache) Items(key uint) []models.MemDate {
63
	c.mu.RLock()
64
	defer c.mu.RUnlock()
65
	item, found := c.c[key]
66
	if !found {
67
		return nil
68
	}
69
	memDates := make([]models.MemDate, 0, item.Size())
70
71
	for _, v := range item.Object {
72
		memDates = append(memDates, v)
73
	}
74
	return memDates
75
}
76
77
func (c *Cache) Replace(key uint, item models.MemDate) {
78
	c.mu.Lock()
79
	defer c.mu.Unlock()
80
	_, found := c.c[key]
81
	if !found {
82
		return
83
	}
84
85
	c.c[key].Object[item.ID] = item
86
}
87
88
func (c *Cache) Exists(key uint) bool {
89
	c.mu.RLock()
90
	defer c.mu.RUnlock()
91
	_, found := c.c[key]
92
	return found
93
}
94
95
func (c *Cache) Get(key uint) (map[uint]models.MemDate, error) {
96
	c.mu.RLock()
97
	defer c.mu.RUnlock()
98
	item, found := c.c[key]
99
	if !found {
100
		return nil, fmt.Errorf("key not found")
101
	}
102
	return item.Object, nil
103
}
104
105
func (c *Cache) Delete(key uint) error {
106
	c.mu.Lock()
107
	defer c.mu.Unlock()
108
	_, found := c.c[key]
109
	if !found {
110
		return fmt.Errorf("key not found")
111
	}
112
	delete(c.c, key)
113
	return nil
114
}
115
116
func (c *Cache) Size() int {
117
	return len(c.c)
118
}
119
120
func (cacheItem *cacheItem) Size() int {
121
	return len(cacheItem.Object)
122
}
123
124
func (c *Cache) DeleteItem(key uint, item uint) error {
125
	c.mu.Lock()
126
	defer c.mu.Unlock()
127
	_, found := c.c[key]
128
	if !found {
129
		return fmt.Errorf("key not found")
130
	}
131
132
	_, found = c.c[key].Object[item]
133
	if !found {
134
		return fmt.Errorf("item not found")
135
	}
136
137
	delete(c.c[key].Object, item)
138
	return nil
139
}
140
141
func runCron(c *Cache) {
142
	_, _ = c.cron.AddFunc("@daily", func() {
143
		c.Flush()
144
	})
145
	c.cron.Start()
146
}
147
148
func NewCache() *Cache {
149
	cache := &Cache{
150
		c:    make(map[uint]cacheItem),
151
		cron: cron.New(),
152
	}
153
154
	runCron(cache)
155
156
	return cache
157
}
158