-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidcache.go
More file actions
70 lines (57 loc) · 1.86 KB
/
Copy pathidcache.go
File metadata and controls
70 lines (57 loc) · 1.86 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
package spotlib
import (
"time"
"github.com/BottleFmt/gobottle"
)
// idCacheEntry represents a cached ID card with a timestamp for expiration handling
type idCacheEntry struct {
id *gobottle.IDCard // The cached identity card
t time.Time // Time when the entry was cached
}
// getIDCardFromCache retrieves an ID card from the cache if it exists
// Returns nil if the hash is not found in the cache
func (c *Client) getIDCardFromCache(h []byte) *gobottle.IDCard {
c.idCacheLk.RLock()
defer c.idCacheLk.RUnlock()
// handle expiration in cache
v, ok := c.idCache[string(h)]
if !ok {
return nil
}
return v.id
}
// setIDCardCache adds or updates an ID card in the cache
// Includes protection against cache overfill by clearing the cache if it grows too large
// Returns true if this was a cache update (not a new entry)
func (c *Client) setIDCardCache(h []byte, obj *gobottle.IDCard) bool {
c.idCacheLk.Lock()
defer c.idCacheLk.Unlock()
if len(c.idCache) > 1024 {
// cache overfill protection
clear(c.idCache)
}
// Check if this is an update to an existing entry
isUpdate := false
key := string(h)
if _, exists := c.idCache[key]; exists {
isUpdate = true
}
e := &idCacheEntry{
id: obj,
t: time.Now(),
}
c.idCache[key] = e
return isUpdate
}
// needKeyRefresh clears the ID cache when signature verification fails
// This triggers fetching fresh ID cards when needed
func (c *Client) needKeyRefresh() {
c.idCacheLk.Lock()
defer c.idCacheLk.Unlock()
// for now we only clear the cache, actually we should send a signal on a channel that will trigger a waiting
// thread to send the list of cached ids (id+modified timestamp in secs) for the server to respond if the ID
// is known and up to date, known and outdated (flush from cache) ou not known (flush from cache).
//
// This would help ensure that IDs are always up to date
clear(c.idCache)
}