2
0
mirror of https://github.com/miguelmota/cointop synced 2024-11-16 21:25:38 +00:00
cointop/pkg/fcache/fcache.go
Miguel Mota c0fffae086 fcache
Former-commit-id: 70acc67450cc1b1f33bf24dcd9b55067dd601e08 [formerly 70acc67450cc1b1f33bf24dcd9b55067dd601e08 [formerly e21e905f55331dd7065e546bb41cb09cb70f9c9e [formerly bb60145fad704b7ff79ce2b9d0c96e7cddf99e61]]]
Former-commit-id: 5b315791e4e1104894aaaae41abb1c41d5683f84
Former-commit-id: 7ee3f06ae54278620ca8b76846fa9015bd28c68e [formerly c3c89d40300b7a5a7fa354f9aff76f4ccc24fd80]
Former-commit-id: 72585f33661de190e503ba42ba5591ad7100bb68
2018-04-22 15:52:33 -07:00

131 lines
2.5 KiB
Go

package fcache
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
"time"
)
const cachedir = "/tmp"
// Set writes item to cache
func Set(key string, data interface{}, expire time.Duration) error {
key = regexp.MustCompile("[^a-zA-Z0-9_-]").ReplaceAllLiteralString(key, "")
file := fmt.Sprintf("fcache.%s.%v", key, strconv.FormatInt(time.Now().Add(expire).Unix(), 10))
fpath := filepath.Join(cachedir, file)
clean(key)
serialized, err := serialize(data)
if err != nil {
return err
}
var fmutex sync.RWMutex
fmutex.Lock()
defer fmutex.Unlock()
fp, err := os.OpenFile(fpath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer fp.Close()
if _, err = fp.Write(serialized); err != nil {
return err
}
return nil
}
// Get reads item from cache
func Get(key string, dst interface{}) error {
key = regexp.MustCompile("[^a-zA-Z0-9_-]").ReplaceAllLiteralString(key, "")
pattern := filepath.Join(cachedir, fmt.Sprintf("fcache.%s.*", key))
files, err := filepath.Glob(pattern)
if len(files) < 1 || err != nil {
return errors.New("fcache: no cache file found")
}
if _, err = os.Stat(files[0]); err != nil {
return err
}
fp, err := os.OpenFile(files[0], os.O_RDONLY, 0400)
if err != nil {
return err
}
defer fp.Close()
var serialized []byte
buf := make([]byte, 1024)
for {
var n int
n, err = fp.Read(buf)
serialized = append(serialized, buf[0:n]...)
if err != nil || err == io.EOF {
break
}
}
if err = deserialize(serialized, dst); err != nil {
return err
}
for _, file := range files {
exptime, err := strconv.ParseInt(strings.Split(file, ".")[2], 10, 64)
if err != nil {
return err
}
if exptime < time.Now().Unix() {
if _, err = os.Stat(file); err == nil {
os.Remove(file)
}
}
}
return nil
}
// clean removes item from cache
func clean(key string) error {
pattern := filepath.Join(cachedir, fmt.Sprintf("fcache.%s.*", key))
files, _ := filepath.Glob(pattern)
for _, file := range files {
if _, err := os.Stat(file); err == nil {
os.Remove(file)
}
}
return nil
}
// serialize encodes a value using binary
func serialize(src interface{}) ([]byte, error) {
buf := new(bytes.Buffer)
if err := gob.NewEncoder(buf).Encode(src); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// deserialize decodes a value using binary
func deserialize(src []byte, dst interface{}) error {
buf := bytes.NewReader(src)
if err := gob.NewDecoder(buf).Decode(dst); err != nil {
return err
}
return nil
}