Repository navigation
Expand file tree
/
Copy pathitem.go
More file actions
75 lines (64 loc) · 1.7 KB
/
Copy pathitem.go
File metadata and controls
75 lines (64 loc) · 1.7 KB
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
package filecache
import (
"bytes"
"io"
"os"
"sync"
"time"
)
type cacheItem struct {
name string
content []byte
mutex sync.RWMutex
Size int64
Lastaccess time.Time
ModTime time.Time
AccessCount uint64
}
// Key returns the name of the cached item.
func (itm *cacheItem) Key() string {
return itm.name
}
// Content returns the cached content.
func (itm *cacheItem) Content() []byte {
itm.mutex.RLock()
defer itm.mutex.RUnlock()
return itm.content
}
// WasModified checks if the file has the same mod time as when cached.
func (itm *cacheItem) WasModified(fi os.FileInfo) bool {
itm.mutex.RLock()
defer itm.mutex.RUnlock()
return itm.ModTime.Equal(fi.ModTime())
}
// GetReader returns a new reader for the cached content.
func (itm *cacheItem) GetReader() io.Reader {
itm.mutex.RLock()
defer itm.mutex.RUnlock()
return bytes.NewReader(itm.content)
}
// Access returns the cached content and updates last access time and access count.
func (itm *cacheItem) Access() []byte {
itm.mutex.Lock()
itm.Lastaccess = time.Now()
itm.AccessCount++
content := itm.content
itm.mutex.Unlock()
return content
}
// Dur returns the time since the item was last accessed.
func (itm *cacheItem) Dur() time.Duration {
itm.mutex.RLock()
last := itm.Lastaccess
itm.mutex.RUnlock()
return time.Since(last)
}
// 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 }