/
cache.go
112 lines (94 loc) · 2.31 KB
/
cache.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
100
101
102
103
104
105
106
107
108
109
110
111
112
package fs
import (
"fmt"
"time"
"github.com/cyverse/go-irodsclient/irods/util"
gocache "github.com/patrickmn/go-cache"
)
// FileSystemCache ...
type FileSystemCache struct {
CacheTimeout time.Duration
CleanupTimeout time.Duration
EntryCache *gocache.Cache
DirCache *gocache.Cache
}
// NewFileSystemCache creates a new FileSystemCache
func NewFileSystemCache(cacheTimeout time.Duration, cleanup time.Duration) *FileSystemCache {
entryCache := gocache.New(cacheTimeout, cleanup)
dirCache := gocache.New(cacheTimeout, cleanup)
return &FileSystemCache{
CacheTimeout: cacheTimeout,
CleanupTimeout: cleanup,
EntryCache: entryCache,
DirCache: dirCache,
}
}
func shouldHaveInfiniteCacheTTL(path string) bool {
zone, err := util.GetIRODSZone(path)
if err != nil {
return false
}
root := "/"
zoneRoot := fmt.Sprintf("/%s", zone)
home := fmt.Sprintf("/%s/home", zone)
switch path {
case root:
return true
case zoneRoot:
return true
case home:
return true
default:
return false
}
}
// AddEntryCache ...
func (cache *FileSystemCache) AddEntryCache(entry *FSEntry) {
if shouldHaveInfiniteCacheTTL(entry.Path) {
cache.EntryCache.Set(entry.Path, entry, -1)
}
// default
cache.EntryCache.Set(entry.Path, entry, 0)
}
// RemoveEntryCache ...
func (cache *FileSystemCache) RemoveEntryCache(path string) {
cache.EntryCache.Delete(path)
}
// GetEntryCache ...
func (cache *FileSystemCache) GetEntryCache(path string) *FSEntry {
entry, _ := cache.EntryCache.Get(path)
if fsentry, ok := entry.(*FSEntry); ok {
return fsentry
}
return nil
}
// ClearEntryCache ...
func (cache *FileSystemCache) ClearEntryCache() {
cache.EntryCache.Flush()
}
// AddDirCache ...
func (cache *FileSystemCache) AddDirCache(path string, entries []string) {
if shouldHaveInfiniteCacheTTL(path) {
cache.DirCache.Set(path, entries, -1)
}
// default
cache.DirCache.Set(path, entries, 0)
}
// RemoveDirCache ...
func (cache *FileSystemCache) RemoveDirCache(path string) {
cache.DirCache.Delete(path)
}
// GetDirCache ...
func (cache *FileSystemCache) GetDirCache(path string) []string {
data, exist := cache.DirCache.Get(path)
if exist {
if entries, ok := data.([]string); ok {
return entries
}
}
return nil
}
// ClearDirCache ...
func (cache *FileSystemCache) ClearDirCache() {
cache.DirCache.Flush()
}