2019-02-02 16:18:25 +08:00
|
|
|
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2018-08-21 21:18:56 +08:00
|
|
|
//
|
|
|
|
// 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,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-08-21 21:18:56 +08:00
|
|
|
|
2019-01-16 09:00:23 +08:00
|
|
|
// Package gfcache provides reading and caching for file contents.
|
2018-08-21 21:18:56 +08:00
|
|
|
package gfcache
|
|
|
|
|
|
|
|
import (
|
2019-07-09 13:19:28 +08:00
|
|
|
"time"
|
|
|
|
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/internal/cmdenv"
|
|
|
|
"github.com/gogf/gf/os/gcache"
|
|
|
|
"github.com/gogf/gf/os/gfile"
|
|
|
|
"github.com/gogf/gf/os/gfsnotify"
|
2018-08-21 21:18:56 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-06-19 09:06:52 +08:00
|
|
|
// Default expire time for file content caching in seconds.
|
2019-12-04 14:42:09 +08:00
|
|
|
gDEFAULT_CACHE_EXPIRE = time.Minute
|
2018-08-21 21:18:56 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-12-04 14:42:09 +08:00
|
|
|
// Default expire time for file content caching.
|
|
|
|
cacheExpire = cmdenv.Get("gf.gfcache.expire", gDEFAULT_CACHE_EXPIRE).Duration()
|
2018-08-21 21:18:56 +08:00
|
|
|
)
|
|
|
|
|
2019-06-11 20:57:43 +08:00
|
|
|
// GetContents returns string content of given file by <path> from cache.
|
|
|
|
// If there's no content in the cache, it will read it from disk file specified by <path>.
|
|
|
|
// The parameter <expire> specifies the caching time for this file content in seconds.
|
2019-12-04 14:42:09 +08:00
|
|
|
func GetContents(path string, duration ...time.Duration) string {
|
2019-07-09 13:19:28 +08:00
|
|
|
return string(GetBinContents(path, duration...))
|
2018-08-21 21:18:56 +08:00
|
|
|
}
|
|
|
|
|
2019-06-11 20:57:43 +08:00
|
|
|
// GetBinContents returns []byte content of given file by <path> from cache.
|
|
|
|
// If there's no content in the cache, it will read it from disk file specified by <path>.
|
|
|
|
// The parameter <expire> specifies the caching time for this file content in seconds.
|
2019-12-04 14:42:09 +08:00
|
|
|
func GetBinContents(path string, duration ...time.Duration) []byte {
|
|
|
|
key := cacheKey(path)
|
|
|
|
expire := cacheExpire
|
2019-07-09 13:19:28 +08:00
|
|
|
if len(duration) > 0 {
|
2019-12-04 14:42:09 +08:00
|
|
|
expire = duration[0]
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2019-12-04 14:42:09 +08:00
|
|
|
r := gcache.GetOrSetFuncLock(key, func() interface{} {
|
2019-09-06 16:59:38 +08:00
|
|
|
b := gfile.GetBytes(path)
|
2019-06-19 09:06:52 +08:00
|
|
|
if b != nil {
|
|
|
|
// Adding this <path> to gfsnotify,
|
|
|
|
// it will clear its cache if there's any changes of the file.
|
|
|
|
_, _ = gfsnotify.Add(path, func(event *gfsnotify.Event) {
|
2019-12-04 14:42:09 +08:00
|
|
|
gcache.Remove(key)
|
2019-06-19 09:06:52 +08:00
|
|
|
gfsnotify.Exit()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return b
|
2019-12-04 14:42:09 +08:00
|
|
|
}, expire)
|
2019-06-19 09:06:52 +08:00
|
|
|
if r != nil {
|
|
|
|
return r.([]byte)
|
|
|
|
}
|
|
|
|
return nil
|
2018-08-21 21:18:56 +08:00
|
|
|
}
|
|
|
|
|
2019-07-09 13:19:28 +08:00
|
|
|
// cacheKey produces the cache key for gcache.
|
2019-03-14 23:28:56 +08:00
|
|
|
func cacheKey(path string) string {
|
2019-06-19 09:06:52 +08:00
|
|
|
return "gf.gfcache:" + path
|
|
|
|
}
|