...
1 package service
2
3 import (
4 "sync"
5 "time"
6 )
7
8 const (
9 cacheExpiration = 5 * time.Minute
10 )
11
12 type cache struct {
13 mu sync.RWMutex
14 cache map[string][]string
15 expiration time.Duration
16 }
17
18 func (c *cache) Get(key string) (val []string) {
19 c.mu.RLock()
20 defer c.mu.RUnlock()
21 return c.cache[key]
22 }
23
24 func (c *cache) Insert(key string, val []string) {
25 c.mu.Lock()
26 c.cache[key] = val
27 c.mu.Unlock()
28 go func() {
29 time.Sleep(c.expiration)
30 c.Delete(key)
31 }()
32 }
33
34 func (c *cache) Delete(key string) {
35 c.mu.Lock()
36 defer c.mu.Unlock()
37 delete(c.cache, key)
38 }
39
View as plain text