/
cache.go
176 lines (147 loc) · 4.2 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package manager
import (
"sync"
"time"
"github.com/micro/micro/v3/service/runtime"
)
var cacheTTL = time.Second * 10
// NewCache wraps a runtime with a cache
func NewCache(r runtime.Runtime) runtime.Runtime {
return &cache{
lastUpdated: make(map[string]time.Time),
services: make(map[string][]*runtime.Service),
mux: new(sync.RWMutex),
Runtime: r,
}
}
type cache struct {
// lastUpdated contains the last time services were read from the underlying runtime for a given
// namespace. When a service is Created/Updated/Deleted in a given namespace, the value is deleted
// from the map
lastUpdated map[string]time.Time
// services is a cache of the services in a namespace, it's reset when the lastUpdated is reset
services map[string][]*runtime.Service
// mux is a mutex to protect the lastUpdated and services
mux *sync.RWMutex
runtime.Runtime
}
// Create a service
func (c *cache) Create(resource runtime.Resource, opts ...runtime.CreateOption) error {
// parse the options
var options runtime.CreateOptions
for _, o := range opts {
o(&options)
}
if options.Namespace == "" {
options.Namespace = "micro"
}
// Assert the resource back into a *runtime.Service
srv, ok := resource.(*runtime.Service)
if !ok {
return runtime.ErrInvalidResource
}
// call the underlying runtime
if err := c.Runtime.Create(srv, opts...); err != nil {
return err
}
// the service was written so reset the cache for the namespace
c.mux.Lock()
delete(c.lastUpdated, options.Namespace)
delete(c.services, options.Namespace)
c.mux.Unlock()
return nil
}
// Read returns the service
func (c *cache) Read(opts ...runtime.ReadOption) ([]*runtime.Service, error) {
// parse the options
var options runtime.ReadOptions
for _, o := range opts {
o(&options)
}
if options.Namespace == "" {
options.Namespace = "micro"
}
// if a query was used we can't use the cache
if len(options.Service) > 0 || len(options.Type) > 0 || len(options.Version) > 0 {
return c.Runtime.Read(opts...)
}
// check to see if the cache is valid
c.mux.RLock()
if t, ok := c.lastUpdated[options.Namespace]; ok && cacheIsValid(t) {
c.mux.RUnlock()
return c.services[options.Namespace], nil
}
c.mux.RUnlock()
// the cache was not valid, read from the runtime
c.mux.Lock()
defer c.mux.Unlock()
srvs, err := c.Runtime.Read(opts...)
if err != nil {
// if there was an error, don't update the cache
return nil, err
}
c.lastUpdated[options.Namespace] = time.Now()
c.services[options.Namespace] = srvs
return srvs, nil
}
// Update the service in place
func (c *cache) Update(resource runtime.Resource, opts ...runtime.UpdateOption) error {
// parse the options
var options runtime.UpdateOptions
for _, o := range opts {
o(&options)
}
if options.Namespace == "" {
options.Namespace = "micro"
}
// Assert the resource back into a *runtime.Service
srv, ok := resource.(*runtime.Service)
if !ok {
return runtime.ErrInvalidResource
}
// call the underlying runtime
if err := c.Runtime.Update(srv, opts...); err != nil {
return err
}
// the service was updated so reset the cache for the namespace
c.mux.Lock()
delete(c.lastUpdated, options.Namespace)
delete(c.services, options.Namespace)
c.mux.Unlock()
return nil
}
// Remove a service
func (c *cache) Delete(resource runtime.Resource, opts ...runtime.DeleteOption) error {
// parse the options
var options runtime.DeleteOptions
for _, o := range opts {
o(&options)
}
if options.Namespace == "" {
options.Namespace = "micro"
}
// Assert the resource back into a *runtime.Service
srv, ok := resource.(*runtime.Service)
if !ok {
return runtime.ErrInvalidResource
}
// call the underlying runtime
if err := c.Runtime.Delete(srv, opts...); err != nil {
return err
}
// the service was deleted so reset the cache for the namespace
c.mux.Lock()
delete(c.lastUpdated, options.Namespace)
delete(c.services, options.Namespace)
c.mux.Unlock()
return nil
}
// String defines the runtime implementation
func (c *cache) String() string {
return "cache"
}
// cacheIsValid returns a boolean indicating if a cache initialized at the time provided is still
// valid now
func cacheIsValid(t time.Time) bool {
return t.After(time.Now().Add(-cacheTTL))
}