This repository has been archived by the owner on Apr 28, 2022. It is now read-only.
forked from trickstercache/trickster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesystem.go
168 lines (140 loc) · 4.75 KB
/
filesystem.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* Copyright 2018 Comcast Cable Communications Management, LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/go-kit/kit/log/level"
"golang.org/x/sys/unix"
)
// FilesystemCache describes a Filesystem Cache
type FilesystemCache struct {
T *TricksterHandler
Config FilesystemCacheConfig
mutexes map[string]*sync.Mutex
mapMutex sync.Mutex
}
// Connect instantiates the FilesystemCache mutex map and starts the Expired Entry Reaper goroutine
func (c *FilesystemCache) Connect() error {
level.Info(c.T.Logger).Log("event", "filesystem cache setup", "cachePath", c.Config.CachePath)
if err := makeDirectory(c.Config.CachePath); err != nil {
return err
}
c.mutexes = make(map[string]*sync.Mutex)
go c.Reap()
return nil
}
// Store places an object in the cache using the specified key and ttl
func (c *FilesystemCache) Store(cacheKey string, data string, ttl int64) error {
expFile, dataFile := c.getFileNames(cacheKey)
expiration := []byte(strconv.FormatInt(time.Now().Unix()+ttl, 10))
level.Debug(c.T.Logger).Log("event", "filesystem cache store", "key", cacheKey, "expFile", expFile, "dataFile", dataFile)
mtx := c.getMutex(cacheKey)
mtx.Lock()
err1 := ioutil.WriteFile(dataFile, []byte(data), os.FileMode(0777))
err2 := ioutil.WriteFile(expFile, expiration, os.FileMode(0777))
mtx.Unlock()
if err1 != nil {
return err1
} else if err2 != nil {
return err2
}
return nil
}
// Retrieve looks for an object in cache and returns it (or an error if not found)
func (c *FilesystemCache) Retrieve(cacheKey string) (string, error) {
_, dataFile := c.getFileNames(cacheKey)
level.Debug(c.T.Logger).Log("event", "filesystem cache retrieve", "key", cacheKey, "dataFile", dataFile)
mtx := c.getMutex(cacheKey)
mtx.Lock()
content, err := ioutil.ReadFile(dataFile)
mtx.Unlock()
if err != nil {
return "", fmt.Errorf("Value for key [%s] not in cache", cacheKey)
}
return string(content), nil
}
// Reap continually iterates through the cache to find expired elements and removes them
func (c *FilesystemCache) Reap() {
for {
now := time.Now().Unix()
files, err := ioutil.ReadDir(c.Config.CachePath)
if err == nil {
for _, file := range files {
if strings.HasSuffix(file.Name(), ".expiration") {
cacheKey := strings.Replace(file.Name(), ".expiration", "", 1)
expFile, dataFile := c.getFileNames(cacheKey)
mtx := c.getMutex(cacheKey)
mtx.Lock()
content, err := ioutil.ReadFile(expFile)
if err == nil {
expiration, err := strconv.ParseInt(string(content), 10, 64)
if err != nil || expiration < now {
level.Debug(c.T.Logger).Log("event", "filesystem cache reap", "key", cacheKey, "dataFile", dataFile)
// Get a lock
c.T.ChannelCreateMtx.Lock()
// Delete the key
os.Remove(expFile)
os.Remove(dataFile)
// Close out the channel if it exists
if _, ok := c.T.ResponseChannels[cacheKey]; ok {
close(c.T.ResponseChannels[cacheKey])
delete(c.T.ResponseChannels, cacheKey)
}
// Unlock
c.T.ChannelCreateMtx.Unlock()
}
}
mtx.Unlock()
}
}
}
time.Sleep(time.Duration(c.T.Config.Caching.ReapSleepMS) * time.Millisecond)
}
}
// Close is not used for FilesystemCache
func (c *FilesystemCache) Close() error {
return nil
}
func (c *FilesystemCache) getFileNames(cacheKey string) (string, string) {
prefix := strings.Replace(c.Config.CachePath+"/"+cacheKey+".", "//", "/", 1)
return prefix + "expiration", prefix + "data"
}
func (c *FilesystemCache) getMutex(cacheKey string) *sync.Mutex {
var mtx *sync.Mutex
var ok bool
c.mapMutex.Lock()
if mtx, ok = c.mutexes[cacheKey]; !ok {
mtx = &sync.Mutex{}
c.mutexes[cacheKey] = mtx
}
c.mapMutex.Unlock()
return mtx
}
// writeable returns true if the path is writeable by the calling process.
func writeable(path string) bool {
return unix.Access(path, unix.W_OK) == nil
}
// makeDirectory creates a directory on the filesystem and exits the application in the event of a failure.
func makeDirectory(path string) error {
err := os.MkdirAll(path, 0755)
if err != nil || !writeable(path) {
return fmt.Errorf("[%s] directory is not writeable by the trickster: %v", path, err)
}
return nil
}