-
Notifications
You must be signed in to change notification settings - Fork 2
/
cache.go
42 lines (34 loc) · 827 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
40
41
42
package radium
import (
"context"
"sync"
)
// Cache implementation is responsible for caching
// a given query-results pair for later use
type Cache interface {
Source
// Set should store the given pair in a caching
// backend for fast access. If an entry with same
// query already exists, it should be replaced
// with the new results slice
Set(q Query, rs []Article) error
}
type defaultCache struct {
mu *sync.Mutex
data map[string][]Article
}
func (dc *defaultCache) Search(ctx context.Context, q Query) ([]Article, error) {
dc.mu.Lock()
defer dc.mu.Unlock()
var rs []Article
if vals, found := dc.data[q.Text]; found {
rs = append(rs, vals...)
}
return rs, nil
}
func (dc *defaultCache) Set(q Query, rs []Article) error {
dc.mu.Lock()
defer dc.mu.Unlock()
dc.data[q.Text] = rs
return nil
}