/
cache.go
109 lines (96 loc) · 2.54 KB
/
cache.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
/*
* Copyright 2018-2019, CS Systemes d'Information, http://www.c-s.fr
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package utils
import (
"sync"
)
//go:generate mockgen -destination=../mocks/mock_cache.go -package=mocks github.com/CS-SI/SafeScale/lib/utils Cache
// Cache is an interface for caching elements
type Cache interface {
SetBy(string, func() (interface{}, error)) error
Set(string, interface{}) error
ForceSetBy(string, func() (interface{}, error)) error
ForceSet(string, interface{}) error
Reset(string) Cache
Get(string) (interface{}, bool)
GetOrDefault(string, interface{}) interface{}
}
// MapCache implements Cache interface using map
type MapCache struct {
lock sync.RWMutex
cache map[string]interface{}
}
// NewMapCache ...
func NewMapCache() Cache {
return &MapCache{
cache: map[string]interface{}{},
}
}
// SetBy ...
func (c *MapCache) SetBy(key string, by func() (interface{}, error)) error {
c.lock.Lock()
if _, ok := c.cache[key]; !ok {
value, err := by()
if err != nil {
return err
}
c.cache[key] = value
}
c.lock.Unlock()
return nil
}
// ForceSetBy ...
func (c *MapCache) ForceSetBy(key string, by func() (interface{}, error)) error {
c.lock.Lock()
value, err := by()
if err != nil {
return err
}
c.cache[key] = value
c.lock.Unlock()
return nil
}
// Set ...
func (c *MapCache) Set(key string, value interface{}) error {
return c.SetBy(key, func() (interface{}, error) { return value, nil })
}
// ForceSet ...
func (c *MapCache) ForceSet(key string, value interface{}) error {
return c.ForceSetBy(key, func() (interface{}, error) { return value, nil })
}
// Reset ...
func (c *MapCache) Reset(key string) Cache {
c.lock.Lock()
delete(c.cache, key)
c.lock.Unlock()
return c
}
// Get ...
func (c *MapCache) Get(key string) (value interface{}, ok bool) {
c.lock.RLock()
value, ok = c.cache[key]
c.lock.RUnlock()
return
}
// GetOrDefault ...
func (c *MapCache) GetOrDefault(key string, def interface{}) (value interface{}) {
var ok bool
value, ok = c.Get(key)
if !ok {
value = def
}
return
}