-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbyte_cache_wrapper.go
More file actions
96 lines (78 loc) · 2.04 KB
/
byte_cache_wrapper.go
File metadata and controls
96 lines (78 loc) · 2.04 KB
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
package cache
import "time"
const (
RetryTimeout = time.Second * 10
)
type wrapperCache struct {
realCache IByteCache
stubCache IByteCache
isConnected bool
doneChan chan bool
logger IAerospikeCacheLogger
}
type fnCreate func() (IByteCache, error)
// NewEntryCacheWrapper initializes instance of IEntryCache
func NewEntryCacheWrapper(fn fnCreate, logger IAerospikeCacheLogger) IByteCache {
result := &wrapperCache{
stubCache: NewBlackholeCache(),
logger: logger,
doneChan: make(chan bool, 1),
}
go result.createRealCache(fn)
return result
}
func (this *wrapperCache) getCache() IByteCache {
if this.isConnected {
return this.realCache
}
return this.stubCache
}
func (this *wrapperCache) createRealCache(fn fnCreate) {
for {
select {
case <-this.doneChan:
close(this.doneChan)
return
case <-time.After(RetryTimeout):
if cache, err := fn(); err == nil {
this.realCache = cache
this.isConnected = true
this.logger.Debugf("Wrapped cache was created")
return
}
}
}
}
// Get returns nil, do nothing
func (this *wrapperCache) Get(key *Key) ([]byte, bool) {
return this.getCache().Get(key)
}
// Put returns nil, do nothing
func (this *wrapperCache) Put(data []byte, key *Key, ttl time.Duration) {
this.getCache().Put(data, key, ttl)
}
// ScanKeys returns nil, do nothing
func (this *wrapperCache) ScanKeys(set string) ([]Key, error) {
return this.getCache().ScanKeys(set)
}
// Remove returns nil, do nothing
func (this *wrapperCache) Remove(key *Key) error {
return this.getCache().Remove(key)
}
// Close do nothing
func (this *wrapperCache) Close() {
this.doneChan <- true
this.getCache().Close()
}
// Flush removes all entries from cache and returns number of flushed entries
func (this *wrapperCache) Flush() int {
return this.getCache().Flush()
}
// Count returns count of data in cache
func (this *wrapperCache) Count() int {
return this.getCache().Count()
}
// ClearSet returns nil, does nothing
func (this *wrapperCache) ClearSet(set string) error {
return this.getCache().ClearSet(set)
}