mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 13:10:54 +00:00
code style simplify
This commit is contained in:
parent
007db805be
commit
8683ee7c67
126
cache/file.go
vendored
126
cache/file.go
vendored
@ -60,12 +60,10 @@ func NewFileCache() *FileCache {
|
|||||||
|
|
||||||
// Start and begin gc for file cache.
|
// Start and begin gc for file cache.
|
||||||
// the config need to be like {CachePath:"/cache","FileSuffix":".bin","DirectoryLevel":2,"EmbedExpiry":0}
|
// the config need to be like {CachePath:"/cache","FileSuffix":".bin","DirectoryLevel":2,"EmbedExpiry":0}
|
||||||
func (this *FileCache) StartAndGC(config string) error {
|
func (fc *FileCache) StartAndGC(config string) error {
|
||||||
|
|
||||||
var cfg map[string]string
|
var cfg map[string]string
|
||||||
json.Unmarshal([]byte(config), &cfg)
|
json.Unmarshal([]byte(config), &cfg)
|
||||||
//fmt.Println(cfg)
|
|
||||||
//fmt.Println(config)
|
|
||||||
if _, ok := cfg["CachePath"]; !ok {
|
if _, ok := cfg["CachePath"]; !ok {
|
||||||
cfg["CachePath"] = FileCachePath
|
cfg["CachePath"] = FileCachePath
|
||||||
}
|
}
|
||||||
@ -78,69 +76,53 @@ func (this *FileCache) StartAndGC(config string) error {
|
|||||||
if _, ok := cfg["EmbedExpiry"]; !ok {
|
if _, ok := cfg["EmbedExpiry"]; !ok {
|
||||||
cfg["EmbedExpiry"] = strconv.FormatInt(FileCacheEmbedExpiry, 10)
|
cfg["EmbedExpiry"] = strconv.FormatInt(FileCacheEmbedExpiry, 10)
|
||||||
}
|
}
|
||||||
this.CachePath = cfg["CachePath"]
|
fc.CachePath = cfg["CachePath"]
|
||||||
this.FileSuffix = cfg["FileSuffix"]
|
fc.FileSuffix = cfg["FileSuffix"]
|
||||||
this.DirectoryLevel, _ = strconv.Atoi(cfg["DirectoryLevel"])
|
fc.DirectoryLevel, _ = strconv.Atoi(cfg["DirectoryLevel"])
|
||||||
this.EmbedExpiry, _ = strconv.Atoi(cfg["EmbedExpiry"])
|
fc.EmbedExpiry, _ = strconv.Atoi(cfg["EmbedExpiry"])
|
||||||
|
|
||||||
this.Init()
|
fc.Init()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init will make new dir for file cache if not exist.
|
// Init will make new dir for file cache if not exist.
|
||||||
func (this *FileCache) Init() {
|
func (fc *FileCache) Init() {
|
||||||
app := filepath.Dir(os.Args[0])
|
app := filepath.Dir(os.Args[0])
|
||||||
this.CachePath = filepath.Join(app, this.CachePath)
|
fc.CachePath = filepath.Join(app, fc.CachePath)
|
||||||
ok, err := exists(this.CachePath)
|
if ok, _ := exists(fc.CachePath); !ok { // todo : error handle
|
||||||
if err != nil { // print error
|
_ = os.MkdirAll(fc.CachePath, os.ModePerm) // todo : error handle
|
||||||
//fmt.Println(err)
|
|
||||||
}
|
}
|
||||||
if !ok {
|
|
||||||
if err = os.Mkdir(this.CachePath, os.ModePerm); err != nil {
|
|
||||||
//fmt.Println(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//fmt.Println(this.getCacheFileName("123456"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// get cached file name. it's md5 encoded.
|
// get cached file name. it's md5 encoded.
|
||||||
func (this *FileCache) getCacheFileName(key string) string {
|
func (fc *FileCache) getCacheFileName(key string) string {
|
||||||
m := md5.New()
|
m := md5.New()
|
||||||
io.WriteString(m, key)
|
io.WriteString(m, key)
|
||||||
keyMd5 := hex.EncodeToString(m.Sum(nil))
|
keyMd5 := hex.EncodeToString(m.Sum(nil))
|
||||||
cachePath := this.CachePath
|
cachePath := fc.CachePath
|
||||||
//fmt.Println("cachepath : " , cachePath)
|
switch fc.DirectoryLevel {
|
||||||
//fmt.Println("md5" , keyMd5);
|
|
||||||
switch this.DirectoryLevel {
|
|
||||||
case 2:
|
case 2:
|
||||||
cachePath = filepath.Join(cachePath, keyMd5[0:2], keyMd5[2:4])
|
cachePath = filepath.Join(cachePath, keyMd5[0:2], keyMd5[2:4])
|
||||||
case 1:
|
case 1:
|
||||||
cachePath = filepath.Join(cachePath, keyMd5[0:2])
|
cachePath = filepath.Join(cachePath, keyMd5[0:2])
|
||||||
}
|
}
|
||||||
|
|
||||||
ok, err := exists(cachePath)
|
if ok, _ := exists(cachePath); !ok { // todo : error handle
|
||||||
if err != nil {
|
_ = os.MkdirAll(cachePath, os.ModePerm) // todo : error handle
|
||||||
//fmt.Println(err)
|
|
||||||
}
|
}
|
||||||
if !ok {
|
|
||||||
if err = os.MkdirAll(cachePath, os.ModePerm); err != nil {
|
return filepath.Join(cachePath, fmt.Sprintf("%s%s", keyMd5, fc.FileSuffix))
|
||||||
//fmt.Println(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return filepath.Join(cachePath, fmt.Sprintf("%s%s", keyMd5, this.FileSuffix))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get value from file cache.
|
// Get value from file cache.
|
||||||
// if non-exist or expired, return empty string.
|
// if non-exist or expired, return empty string.
|
||||||
func (this *FileCache) Get(key string) interface{} {
|
func (fc *FileCache) Get(key string) interface{} {
|
||||||
filename := this.getCacheFileName(key)
|
fileData, err := File_get_contents(fc.getCacheFileName(key))
|
||||||
filedata, err := File_get_contents(filename)
|
|
||||||
//fmt.Println("get length:" , len(filedata));
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
var to FileCacheItem
|
var to FileCacheItem
|
||||||
Gob_decode(filedata, &to)
|
Gob_decode(fileData, &to)
|
||||||
if to.Expired < time.Now().Unix() {
|
if to.Expired < time.Now().Unix() {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@ -150,29 +132,26 @@ func (this *FileCache) Get(key string) interface{} {
|
|||||||
// Put value into file cache.
|
// Put value into file cache.
|
||||||
// timeout means how long to keep this file, unit of ms.
|
// timeout means how long to keep this file, unit of ms.
|
||||||
// if timeout equals FileCacheEmbedExpiry(default is 0), cache this item forever.
|
// if timeout equals FileCacheEmbedExpiry(default is 0), cache this item forever.
|
||||||
func (this *FileCache) Put(key string, val interface{}, timeout int64) error {
|
func (fc *FileCache) Put(key string, val interface{}, timeout int64) error {
|
||||||
gob.Register(val)
|
gob.Register(val)
|
||||||
|
|
||||||
filename := this.getCacheFileName(key)
|
item := FileCacheItem{Data:val}
|
||||||
var item FileCacheItem
|
|
||||||
item.Data = val
|
|
||||||
if timeout == FileCacheEmbedExpiry {
|
if timeout == FileCacheEmbedExpiry {
|
||||||
item.Expired = time.Now().Unix() + (86400 * 365 * 10) // ten years
|
item.Expired = time.Now().Unix()+(86400*365*10) // ten years
|
||||||
} else {
|
} else {
|
||||||
item.Expired = time.Now().Unix() + timeout
|
item.Expired = time.Now().Unix()+timeout
|
||||||
}
|
}
|
||||||
item.Lastaccess = time.Now().Unix()
|
item.Lastaccess = time.Now().Unix()
|
||||||
data, err := Gob_encode(item)
|
data, err := Gob_encode(item)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = File_put_contents(filename, data)
|
return File_put_contents(fc.getCacheFileName(key), data)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete file cache value.
|
// Delete file cache value.
|
||||||
func (this *FileCache) Delete(key string) error {
|
func (fc *FileCache) Delete(key string) error {
|
||||||
filename := this.getCacheFileName(key)
|
filename := fc.getCacheFileName(key)
|
||||||
if ok, _ := exists(filename); ok {
|
if ok, _ := exists(filename); ok {
|
||||||
return os.Remove(filename)
|
return os.Remove(filename)
|
||||||
}
|
}
|
||||||
@ -180,44 +159,41 @@ func (this *FileCache) Delete(key string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Increase cached int value.
|
// Increase cached int value.
|
||||||
// this value is saving forever unless Delete.
|
// fc value is saving forever unless Delete.
|
||||||
func (this *FileCache) Incr(key string) error {
|
func (fc *FileCache) Incr(key string) error {
|
||||||
data := this.Get(key)
|
data := fc.Get(key)
|
||||||
var incr int
|
var incr int
|
||||||
//fmt.Println(reflect.TypeOf(data).Name())
|
|
||||||
if reflect.TypeOf(data).Name() != "int" {
|
if reflect.TypeOf(data).Name() != "int" {
|
||||||
incr = 0
|
incr = 0
|
||||||
} else {
|
} else {
|
||||||
incr = data.(int) + 1
|
incr = data.(int)+1
|
||||||
}
|
}
|
||||||
this.Put(key, incr, FileCacheEmbedExpiry)
|
fc.Put(key, incr, FileCacheEmbedExpiry)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decrease cached int value.
|
// Decrease cached int value.
|
||||||
func (this *FileCache) Decr(key string) error {
|
func (fc *FileCache) Decr(key string) error {
|
||||||
data := this.Get(key)
|
data := fc.Get(key)
|
||||||
var decr int
|
var decr int
|
||||||
if reflect.TypeOf(data).Name() != "int" || data.(int)-1 <= 0 {
|
if reflect.TypeOf(data).Name() != "int" || data.(int)-1 <= 0 {
|
||||||
decr = 0
|
decr = 0
|
||||||
} else {
|
} else {
|
||||||
decr = data.(int) - 1
|
decr = data.(int)-1
|
||||||
}
|
}
|
||||||
this.Put(key, decr, FileCacheEmbedExpiry)
|
fc.Put(key, decr, FileCacheEmbedExpiry)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check value is exist.
|
// Check value is exist.
|
||||||
func (this *FileCache) IsExist(key string) bool {
|
func (fc *FileCache) IsExist(key string) bool {
|
||||||
filename := this.getCacheFileName(key)
|
ret, _ := exists(fc.getCacheFileName(key))
|
||||||
ret, _ := exists(filename)
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean cached files.
|
// Clean cached files.
|
||||||
// not implemented.
|
// not implemented.
|
||||||
func (this *FileCache) ClearAll() error {
|
func (fc *FileCache) ClearAll() error {
|
||||||
//this.CachePath
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -235,22 +211,22 @@ func exists(path string) (bool, error) {
|
|||||||
|
|
||||||
// Get bytes to file.
|
// Get bytes to file.
|
||||||
// if non-exist, create this file.
|
// if non-exist, create this file.
|
||||||
func File_get_contents(filename string) ([]byte, error) {
|
func File_get_contents(filename string) (data []byte, e error) {
|
||||||
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm)
|
f, e := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm)
|
||||||
if err != nil {
|
if e != nil {
|
||||||
return []byte(""), err
|
return
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
stat, err := f.Stat()
|
stat, e := f.Stat()
|
||||||
if err != nil {
|
if e != nil {
|
||||||
return []byte(""), err
|
return
|
||||||
}
|
}
|
||||||
data := make([]byte, stat.Size())
|
data = make([]byte, stat.Size())
|
||||||
result, err := f.Read(data)
|
result, e := f.Read(data)
|
||||||
if int64(result) == stat.Size() {
|
if e != nil || int64(result) != stat.Size() {
|
||||||
return data, err
|
return nil, e
|
||||||
}
|
}
|
||||||
return []byte(""), err
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put bytes to file.
|
// Put bytes to file.
|
||||||
|
Loading…
Reference in New Issue
Block a user