/
cache.go
111 lines (102 loc) · 2.76 KB
/
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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package brooklet
import (
"github.com/hygerth/brooklet/db"
"github.com/hygerth/brooklet/structure"
"github.com/hygerth/brooklet/utils"
"log"
"sync"
"time"
)
type Cache struct {
lock sync.RWMutex
duration int64
timeunit time.Duration
data map[string]Data
}
type Data struct {
timestamp time.Time
Title string
Entries []structure.Entry
}
func (c *Cache) Init(options map[string]string) {
c.parseOptions(options)
c.data = make(map[string]Data)
go func() {
timer := time.Tick(1 * time.Minute)
for _ = range timer {
//log.Println("brooklet: Cleaning cache")
c.lock.RLock()
max := time.Duration(c.duration) * c.timeunit
for key := range c.data {
if time.Since(c.data[key].timestamp) > max {
log.Printf("brooklet: Purged '%s' from cache\n", key)
delete(c.data, key)
}
}
c.lock.RUnlock()
//log.Println("brooklet: Cleaning cache completed")
}
}()
}
func (c *Cache) parseOptions(options map[string]string) {
c.duration = utils.StringToInt64(options["cacheduration"])
c.timeunit = utils.StringToTimeDurationUnit(options["cachetimeunit"])
}
func (c *Cache) Empty() {
log.Println("brooklet: Emptying cache")
c.lock.RLock()
defer c.lock.RUnlock()
for key := range c.data {
delete(c.data, key)
}
}
func (c *Cache) Get(key string) (*Data, bool) {
c.lock.RLock()
defer c.lock.RUnlock()
d, ok := c.data[key]
if !ok || time.Since(d.timestamp) > time.Duration(c.duration) * c.timeunit {
c.lock.RUnlock()
c.Set(key)
c.lock.RLock()
return c.Get(key)
}
log.Println("brooklet: Serving from cache")
return &d, ok
}
func (c *Cache) Set(key string) {
log.Printf("brooklet: Adding '%s' to cache\n", key)
if key == "all" {
c.SetAll()
return
}
c.lock.Lock()
defer c.lock.Unlock()
feed, _ := db.GetFeedByName(key)
entries := structure.ExtractEntriesFromFeeds(feed)
filter, _ := db.GetFilter()
entries = structure.FilterEntries(entries, filter)
if len(entries) > 100 {
entries = entries[:100]
}
var d Data
d.timestamp = time.Now()
d.Title = feed.Title
d.Entries = entries
c.data[key] = d
}
func (c *Cache) SetAll() {
c.lock.Lock()
defer c.lock.Unlock()
feeds, _ := db.GetAllFeeds()
entries := structure.ExtractEntriesFromFeeds(feeds...)
filter, _ := db.GetFilter()
entries = structure.FilterEntries(entries, filter)
if len(entries) > 100 {
entries = entries[:100]
}
var d Data
d.timestamp = time.Now()
d.Title = "All"
d.Entries = entries
c.data["all"] = d
}