1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-23 13:10:49 +00:00
Beego/pkg/client/cache/file.go

272 lines
7.2 KiB
Go
Raw Normal View History

2020-07-22 14:50:08 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
//
// 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 (
"bytes"
2020-10-04 14:11:28 +00:00
"context"
2020-07-22 14:50:08 +00:00
"crypto/md5"
"encoding/gob"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strconv"
"time"
2020-10-04 14:11:28 +00:00
"github.com/pkg/errors"
2020-07-22 14:50:08 +00:00
)
2020-08-05 16:44:39 +00:00
// FileCacheItem is basic unit of file cache adapter which
// contains data and expire time.
2020-07-22 14:50:08 +00:00
type FileCacheItem struct {
Data interface{}
Lastaccess time.Time
Expired time.Time
}
// FileCache Config
var (
FileCachePath = "cache" // cache directory
FileCacheFileSuffix = ".bin" // cache file suffix
FileCacheDirectoryLevel = 2 // cache file deep level if auto generated cache files.
FileCacheEmbedExpiry time.Duration // cache expire time, default is no expire forever.
)
// FileCache is cache adapter for file storage.
type FileCache struct {
CachePath string
FileSuffix string
DirectoryLevel int
EmbedExpiry int
}
2020-08-06 15:07:18 +00:00
// NewFileCache creates a new file cache with no config.
2020-08-05 16:44:39 +00:00
// The level and expiry need to be set in the method StartAndGC as config string.
2020-07-22 14:50:08 +00:00
func NewFileCache() Cache {
// return &FileCache{CachePath:FileCachePath, FileSuffix:FileCacheFileSuffix}
return &FileCache{}
}
2020-08-05 16:44:39 +00:00
// StartAndGC starts gc for file cache.
// config must be in the format {CachePath:"/cache","FileSuffix":".bin","DirectoryLevel":"2","EmbedExpiry":"0"}
2020-07-22 14:50:08 +00:00
func (fc *FileCache) StartAndGC(config string) error {
cfg := make(map[string]string)
err := json.Unmarshal([]byte(config), &cfg)
if err != nil {
return err
}
if _, ok := cfg["CachePath"]; !ok {
cfg["CachePath"] = FileCachePath
}
if _, ok := cfg["FileSuffix"]; !ok {
cfg["FileSuffix"] = FileCacheFileSuffix
}
if _, ok := cfg["DirectoryLevel"]; !ok {
cfg["DirectoryLevel"] = strconv.Itoa(FileCacheDirectoryLevel)
}
if _, ok := cfg["EmbedExpiry"]; !ok {
cfg["EmbedExpiry"] = strconv.FormatInt(int64(FileCacheEmbedExpiry.Seconds()), 10)
}
fc.CachePath = cfg["CachePath"]
fc.FileSuffix = cfg["FileSuffix"]
fc.DirectoryLevel, _ = strconv.Atoi(cfg["DirectoryLevel"])
fc.EmbedExpiry, _ = strconv.Atoi(cfg["EmbedExpiry"])
fc.Init()
return nil
}
2020-08-05 16:44:39 +00:00
// Init makes new a dir for file cache if it does not already exist
2020-07-22 14:50:08 +00:00
func (fc *FileCache) Init() {
if ok, _ := exists(fc.CachePath); !ok { // todo : error handle
_ = os.MkdirAll(fc.CachePath, os.ModePerm) // todo : error handle
}
}
2020-08-05 16:44:39 +00:00
// getCachedFilename returns an md5 encoded file name.
2020-07-22 14:50:08 +00:00
func (fc *FileCache) getCacheFileName(key string) string {
m := md5.New()
io.WriteString(m, key)
keyMd5 := hex.EncodeToString(m.Sum(nil))
cachePath := fc.CachePath
switch fc.DirectoryLevel {
case 2:
cachePath = filepath.Join(cachePath, keyMd5[0:2], keyMd5[2:4])
case 1:
cachePath = filepath.Join(cachePath, keyMd5[0:2])
}
if ok, _ := exists(cachePath); !ok { // todo : error handle
_ = os.MkdirAll(cachePath, os.ModePerm) // todo : error handle
}
return filepath.Join(cachePath, fmt.Sprintf("%s%s", keyMd5, fc.FileSuffix))
}
// Get value from file cache.
2020-08-05 16:44:39 +00:00
// if nonexistent or expired return an empty string.
2020-10-04 14:11:28 +00:00
func (fc *FileCache) Get(ctx context.Context, key string) (interface{}, error) {
2020-07-22 14:50:08 +00:00
fileData, err := FileGetContents(fc.getCacheFileName(key))
if err != nil {
2020-10-04 14:11:28 +00:00
return nil, err
2020-07-22 14:50:08 +00:00
}
2020-10-04 14:11:28 +00:00
2020-07-22 14:50:08 +00:00
var to FileCacheItem
2020-10-04 14:11:28 +00:00
err = GobDecode(fileData, &to)
if err != nil {
return nil, err
}
2020-07-22 14:50:08 +00:00
if to.Expired.Before(time.Now()) {
2020-10-04 14:11:28 +00:00
return nil, errors.New("The key is expired")
2020-07-22 14:50:08 +00:00
}
2020-10-04 14:11:28 +00:00
return to.Data, nil
2020-07-22 14:50:08 +00:00
}
// GetMulti gets values from file cache.
2020-08-05 16:44:39 +00:00
// if nonexistent or expired return an empty string.
2020-10-04 14:11:28 +00:00
func (fc *FileCache) GetMulti(ctx context.Context, keys []string) ([]interface{}, error) {
2020-07-22 14:50:08 +00:00
var rc []interface{}
for _, key := range keys {
2020-10-04 14:11:28 +00:00
val, err := fc.Get(context.Background(), key)
if err != nil {
rc = append(rc, err)
} else {
rc = append(rc, val)
}
2020-07-22 14:50:08 +00:00
}
2020-10-04 14:11:28 +00:00
return rc, nil
2020-07-22 14:50:08 +00:00
}
// Put value into file cache.
2020-08-05 16:44:39 +00:00
// timeout: how long this file should be kept in ms
2020-07-22 14:50:08 +00:00
// if timeout equals fc.EmbedExpiry(default is 0), cache this item forever.
2020-10-04 14:11:28 +00:00
func (fc *FileCache) Put(ctx context.Context, key string, val interface{}, timeout time.Duration) error {
2020-07-22 14:50:08 +00:00
gob.Register(val)
item := FileCacheItem{Data: val}
if timeout == time.Duration(fc.EmbedExpiry) {
item.Expired = time.Now().Add((86400 * 365 * 10) * time.Second) // ten years
} else {
item.Expired = time.Now().Add(timeout)
}
item.Lastaccess = time.Now()
data, err := GobEncode(item)
if err != nil {
return err
}
return FilePutContents(fc.getCacheFileName(key), data)
}
// Delete file cache value.
2020-10-04 14:11:28 +00:00
func (fc *FileCache) Delete(ctx context.Context, key string) error {
2020-07-22 14:50:08 +00:00
filename := fc.getCacheFileName(key)
if ok, _ := exists(filename); ok {
return os.Remove(filename)
}
return nil
}
2020-08-05 16:44:39 +00:00
// Incr increases cached int value.
// fc value is saved forever unless deleted.
2020-10-04 14:11:28 +00:00
func (fc *FileCache) Incr(ctx context.Context, key string) error {
data, _ := fc.Get(context.Background(), key)
2020-07-22 14:50:08 +00:00
var incr int
if reflect.TypeOf(data).Name() != "int" {
incr = 0
} else {
incr = data.(int) + 1
}
2020-10-04 14:11:28 +00:00
fc.Put(context.Background(), key, incr, time.Duration(fc.EmbedExpiry))
2020-07-22 14:50:08 +00:00
return nil
}
2020-08-05 16:44:39 +00:00
// Decr decreases cached int value.
2020-10-04 14:11:28 +00:00
func (fc *FileCache) Decr(ctx context.Context, key string) error {
data, _ := fc.Get(context.Background(), key)
2020-07-22 14:50:08 +00:00
var decr int
if reflect.TypeOf(data).Name() != "int" || data.(int)-1 <= 0 {
decr = 0
} else {
decr = data.(int) - 1
}
2020-10-04 14:11:28 +00:00
fc.Put(context.Background(), key, decr, time.Duration(fc.EmbedExpiry))
2020-07-22 14:50:08 +00:00
return nil
}
2020-08-05 16:44:39 +00:00
// IsExist checks if value exists.
2020-10-04 14:11:28 +00:00
func (fc *FileCache) IsExist(ctx context.Context, key string) (bool, error) {
2020-07-22 14:50:08 +00:00
ret, _ := exists(fc.getCacheFileName(key))
2020-10-04 14:11:28 +00:00
return ret, nil
2020-07-22 14:50:08 +00:00
}
2020-08-05 16:44:39 +00:00
// ClearAll cleans cached files (not implemented)
2020-10-04 14:11:28 +00:00
func (fc *FileCache) ClearAll(context.Context) error {
2020-07-22 14:50:08 +00:00
return nil
}
2020-08-05 16:44:39 +00:00
// Check if a file exists
2020-07-22 14:50:08 +00:00
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
2020-08-05 16:44:39 +00:00
// FileGetContents Reads bytes from a file.
// if non-existent, create this file.
2020-07-22 14:50:08 +00:00
func FileGetContents(filename string) (data []byte, e error) {
return ioutil.ReadFile(filename)
}
2020-08-05 16:44:39 +00:00
// FilePutContents puts bytes into a file.
// if non-existent, create this file.
2020-07-22 14:50:08 +00:00
func FilePutContents(filename string, content []byte) error {
return ioutil.WriteFile(filename, content, os.ModePerm)
}
2020-08-05 16:44:39 +00:00
// GobEncode Gob encodes a file cache item.
2020-07-22 14:50:08 +00:00
func GobEncode(data interface{}) ([]byte, error) {
buf := bytes.NewBuffer(nil)
enc := gob.NewEncoder(buf)
err := enc.Encode(data)
if err != nil {
return nil, err
}
return buf.Bytes(), err
}
2020-08-05 16:44:39 +00:00
// GobDecode Gob decodes a file cache item.
2020-07-22 14:50:08 +00:00
func GobDecode(data []byte, to *FileCacheItem) error {
buf := bytes.NewBuffer(data)
dec := gob.NewDecoder(buf)
return dec.Decode(&to)
}
func init() {
Register("file", NewFileCache)
}