forked from ucwong/filecache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.go
45 lines (39 loc) · 773 Bytes
/
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
package filecache
import (
"bytes"
"io"
"os"
"sync"
"time"
)
type cacheItem struct {
content []byte
mutex sync.RWMutex
Size int64
Lastaccess time.Time
Modified time.Time
}
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) {
itm.mutex.Lock()
defer itm.mutex.Unlock()
b = bytes.NewReader(itm.Access())
return
}
func (itm *cacheItem) Access() (c []byte) {
itm.mutex.Lock()
defer itm.mutex.Unlock()
itm.Lastaccess = time.Now()
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
}