Files
fission-src/cache/cache.go
T
Soam Vasani 094da5c69a Add expiry time to cache
MakeCache() now takes a time.Duration after which entries are
considered invalid, and not returned from a Get().
2016-11-04 23:05:29 -07:00

171 lines
3.5 KiB
Go

/*
Copyright 2016 The Fission Authors.
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 cache
import (
"time"
"fmt"
"github.com/platform9/fission"
)
type requestType int
const (
GET requestType = iota
SET
DELETE
EXPIRE
)
type (
Value struct {
ctime time.Time
atime time.Time
value interface{}
}
Cache struct {
cache map[interface{}]Value
expiryTime time.Duration
requestChannel chan *request
}
request struct {
requestType
key interface{}
value interface{}
responseChannel chan *response
}
response struct {
error
value interface{}
}
)
func (c *Cache) IsOld(v *Value) bool {
if c.expiryTime == 0 {
return false
}
if time.Now().Sub(v.atime) > c.expiryTime {
return true
}
return false
}
func MakeCache(expiryTime time.Duration) *Cache {
c := &Cache{
cache: make(map[interface{}]Value),
expiryTime: expiryTime,
requestChannel: make(chan *request),
}
go c.service()
if expiryTime != time.Duration(0) {
go c.expiryService()
}
return c
}
func (c *Cache) service() {
for {
req := <-c.requestChannel
resp := &response{}
switch req.requestType {
case GET:
val, ok := c.cache[req.key]
if !ok {
resp.error = fission.MakeError(fission.ErrorNotFound,
fmt.Sprintf("key '%v' not found", req.key))
} else if c.IsOld(&val) {
resp.error = fission.MakeError(fission.ErrorNotFound,
fmt.Sprintf("key '%v' expired (atime %v)", req.key, val.atime))
delete(c.cache, req.key)
} else {
// update atime
val.atime = time.Now()
c.cache[req.key] = val
resp.value = val.value
}
req.responseChannel <- resp
case SET:
now := time.Now()
c.cache[req.key] = Value{
value: req.value,
ctime: now,
atime: now,
}
req.responseChannel <- resp
case DELETE:
delete(c.cache, req.key)
req.responseChannel <- resp
case EXPIRE:
for k, v := range c.cache {
if c.IsOld(&v) {
delete(c.cache, k)
}
}
// no response
default:
resp.error = fission.MakeError(fission.ErrorInvalidArgument,
fmt.Sprintf("invalid request type: %v", req.requestType))
req.responseChannel <- resp
}
}
}
func (c *Cache) Get(key interface{}) (interface{}, error) {
respChannel := make(chan *response)
c.requestChannel <- &request{
requestType: GET,
key: key,
responseChannel: respChannel,
}
resp := <-respChannel
return resp.value, resp.error
}
func (c *Cache) Set(key interface{}, value interface{}) error {
respChannel := make(chan *response)
c.requestChannel <- &request{
requestType: SET,
key: key,
value: value,
responseChannel: respChannel,
}
resp := <-respChannel
return resp.error
}
func (c *Cache) Delete(key interface{}) error {
respChannel := make(chan *response)
c.requestChannel <- &request{
requestType: DELETE,
key: key,
responseChannel: respChannel,
}
resp := <-respChannel
return resp.error
}
func (c *Cache) expiryService() {
for {
time.Sleep(time.Minute)
c.requestChannel <- &request{
requestType: EXPIRE,
}
}
}