-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImageCache.go
99 lines (83 loc) · 2.09 KB
/
ImageCache.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
package main
import (
"container/heap"
"time"
"bytes"
"strconv"
)
type CacheItem struct {
timestamp int64
data *bytes.Reader
index int
path string
}
type ImageCache struct {
items []*CacheItem
imap map[string]*CacheItem
}
var start int64
func NewImageCache() (*ImageCache) {
ic := &ImageCache{imap: make( map[string]*CacheItem, 100)}
heap.Init( ic )
return ic
}
func NewCacheItem(path string, data *bytes.Reader) (*CacheItem) {
if start == 0 {
//FIXME: remove this
start = time.Now().UnixNano()
}
return &CacheItem{ data: data, path: path, timestamp: time.Now().UnixNano() }
}
func (pq ImageCache) Find( path string ) (*bytes.Reader, bool) {
if item, ok := pq.imap[path]; ok {
return item.data, ok
}
return nil, false
}
func (pq ImageCache) Len() int { return len(pq.items) }
func (pq ImageCache) Top() *CacheItem {
n := len(pq.items)
if n == 0 {
return nil
}
return pq.items[0]
}
func (pq ImageCache) Less( i, j int ) bool {
return pq.items[i].timestamp < pq.items[j].timestamp
}
func (pq ImageCache) Swap( i, j int ) {
pq.items[i], pq.items[j] = pq.items[j], pq.items[i]
pq.items[i].index = i
pq.items[j].index = j
}
func (pq ImageCache) GetPaths() ([]string) {
paths := make([]string, len(pq.items))
for i, v := range pq.items {
paths[i] = v.path + ":" + strconv.FormatInt(v.timestamp - start, 10)
}
return paths
}
func (pq *ImageCache) Push( x interface{} ) {
num := len(pq.items)
item := x.(*CacheItem)
item.index = num
pq.items = append( pq.items, item )
pq.imap[item.path] = item
}
func (pq *ImageCache) Pop() interface{} {
old := pq.items
n := len(old)
item := old[n-1]
item.index = -1 // for safety
pq.items = old[0 : n-1]
delete( pq.imap, item.path )
return item
}
func (pq *ImageCache) Update( path string ) {
item, ok := pq.imap[ path ]
if !ok { return }
heap.Remove( pq, item.index )
item.timestamp = time.Now().UnixNano()
//heap.Fix( pq, item.index )
heap.Push(pq, item)
}