-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentry.go
More file actions
54 lines (46 loc) · 1.1 KB
/
entry.go
File metadata and controls
54 lines (46 loc) · 1.1 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
package fastcache
import (
"time"
"github.com/dgraph-io/ristretto"
rcache "github.com/go-redis/cache/v8"
)
type entry struct {
RevalidateAt time.Time
ExpiresAt time.Time
Key string
Value interface{}
}
func (e *entry) revalid(c *Cache, r *ristretto.Cache, i *ItemRequest) {
if time.Now().After(e.RevalidateAt) {
go func() {
if newEntry, err := i.loadEntry(c); err == nil {
r.SetWithTTL(i.Key, newEntry, 1, i.expiresAt(c))
// wait for value to pass through buffers
r.Wait()
c.redis_conn.Set(&rcache.Item{
Key: i.Key,
Value: newEntry,
SkipLocalCache: true,
TTL: i.expiresAt(c),
})
}
}()
}
}
func (e *entry) expired() bool {
return time.Now().After(e.ExpiresAt)
}
func (i *ItemRequest) loadEntry(c *Cache) (*entry, error) {
c.mu.Lock()
defer c.mu.Unlock()
if result, err := i.Do(i); err == nil {
return &entry{
RevalidateAt: time.Now().Add(i.revalidateAt(c)),
ExpiresAt: time.Now().Add(i.expiresAt(c)),
Key: i.Key,
Value: result,
}, nil
} else {
return nil, err
}
}