1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-24 01:00:50 +00:00
Beego/cache/file.go

282 lines
6.9 KiB
Go
Raw Normal View History

2014-04-12 05:18:18 +00:00
// Beego (http://beego.me/)
// @description beego is an open-source, high-performance web framework for the Go programming language.
// @link http://github.com/astaxie/beego for the canonical source repository
// @license http://github.com/astaxie/beego/blob/master/LICENSE
// @authors astaxie
2013-11-18 03:22:06 +00:00
package cache
import (
"bytes"
"crypto/md5"
"encoding/gob"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
2013-11-18 03:22:06 +00:00
"reflect"
"strconv"
"time"
)
func init() {
Register("file", NewFileCache())
}
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{}
Lastaccess int64
Expired int64
}
var (
2013-12-22 05:35:02 +00:00
FileCachePath string = "cache" // cache directory
2013-12-24 13:59:00 +00:00
FileCacheFileSuffix string = ".bin" // cache file suffix
FileCacheDirectoryLevel int = 2 // cache file deep level if auto generated cache files.
FileCacheEmbedExpiry int64 = 0 // 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
}
2013-12-22 07:31:49 +00:00
// 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.
2013-11-18 03:22:06 +00:00
func NewFileCache() *FileCache {
2013-12-22 07:31:49 +00:00
// return &FileCache{CachePath:FileCachePath, FileSuffix:FileCacheFileSuffix}
return &FileCache{}
2013-11-18 03:22:06 +00:00
}
2013-12-22 05:35:02 +00:00
// Start and begin gc for file cache.
// the config need to be like {CachePath:"/cache","FileSuffix":".bin","DirectoryLevel":2,"EmbedExpiry":0}
2013-11-18 03:22:06 +00:00
func (this *FileCache) StartAndGC(config string) error {
var cfg map[string]string
json.Unmarshal([]byte(config), &cfg)
//fmt.Println(cfg)
2014-01-01 12:50:06 +00:00
//fmt.Println(config)
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 {
cfg["EmbedExpiry"] = strconv.FormatInt(FileCacheEmbedExpiry, 10)
}
this.CachePath = cfg["CachePath"]
this.FileSuffix = cfg["FileSuffix"]
this.DirectoryLevel, _ = strconv.Atoi(cfg["DirectoryLevel"])
this.EmbedExpiry, _ = strconv.Atoi(cfg["EmbedExpiry"])
this.Init()
return nil
}
2013-12-22 05:35:02 +00:00
// Init will make new dir for file cache if not exist.
2013-11-18 03:22:06 +00:00
func (this *FileCache) Init() {
app := filepath.Dir(os.Args[0])
this.CachePath = filepath.Join(app, this.CachePath)
2013-11-18 03:22:06 +00:00
ok, err := exists(this.CachePath)
if err != nil { // print error
//fmt.Println(err)
}
if !ok {
if err = os.Mkdir(this.CachePath, os.ModePerm); err != nil {
//fmt.Println(err);
}
}
//fmt.Println(this.getCacheFileName("123456"));
}
2013-12-22 05:35:02 +00:00
// get cached file name. it's md5 encoded.
2013-11-18 03:22:06 +00:00
func (this *FileCache) getCacheFileName(key string) string {
m := md5.New()
io.WriteString(m, key)
keyMd5 := hex.EncodeToString(m.Sum(nil))
cachePath := this.CachePath
//fmt.Println("cachepath : " , cachePath)
//fmt.Println("md5" , keyMd5);
switch this.DirectoryLevel {
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
}
ok, err := exists(cachePath)
if err != nil {
//fmt.Println(err)
}
if !ok {
if err = os.MkdirAll(cachePath, os.ModePerm); err != nil {
//fmt.Println(err);
}
}
return filepath.Join(cachePath, fmt.Sprintf("%s%s", keyMd5, this.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.
2013-11-18 03:22:06 +00:00
func (this *FileCache) Get(key string) interface{} {
filename := this.getCacheFileName(key)
filedata, err := File_get_contents(filename)
//fmt.Println("get length:" , len(filedata));
if err != nil {
return ""
}
var to FileCacheItem
2014-01-01 12:50:06 +00:00
Gob_decode(filedata, &to)
2013-11-18 03:22:06 +00:00
if to.Expired < time.Now().Unix() {
return ""
}
return to.Data
}
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.
// if timeout equals FileCacheEmbedExpiry(default is 0), cache this item forever.
2013-11-18 03:22:06 +00:00
func (this *FileCache) Put(key string, val interface{}, timeout int64) error {
gob.Register(val)
2013-11-18 03:22:06 +00:00
filename := this.getCacheFileName(key)
var item FileCacheItem
item.Data = val
if timeout == FileCacheEmbedExpiry {
item.Expired = time.Now().Unix() + (86400 * 365 * 10) // ten years
2013-11-18 03:22:06 +00:00
} else {
item.Expired = time.Now().Unix() + timeout
}
item.Lastaccess = time.Now().Unix()
data, err := Gob_encode(item)
if err != nil {
return err
}
err = File_put_contents(filename, data)
return err
}
2013-12-22 05:35:02 +00:00
// Delete file cache value.
2013-11-18 03:22:06 +00:00
func (this *FileCache) Delete(key string) error {
filename := this.getCacheFileName(key)
if ok, _ := exists(filename); ok {
return os.Remove(filename)
}
return nil
}
2013-12-22 05:35:02 +00:00
// Increase cached int value.
// this value is saving forever unless Delete.
2013-11-18 03:22:06 +00:00
func (this *FileCache) Incr(key string) error {
data := this.Get(key)
var incr int
2014-01-01 12:50:06 +00:00
//fmt.Println(reflect.TypeOf(data).Name())
2013-11-18 03:22:06 +00:00
if reflect.TypeOf(data).Name() != "int" {
incr = 0
} else {
incr = data.(int) + 1
}
this.Put(key, incr, FileCacheEmbedExpiry)
return nil
}
2013-12-22 05:35:02 +00:00
// Decrease cached int value.
2013-11-18 03:22:06 +00:00
func (this *FileCache) Decr(key string) error {
data := this.Get(key)
var decr int
if reflect.TypeOf(data).Name() != "int" || data.(int)-1 <= 0 {
decr = 0
} else {
decr = data.(int) - 1
}
this.Put(key, decr, FileCacheEmbedExpiry)
return nil
}
2013-12-22 05:35:02 +00:00
// Check value is exist.
2013-11-18 03:22:06 +00:00
func (this *FileCache) IsExist(key string) bool {
filename := this.getCacheFileName(key)
ret, _ := exists(filename)
return ret
}
2013-12-22 05:35:02 +00:00
// Clean cached files.
// not implemented.
2013-11-18 03:22:06 +00:00
func (this *FileCache) ClearAll() error {
2014-01-01 12:50:06 +00:00
//this.CachePath
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
}
2013-12-22 05:35:02 +00:00
// Get bytes to file.
// if non-exist, create this file.
func File_get_contents(filename string) ([]byte, error) {
2013-11-18 03:22:06 +00:00
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm)
if err != nil {
return []byte(""), err
}
defer f.Close()
stat, err := f.Stat()
if err != nil {
return []byte(""), err
}
data := make([]byte, stat.Size())
result, err := f.Read(data)
if int64(result) == stat.Size() {
return data, err
}
return []byte(""), err
}
2013-12-22 05:35:02 +00:00
// Put bytes to file.
// if non-exist, create this file.
2013-11-18 03:22:06 +00:00
func File_put_contents(filename string, content []byte) error {
fp, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm)
if err != nil {
return err
}
defer fp.Close()
_, err = fp.Write(content)
return err
}
2013-12-22 05:35:02 +00:00
// Gob encodes file cache item.
2013-11-18 03:22:06 +00:00
func Gob_encode(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
}
2013-12-22 05:35:02 +00:00
// Gob decodes file cache item.
2014-01-01 12:50:06 +00:00
func Gob_decode(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)
}