Debian Codegraph @

cache/default_cache.go

raw
1 package cache
2
3 import "sync"
4
5 // DefaultCache is the default cache implementation using sync.Map for thread-safe concurrent access.
6 type DefaultCache struct {
7 m *sync.Map
8 }
9
10 var _ SchemaCache = &DefaultCache{}
11
12 // NewDefaultCache creates a new DefaultCache with an initialized sync.Map.
13 func NewDefaultCache() *DefaultCache {
14 return &DefaultCache{m: &sync.Map{}}
15 }
16
17 // Load retrieves a schema from the cache.
18 func (c *DefaultCache) Load(key uint64) (*SchemaCacheEntry, bool) {
19 if c == nil || c.m == nil {
20 return nil, false
21 }
22 val, ok := c.m.Load(key)
23 if !ok {
24 return nil, false
25 }
26 schemaCache, ok := val.(*SchemaCacheEntry)
27 return schemaCache, ok
28 }
29
30 // Store saves a schema to the cache.
31 func (c *DefaultCache) Store(key uint64, value *SchemaCacheEntry) {
32 if c == nil || c.m == nil {
33 return
34 }
35 c.m.Store(key, value)
36 }
37
38 // Range calls f for each entry in the cache (for testing/inspection).
39 func (c *DefaultCache) Range(f func(key uint64, value *SchemaCacheEntry) bool) {
40 if c == nil || c.m == nil {
41 return
42 }
43 c.m.Range(func(k, v interface{}) bool {
44 key, ok := k.(uint64)
45 if !ok {
46 return true
47 }
48 val, ok := v.(*SchemaCacheEntry)
49 if !ok {
50 return true
51 }
52 return f(key, val)
53 })
54 }