-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathitem.go
66 lines (55 loc) · 1.27 KB
/
item.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
package filecache
import (
"bytes"
"io"
"os"
"sync"
"time"
)
type cacheItem struct {
name string
content []byte
mutex sync.RWMutex
Size int64
Lastaccess time.Time
Modified time.Time
AccessCount uint64
}
func (itm *cacheItem) Key() string {
return itm.name
}
func (itm *cacheItem) Content() []byte {
return itm.content
}
func (itm *cacheItem) WasModified(fi os.FileInfo) bool {
itm.mutex.RLock()
defer itm.mutex.RUnlock()
return itm.Modified.Equal(fi.ModTime())
}
func (itm *cacheItem) GetReader() (b io.Reader) {
b = bytes.NewReader(itm.Access())
return
}
func (itm *cacheItem) Access() (c []byte) {
itm.mutex.Lock()
defer itm.mutex.Unlock()
itm.Lastaccess = time.Now()
itm.AccessCount++
c = itm.content
return
}
func (itm *cacheItem) Dur() (t time.Duration) {
itm.mutex.RLock()
defer itm.mutex.RUnlock()
t = time.Now().Sub(itm.Lastaccess)
return
}
// CacheItemPair maps key to access counter
type CacheItemPair struct {
Key string
AccessCount uint64
}
type CacheItemPairList []CacheItemPair
func (p CacheItemPairList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p CacheItemPairList) Len() int { return len(p) }
func (p CacheItemPairList) Less(i, j int) bool { return p[i].AccessCount > p[j].AccessCount }