-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcache.go
79 lines (68 loc) · 2.29 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
package carrot
import (
"errors"
"time"
)
type CachedControllersList struct {
cachedControllers map[string]*AppController
lru *PriorityQueue
}
// NewCachedControllersList initializes a new instance of the CachedControllersList struct.
func NewCachedControllersList() *CachedControllersList {
return &CachedControllersList{
cachedControllers: make(map[string]*AppController),
lru: NewPriorityQueue(),
}
}
// Exists checks whether a controller of the given type is already being cached.
func (ccl *CachedControllersList) Exists(key string) bool {
_, ok := ccl.cachedControllers[key]
if ok {
return true
}
return false
}
// Get returns a reference to the controller whose type matches the key.
func (ccl *CachedControllersList) Get(key string) (*AppController, error) {
var err error
cc, ok := ccl.cachedControllers[key]
if !ok || cc == nil {
err = errors.New("cached controller does not exist")
}
//update priority to reflect recent controller usage
ccl.lru.UpdatePriority(key, getPriority())
return cc, err
}
// Add maintains the existence of a new controller so that it does not have to be created again upon request reference.
func (ccl *CachedControllersList) Add(key string, ac *AppController) {
//add to controller map
ccl.cachedControllers[key] = ac
//add to LRU to keep track of deletion order
ccl.lru.Insert(key, getPriority())
}
// DeleteOldest frees the memory of the oldest controller maintained by the cache.
func (ccl *CachedControllersList) DeleteOldest() error {
//find oldest controller in LRU to identify token and delete LRU record
key, err := ccl.lru.Pop()
if err != nil {
return err
}
//use token to delete from controller map
delete(ccl.cachedControllers, key.(string)) //doesn't return anything
return nil
}
// IsEmpty determines whether the cache is currently maintaining any controllers.
func (ccl *CachedControllersList) IsEmpty() bool {
if len(ccl.cachedControllers) == 0 {
return true
}
return false
}
// Length returns the size of the cache: the number of maintained controllers.
func (ccl *CachedControllersList) Length() int {
return len(ccl.cachedControllers)
}
// getPriority returns the current Unix time in order to record controller age for later deletion.
func getPriority() float64 {
return float64(time.Now().Unix())
}