aboutsummaryrefslogtreecommitdiff
path: root/cache
diff options
context:
space:
mode:
authorrtz12 <koenig@fagott.pw>2016-10-09 01:33:34 (UTC)
committerrtz12 <koenig@fagott.pw>2016-10-09 01:33:34 (UTC)
commit924f63a7ffbd661d64c1d04547076ea4a31d1739 (patch)
tree3ef1122f4cf1c6c6b8b8ae407407c057cb747bc0 /cache
parentdb4bd8185f7283420b3f60fc561b1b8e4f97b3b9 (diff)
parent46185bf12dd907e536aa216a03c818f40f88cae8 (diff)
Merge branch 'master' of projekte.fagott.pw:grilist
Diffstat (limited to 'cache')
-rw-r--r--cache/cache.go54
1 files changed, 27 insertions, 27 deletions
diff --git a/cache/cache.go b/cache/cache.go
index f6e5661..9647836 100644
--- a/cache/cache.go
+++ b/cache/cache.go
@@ -1,56 +1,56 @@
1package cache 1package cache
2 2
3import ( 3import (
4 "time" 4 "log"
5 "log" 5 "time"
6) 6)
7 7
8type CacheItem struct { 8type CacheItem struct {
9 Expires time.Time 9 Expires time.Time
10 Value interface{} 10 Value interface{}
11} 11}
12type Cache struct { 12type Cache struct {
13 items map[int]CacheItem 13 items map[int]CacheItem
14} 14}
15 15
16func New() *Cache { 16func New() *Cache {
17 c := &Cache{ make(map[int]CacheItem) } 17 c := &Cache{make(map[int]CacheItem)}
18 go c.ticker() 18 go c.ticker()
19 return c 19 return c
20} 20}
21 21
22func (c *Cache) ticker() { 22func (c *Cache) ticker() {
23 for _ = range time.NewTicker(time.Second * 1).C { 23 for _ = range time.NewTicker(time.Second * 1).C {
24 t := time.Now() 24 t := time.Now()
25 for key, item := range c.items { 25 for key, item := range c.items {
26 if t.Sub(item.Expires) > 0 { 26 if t.Sub(item.Expires) > 0 {
27 log.Println("cache delete", key) 27 log.Println("cache delete", key)
28 delete(c.items, key) 28 delete(c.items, key)
29 } 29 }
30 } 30 }
31 } 31 }
32} 32}
33 33
34func (c *Cache) Insert(key int, item interface{}) { 34func (c *Cache) Insert(key int, item interface{}) {
35 c.items[key] = CacheItem{ time.Now().Add(time.Minute * 15), item } 35 c.items[key] = CacheItem{time.Now().Add(time.Minute * 15), item}
36} 36}
37 37
38func (c *Cache) Remove(key int) { 38func (c *Cache) Remove(key int) {
39 delete(c.items, key) 39 delete(c.items, key)
40} 40}
41 41
42func (c *Cache) Clear() { 42func (c *Cache) Clear() {
43 c.items = make(map[int]CacheItem) 43 c.items = make(map[int]CacheItem)
44} 44}
45 45
46func (c *Cache) Get(key int) (interface{}, bool) { 46func (c *Cache) Get(key int) (interface{}, bool) {
47 if ci, ok := c.items[key]; ok { 47 if ci, ok := c.items[key]; ok {
48 return ci.Value, true 48 return ci.Value, true
49 } 49 }
50 return nil, false 50 return nil, false
51} 51}
52 52
53func (c *Cache) Has(key int) bool { 53func (c *Cache) Has(key int) bool {
54 _, ok := c.items[key] 54 _, ok := c.items[key]
55 return ok 55 return ok
56} \ No newline at end of file 56}