1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-18 00:30:54 +00:00
Beego/cache/file.go

259 lines
6.6 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// 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
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// 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.
2013-11-18 03:22:06 +00:00
package cache
import (
"bytes"
"crypto/md5"
"encoding/gob"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
2013-11-18 03:22:06 +00:00
"os"
"path/filepath"
2013-11-18 03:22:06 +00:00
"reflect"
"strconv"
"time"
)
2013-12-22 05:35:02 +00:00
// FileCacheItem is basic unit of file cache adapter.
// it contains data and expire time.
2013-11-18 03:22:06 +00:00
type FileCacheItem struct {
Data interface{}
2016-01-08 05:47:14 +00:00
Lastaccess time.Time
Expired time.Time
2013-11-18 03:22:06 +00:00
}
2015-09-08 16:15:03 +00:00
// FileCache Config
2013-11-18 03:22:06 +00:00
var (
2016-01-17 16:18:21 +00:00
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.
2013-11-18 03:22:06 +00:00
)
2013-12-22 05:35:02 +00:00
// FileCache is cache adapter for file storage.
2013-11-18 03:22:06 +00:00
type FileCache struct {
CachePath string
FileSuffix string
DirectoryLevel int
EmbedExpiry int
}
2015-09-08 16:15:03 +00:00
// NewFileCache Create new file cache with no config.
2013-12-22 05:35:02 +00:00
// the level and expiry need set in method StartAndGC as config string.
func NewFileCache() Cache {
2013-12-22 07:31:49 +00:00
// return &FileCache{CachePath:FileCachePath, FileSuffix:FileCacheFileSuffix}
return &FileCache{}
2013-11-18 03:22:06 +00:00
}
2015-09-08 16:15:03 +00:00
// StartAndGC will start and begin gc for file cache.
2019-04-05 14:15:53 +00:00
// the config need to be like {CachePath:"/cache","FileSuffix":".bin","DirectoryLevel":"2","EmbedExpiry":"0"}
2014-07-10 05:28:54 +00:00
func (fc *FileCache) StartAndGC(config string) error {
2013-11-18 03:22:06 +00:00
2019-01-25 11:08:39 +00:00
cfg := make(map[string]string)
2019-04-05 14:15:53 +00:00
err := json.Unmarshal([]byte(config), &cfg)
if err != nil {
return err
}
2013-11-18 03:22:06 +00:00
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 {
2016-01-08 05:47:14 +00:00
cfg["EmbedExpiry"] = strconv.FormatInt(int64(FileCacheEmbedExpiry.Seconds()), 10)
2013-11-18 03:22:06 +00:00
}
2014-07-10 05:28:54 +00:00
fc.CachePath = cfg["CachePath"]
fc.FileSuffix = cfg["FileSuffix"]
fc.DirectoryLevel, _ = strconv.Atoi(cfg["DirectoryLevel"])
fc.EmbedExpiry, _ = strconv.Atoi(cfg["EmbedExpiry"])
2013-11-18 03:22:06 +00:00
2014-07-10 05:28:54 +00:00
fc.Init()
2013-11-18 03:22:06 +00:00
return nil
}
2013-12-22 05:35:02 +00:00
// Init will make new dir for file cache if not exist.
2014-07-10 05:28:54 +00:00
func (fc *FileCache) Init() {
if ok, _ := exists(fc.CachePath); !ok { // todo : error handle
_ = os.MkdirAll(fc.CachePath, os.ModePerm) // todo : error handle
2013-11-18 03:22:06 +00:00
}
}
2013-12-22 05:35:02 +00:00
// get cached file name. it's md5 encoded.
2014-07-10 05:28:54 +00:00
func (fc *FileCache) getCacheFileName(key string) string {
2013-11-18 03:22:06 +00:00
m := md5.New()
io.WriteString(m, key)
keyMd5 := hex.EncodeToString(m.Sum(nil))
2014-07-10 05:28:54 +00:00
cachePath := fc.CachePath
switch fc.DirectoryLevel {
2013-11-18 03:22:06 +00:00
case 2:
cachePath = filepath.Join(cachePath, keyMd5[0:2], keyMd5[2:4])
2013-11-18 03:22:06 +00:00
case 1:
cachePath = filepath.Join(cachePath, keyMd5[0:2])
2013-11-18 03:22:06 +00:00
}
2014-07-10 05:28:54 +00:00
if ok, _ := exists(cachePath); !ok { // todo : error handle
_ = os.MkdirAll(cachePath, os.ModePerm) // todo : error handle
2013-11-18 03:22:06 +00:00
}
2014-07-10 05:28:54 +00:00
return filepath.Join(cachePath, fmt.Sprintf("%s%s", keyMd5, fc.FileSuffix))
2013-11-18 03:22:06 +00:00
}
2013-12-22 05:35:02 +00:00
// Get value from file cache.
// if non-exist or expired, return empty string.
2014-07-10 05:28:54 +00:00
func (fc *FileCache) Get(key string) interface{} {
2015-09-08 16:15:03 +00:00
fileData, err := FileGetContents(fc.getCacheFileName(key))
2013-11-18 03:22:06 +00:00
if err != nil {
return ""
}
var to FileCacheItem
2015-09-08 16:15:03 +00:00
GobDecode(fileData, &to)
2016-01-08 05:47:14 +00:00
if to.Expired.Before(time.Now()) {
2013-11-18 03:22:06 +00:00
return ""
}
return to.Data
}
// GetMulti gets values from file cache.
// if non-exist or expired, return empty string.
func (fc *FileCache) GetMulti(keys []string) []interface{} {
var rc []interface{}
for _, key := range keys {
rc = append(rc, fc.Get(key))
}
return rc
}
2013-12-22 05:35:02 +00:00
// Put value into file cache.
2013-12-22 07:31:49 +00:00
// timeout means how long to keep this file, unit of ms.
2019-04-05 14:15:53 +00:00
// if timeout equals fc.EmbedExpiry(default is 0), cache this item forever.
2016-01-08 05:47:14 +00:00
func (fc *FileCache) Put(key string, val interface{}, timeout time.Duration) error {
gob.Register(val)
2014-07-10 05:38:05 +00:00
item := FileCacheItem{Data: val}
2019-04-05 14:15:53 +00:00
if timeout == time.Duration(fc.EmbedExpiry) {
item.Expired = time.Now().Add((86400 * 365 * 10) * time.Second) // ten years
2013-11-18 03:22:06 +00:00
} else {
2016-01-08 05:47:14 +00:00
item.Expired = time.Now().Add(timeout)
2013-11-18 03:22:06 +00:00
}
2016-01-08 05:47:14 +00:00
item.Lastaccess = time.Now()
2015-09-08 16:15:03 +00:00
data, err := GobEncode(item)
2013-11-18 03:22:06 +00:00
if err != nil {
return err
}
2015-09-08 16:15:03 +00:00
return FilePutContents(fc.getCacheFileName(key), data)
2013-11-18 03:22:06 +00:00
}
2013-12-22 05:35:02 +00:00
// Delete file cache value.
2014-07-10 05:28:54 +00:00
func (fc *FileCache) Delete(key string) error {
filename := fc.getCacheFileName(key)
2013-11-18 03:22:06 +00:00
if ok, _ := exists(filename); ok {
return os.Remove(filename)
}
return nil
}
2015-09-08 16:15:03 +00:00
// Incr will increase cached int value.
2014-07-10 05:28:54 +00:00
// fc value is saving forever unless Delete.
func (fc *FileCache) Incr(key string) error {
data := fc.Get(key)
2013-11-18 03:22:06 +00:00
var incr int
if reflect.TypeOf(data).Name() != "int" {
incr = 0
} else {
2014-07-10 05:38:05 +00:00
incr = data.(int) + 1
2013-11-18 03:22:06 +00:00
}
2019-04-05 14:15:53 +00:00
fc.Put(key, incr, time.Duration(fc.EmbedExpiry))
2013-11-18 03:22:06 +00:00
return nil
}
2015-09-08 16:15:03 +00:00
// Decr will decrease cached int value.
2014-07-10 05:28:54 +00:00
func (fc *FileCache) Decr(key string) error {
data := fc.Get(key)
2013-11-18 03:22:06 +00:00
var decr int
if reflect.TypeOf(data).Name() != "int" || data.(int)-1 <= 0 {
decr = 0
} else {
2014-07-10 05:38:05 +00:00
decr = data.(int) - 1
2013-11-18 03:22:06 +00:00
}
2019-04-05 14:15:53 +00:00
fc.Put(key, decr, time.Duration(fc.EmbedExpiry))
2013-11-18 03:22:06 +00:00
return nil
}
2015-09-08 16:15:03 +00:00
// IsExist check value is exist.
2014-07-10 05:28:54 +00:00
func (fc *FileCache) IsExist(key string) bool {
ret, _ := exists(fc.getCacheFileName(key))
2013-11-18 03:22:06 +00:00
return ret
}
2015-09-08 16:15:03 +00:00
// ClearAll will clean cached files.
2013-12-22 05:35:02 +00:00
// not implemented.
2014-07-10 05:28:54 +00:00
func (fc *FileCache) ClearAll() error {
2013-11-18 03:22:06 +00:00
return nil
}
2013-12-22 05:35:02 +00:00
// check file exist.
2013-11-18 03:22:06 +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
}
2015-09-08 16:15:03 +00:00
// FileGetContents Get bytes to file.
2013-12-22 05:35:02 +00:00
// if non-exist, create this file.
2015-09-08 16:15:03 +00:00
func FileGetContents(filename string) (data []byte, e error) {
return ioutil.ReadFile(filename)
2013-11-18 03:22:06 +00:00
}
2015-09-08 16:15:03 +00:00
// FilePutContents Put bytes to file.
2013-12-22 05:35:02 +00:00
// if non-exist, create this file.
2015-09-08 16:15:03 +00:00
func FilePutContents(filename string, content []byte) error {
return ioutil.WriteFile(filename, content, os.ModePerm)
2013-11-18 03:22:06 +00:00
}
2013-12-22 05:35:02 +00:00
2015-09-08 16:15:03 +00:00
// GobEncode Gob encodes file cache item.
func GobEncode(data interface{}) ([]byte, error) {
2013-11-18 03:22:06 +00:00
buf := bytes.NewBuffer(nil)
enc := gob.NewEncoder(buf)
err := enc.Encode(data)
if err != nil {
return nil, err
}
return buf.Bytes(), err
}
2015-09-08 16:15:03 +00:00
// GobDecode Gob decodes file cache item.
func GobDecode(data []byte, to *FileCacheItem) error {
2013-11-18 03:22:06 +00:00
buf := bytes.NewBuffer(data)
dec := gob.NewDecoder(buf)
return dec.Decode(&to)
}
func init() {
Register("file", NewFileCache)
}