@@ -11,11 +11,10 @@ import (
11
11
)
12
12
13
13
const (
14
- nsKey = "ns-map"
15
- svcKeyPrefix = "svc-map"
16
- endptsKeyPrefix = "endpts"
14
+ nsKey = "ns-map"
15
+ svcKeyPrefix = "svc-map"
17
16
18
- defaultCacheSize = 1024
17
+ defaultCacheSize = 2048
19
18
defaultNsTTL = 10 * time .Second
20
19
defaultSvcTTL = 10 * time .Second
21
20
defaultEndptTTL = 5 * time .Second
@@ -34,9 +33,10 @@ type ServiceDiscoveryClientCache interface {
34
33
}
35
34
36
35
type sdCache struct {
37
- log common.Logger
38
- cache * cache.LRUExpireCache
39
- config * SdCacheConfig
36
+ log common.Logger
37
+ defaultCache * cache.LRUExpireCache
38
+ endpointsCache * cache.LRUExpireCache
39
+ config * SdCacheConfig
40
40
}
41
41
42
42
type SdCacheConfig struct {
@@ -47,9 +47,10 @@ type SdCacheConfig struct {
47
47
48
48
func NewServiceDiscoveryClientCache (cacheConfig * SdCacheConfig ) ServiceDiscoveryClientCache {
49
49
return & sdCache {
50
- log : common .NewLogger ("cloudmap" ),
51
- cache : cache .NewLRUExpireCache (defaultCacheSize ),
52
- config : cacheConfig ,
50
+ log : common .NewLogger ("cloudmap" ),
51
+ defaultCache : cache .NewLRUExpireCache (defaultCacheSize ),
52
+ endpointsCache : cache .NewLRUExpireCache (defaultCacheSize ),
53
+ config : cacheConfig ,
53
54
}
54
55
}
55
56
@@ -63,41 +64,41 @@ func NewDefaultServiceDiscoveryClientCache() ServiceDiscoveryClientCache {
63
64
}
64
65
65
66
func (sdCache * sdCache ) GetNamespaceMap () (namespaceMap map [string ]* model.Namespace , found bool ) {
66
- entry , exists := sdCache .cache .Get (nsKey )
67
+ entry , exists := sdCache .defaultCache .Get (nsKey )
67
68
if ! exists {
68
69
return nil , false
69
70
}
70
71
71
72
namespaceMap , ok := entry .(map [string ]* model.Namespace )
72
73
if ! ok {
73
74
sdCache .log .Error (errors .New ("failed to retrieve namespaceMap from cache" ), "" )
74
- sdCache .cache .Remove (nsKey )
75
+ sdCache .defaultCache .Remove (nsKey )
75
76
return nil , false
76
77
}
77
78
78
79
return namespaceMap , true
79
80
}
80
81
81
82
func (sdCache * sdCache ) CacheNamespaceMap (namespaces map [string ]* model.Namespace ) {
82
- sdCache .cache .Add (nsKey , namespaces , sdCache .config .NsTTL )
83
+ sdCache .defaultCache .Add (nsKey , namespaces , sdCache .config .NsTTL )
83
84
}
84
85
85
86
func (sdCache * sdCache ) EvictNamespaceMap () {
86
- sdCache .cache .Remove (nsKey )
87
+ sdCache .defaultCache .Remove (nsKey )
87
88
}
88
89
89
90
func (sdCache * sdCache ) GetServiceIdMap (nsName string ) (serviceIdMap map [string ]string , found bool ) {
90
91
key := sdCache .buildSvcKey (nsName )
91
- entry , exists := sdCache .cache .Get (key )
92
+ entry , exists := sdCache .defaultCache .Get (key )
92
93
if ! exists {
93
94
return nil , false
94
95
}
95
96
96
97
serviceIdMap , ok := entry .(map [string ]string )
97
98
if ! ok {
98
- sdCache . log . Error ( errors . New ( "failed to retrieve service IDs from cache" ), "" ,
99
- "nsName " , nsName )
100
- sdCache .cache .Remove (key )
99
+ err := fmt . Errorf ( "failed to retrieve service IDs from cache" )
100
+ sdCache . log . Error ( err , err . Error (), "namespace " , nsName )
101
+ sdCache .defaultCache .Remove (key )
101
102
return nil , false
102
103
}
103
104
@@ -106,26 +107,26 @@ func (sdCache *sdCache) GetServiceIdMap(nsName string) (serviceIdMap map[string]
106
107
107
108
func (sdCache * sdCache ) CacheServiceIdMap (nsName string , serviceIdMap map [string ]string ) {
108
109
key := sdCache .buildSvcKey (nsName )
109
- sdCache .cache .Add (key , serviceIdMap , sdCache .config .SvcTTL )
110
+ sdCache .defaultCache .Add (key , serviceIdMap , sdCache .config .SvcTTL )
110
111
}
111
112
112
113
func (sdCache * sdCache ) EvictServiceIdMap (nsName string ) {
113
114
key := sdCache .buildSvcKey (nsName )
114
- sdCache .cache .Remove (key )
115
+ sdCache .defaultCache .Remove (key )
115
116
}
116
117
117
118
func (sdCache * sdCache ) GetEndpoints (nsName string , svcName string ) (endpts []* model.Endpoint , found bool ) {
118
119
key := sdCache .buildEndptsKey (nsName , svcName )
119
- entry , exists := sdCache .cache .Get (key )
120
+ entry , exists := sdCache .endpointsCache .Get (key )
120
121
if ! exists {
121
122
return nil , false
122
123
}
123
124
124
125
endpts , ok := entry .([]* model.Endpoint )
125
126
if ! ok {
126
- sdCache . log . Error ( errors . New ( "failed to retrieve endpoints from cache" ), "" ,
127
- "ns " , " nsName" , "svc " , svcName )
128
- sdCache .cache .Remove (key )
127
+ err := fmt . Errorf ( "failed to retrieve endpoints from cache" )
128
+ sdCache . log . Error ( err , err . Error (), "namespace " , nsName , "service " , svcName )
129
+ sdCache .endpointsCache .Remove (key )
129
130
return nil , false
130
131
}
131
132
@@ -134,18 +135,18 @@ func (sdCache *sdCache) GetEndpoints(nsName string, svcName string) (endpts []*m
134
135
135
136
func (sdCache * sdCache ) CacheEndpoints (nsName string , svcName string , endpts []* model.Endpoint ) {
136
137
key := sdCache .buildEndptsKey (nsName , svcName )
137
- sdCache .cache .Add (key , endpts , sdCache .config .EndptTTL )
138
+ sdCache .endpointsCache .Add (key , endpts , sdCache .config .EndptTTL )
138
139
}
139
140
140
141
func (sdCache * sdCache ) EvictEndpoints (nsName string , svcName string ) {
141
142
key := sdCache .buildEndptsKey (nsName , svcName )
142
- sdCache .cache .Remove (key )
143
+ sdCache .endpointsCache .Remove (key )
143
144
}
144
145
145
146
func (sdCache * sdCache ) buildSvcKey (nsName string ) (cacheKey string ) {
146
147
return fmt .Sprintf ("%s:%s" , svcKeyPrefix , nsName )
147
148
}
148
149
149
150
func (sdCache * sdCache ) buildEndptsKey (nsName string , svcName string ) string {
150
- return fmt .Sprintf ("%s:%s:%s" , endptsKeyPrefix , nsName , svcName )
151
+ return fmt .Sprintf ("%s:%s" , nsName , svcName )
151
152
}
0 commit comments