-
Notifications
You must be signed in to change notification settings - Fork 2
/
filescache.go
55 lines (47 loc) · 1.21 KB
/
filescache.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
// -*- Go -*-
/* ------------------------------------------------ */
/* Golang source */
/* Author: Alexei Panov <[email protected]> */
/* ------------------------------------------------ */
package main
import (
"sync"
log "github.com/sirupsen/logrus"
)
// FilesCacheMemory type is a thread-safe files cache
type FilesCacheMemory struct {
cache map[string]string
mutex sync.RWMutex
}
// Get function get file from cache by name
func (fc *FilesCacheMemory) Get(name string) (value string) {
var ok bool
fc.mutex.RLock()
defer fc.mutex.RUnlock()
if value, ok = fc.cache[name]; ok {
return
}
return ""
}
// Set function store file information in cache
func (fc *FilesCacheMemory) Set(name, value string) {
fc.mutex.Lock()
fc.cache[name] = value
fc.mutex.Unlock()
}
// Update function updates files in cache
func (fc *FilesCacheMemory) Update() {
var (
files []FileCache
err error
)
log.Debugf("Start update files cache...")
if files, err = getFilesFromCache(); err != nil {
log.Errorf("Unable to get files from cache for local memory cache store")
return
}
for _, file := range files {
fc.Set(file.FileID, file.FileName)
}
log.Debugf("Finish update files cache.")
}