-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcache.go
More file actions
140 lines (124 loc) · 3.43 KB
/
cache.go
File metadata and controls
140 lines (124 loc) · 3.43 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
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
package main
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"strings"
"time"
"github.com/bradfitz/gomemcache/memcache"
)
type Cache interface {
Get(context.Context, *JSONRPCRequest) (*JSONRPCResponse, bool)
Set(context.Context, *JSONRPCRequest, *JSONRPCResponse)
}
var _ Cache = (*NoopCache)(nil)
type NoopCache struct{}
func (n NoopCache) Get(context.Context, *JSONRPCRequest) (*JSONRPCResponse, bool) { return nil, false }
func (n NoopCache) Set(context.Context, *JSONRPCRequest, *JSONRPCResponse) {}
// MemcachedCache provides caching functionality using memcached
type MemcachedCache struct {
client *memcache.Client
cacheables map[string]MethodCache
}
// NewMemcachedCache creates a new cache instance
func NewMemcachedCache(cacheables map[string]MethodCache, memcachedServers ...string) *MemcachedCache {
c := &MemcachedCache{
client: memcache.New(memcachedServers...),
cacheables: cacheables,
}
c.client.Timeout = 100 * time.Millisecond
c.client.MaxIdleConns = 10
return c
}
// IsCacheable checks if a method is cacheable and returns its config
func (c *MemcachedCache) IsCacheable(method string) (MethodCache, bool) {
cfg, ok := c.cacheables[method]
return cfg, ok
}
// buildKey generates a cache key from the request
func (c *MemcachedCache) buildKey(req *JSONRPCRequest) string {
h := sha256.New()
h.Write([]byte(req.Method + ":"))
if len(req.Params) > 0 {
var params interface{}
if json.Unmarshal(req.Params, ¶ms) == nil {
normalized, _ := json.Marshal(params)
h.Write(normalized)
} else {
h.Write(req.Params)
}
}
return "waterway:" + hex.EncodeToString(h.Sum(nil))[:32]
}
// Get retrieves a cached response
func (c *MemcachedCache) Get(_ context.Context, req *JSONRPCRequest) (*JSONRPCResponse, bool) {
cfg, ok := c.IsCacheable(req.Method)
if !ok {
return nil, false
}
item, err := c.client.Get(c.buildKey(req))
if err != nil {
return nil, false
}
var cached JSONRPCResponse
if json.Unmarshal(item.Value, &cached) != nil {
return nil, false
}
if !cfg.KeyIncludesID {
cached.ID = req.ID
}
return &cached, true
}
// Set stores a response in the cache
func (c *MemcachedCache) Set(_ context.Context, req *JSONRPCRequest, resp *JSONRPCResponse) {
if resp.Error != nil {
return
}
cfg, ok := c.IsCacheable(req.Method)
if !ok {
return
}
ttl := cfg.TTL
if c.hasVolatileBlockTag(req) {
ttl = min(ttl, 1*time.Second)
}
toCache := *resp
if !cfg.KeyIncludesID {
toCache.ID = nil
}
data, _ := json.Marshal(toCache)
if err := c.client.Set(&memcache.Item{
Key: c.buildKey(req),
Value: data,
Expiration: int32(ttl.Seconds()),
}); err != nil {
logger.Debug("cache set failed", "method", req.Method, "error", err)
}
}
// hasVolatileBlockTag checks if the request contains volatile block tags
func (c *MemcachedCache) hasVolatileBlockTag(req *JSONRPCRequest) bool {
if len(req.Params) == 0 {
return false
}
p := strings.ToLower(string(req.Params))
volatileTags := []string{`"latest"`, `"pending"`, `"earliest"`, `"safe"`, `"finalized"`}
for _, tag := range volatileTags {
if strings.Contains(p, tag) {
return true
}
}
return false
}
// Ping checks if the cache is healthy
func (c *MemcachedCache) Ping() error {
return c.client.Set(&memcache.Item{
Key: "waterway:ping",
Value: []byte("pong"),
Expiration: 1,
})
}
// Flush clears all cache entries (if supported)
func (c *MemcachedCache) Flush() error {
return c.client.FlushAll()
}