-
Notifications
You must be signed in to change notification settings - Fork 13
/
cache_impl.go
133 lines (121 loc) · 3.35 KB
/
cache_impl.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
133
// Copyright (c) 2018 CA. All rights reserved.
//
// 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 main
import (
"fmt"
)
// N.B.: this is the cache implementation whose functions are
// not intended to be called directly.
// The intended cache interface
// is in cache_access.go: namely "Lookup", "Add", "Remove",
// along with the key-building functions.
// All access to the cache is intended to be via the server mailbox.
// That is our serialization mechanism (akin to an erlang gen_server).
//
// (Deliberately resisting the "internal" package goo...)
var cache map[string]interface{}
func runServer(mbox <-chan *CacheRequest) {
for {
req, ok := <-mbox
if ok {
req.replyChan <- req.operation()
} else {
break
}
}
}
func initCache() {
cache = make(map[string]interface{})
serverMbox := make(chan *CacheRequest)
go runServer(serverMbox)
initCacheClient(serverMbox)
}
func findInList(clist []*JsonObject, target *JsonObject) int {
tname := getName(*target)
tns := getNamespace(*target)
tkind := getKind(*target)
for idx, obj := range clist {
name := getName(*obj)
ns := getNamespace(*obj)
kind := getKind(*obj)
if tname == name && tns == ns && tkind == kind {
return idx
}
}
return -1
}
func deleteFromCacheList(key string, obj *JsonObject) {
if val, ok := cache[key]; ok {
clist := val.([]*JsonObject)
idx := findInList(clist, obj)
if idx > -1 {
cache[key] = append(clist[:idx], clist[idx+1:]...)
}
}
}
func addToCacheList(key string, obj *JsonObject) {
if val, ok := cache[key]; ok {
clist := val.([]*JsonObject)
idx := findInList(clist, obj)
if idx == -1 {
cache[key] = append(clist, obj)
} else {
clist[idx] = obj
}
} else {
cache[key] = []*JsonObject{obj}
}
}
func formattedName(obj *JsonObject) string {
return cacheKey(getKind(*obj), getNamespace(*obj), getName(*obj))
}
func addToCache(obj *JsonObject) {
kind := getKind(*obj)
ns := getNamespace(*obj)
name := getName(*obj)
cacheKey := cacheKey(kind, ns, name)
nsCacheKey := nsCacheKey(kind, ns)
cache[cacheKey] = obj
addToCacheList(nsCacheKey, obj)
addToCacheList(kind, obj)
}
func removeFromCache(obj *JsonObject) {
kind := getKind(*obj)
ns := getNamespace(*obj)
name := getName(*obj)
cacheKey := cacheKey(kind, ns, name)
nsCacheKey := nsCacheKey(kind, ns)
delete(cache, cacheKey)
deleteFromCacheList(nsCacheKey, obj)
deleteFromCacheList(kind, obj)
}
func cacheLookup(key string) interface{} {
if val, ok := cache[key]; ok {
if ref, ok := val.(*JsonObject); ok {
return *ref
} else if list, ok := val.([]*JsonObject); ok {
// clone returned slice so that its "shape" can't be changed
// while caller is holding it...
retlist := make([]JsonObject, len(list))
for idx, item := range list {
retlist[idx] = *item
}
return retlist
} else {
panic(fmt.Sprintf("invalid type in cache: %T", val))
}
} else {
return nil
}
}