gf/g/os/gfcache/gfcache.go

61 lines
1.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// Package gfcache provides reading and caching for file contents.
//
// 文件缓存.
package gfcache
import (
"github.com/gogf/gf/g/internal/cmdenv"
"github.com/gogf/gf/g/os/gcache"
"github.com/gogf/gf/g/os/gfile"
"github.com/gogf/gf/g/os/gfsnotify"
)
const (
// 默认的缓存超时时间(60秒)
gDEFAULT_CACHE_EXPIRE = 60
)
var (
// 默认的缓存时间(秒)
cacheExpire = cmdenv.Get("gf.gfcache.expire", gDEFAULT_CACHE_EXPIRE).Int()*1000
)
// 获得文件内容 stringexpire参数为缓存过期时间单位为秒。
func GetContents(path string, expire...int) string {
return string(GetBinContents(path, expire...))
}
// 获得文件内容 []byteexpire参数为缓存过期时间单位为秒。
func GetBinContents(path string, expire...int) []byte {
k := cacheKey(path)
e := cacheExpire
if len(expire) > 0 {
e = expire[0]
}
r := gcache.GetOrSetFuncLock(k, func() interface{} {
b := gfile.GetBinContents(path)
if b != nil {
// 添加文件监控,如果文件有任何变化,立即清空缓存
gfsnotify.Add(path, func(event *gfsnotify.Event) {
gcache.Remove(k)
gfsnotify.Exit()
})
}
return b
}, e*1000)
if r != nil {
return r.([]byte)
}
return nil
}
// 生成缓存键名
func cacheKey(path string) string {
return "gf.gfcache:" + path
}