-
Notifications
You must be signed in to change notification settings - Fork 2
/
cache.go
39 lines (33 loc) · 846 Bytes
/
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
package rct
import (
"time"
)
// An entry in a datagram cache (pair of datagram and timestamp)
type cacheEntry struct {
dg *Datagram
ts time.Time
}
// A datagram cache
type Cache struct {
entries map[Identifier]cacheEntry
timeout time.Duration
}
// Creates a new datagram cache
func NewCache(timeout time.Duration) (cache *Cache) {
return &Cache{
make(map[Identifier]cacheEntry),
timeout,
}
}
// Returns cache entry for the given identifier, if still valid under timeout
func (c *Cache) Get(i Identifier) (dg *Datagram, ok bool) {
entry, ok := c.entries[i]
if !ok || c.timeout < time.Since(entry.ts) {
return &Datagram{}, false
}
return entry.dg, true
}
// Puts given datagram into the cache, for the identifier contained in the datagram
func (c *Cache) Put(dg *Datagram) {
c.entries[dg.Id] = cacheEntry{dg, time.Now()}
}