gf/os/gfile/gfile_cache.go

88 lines
2.7 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). 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,
// You can obtain one at https://github.com/gogf/gf.
2018-08-21 21:18:56 +08:00
package gfile
2018-08-21 21:18:56 +08:00
import (
"context"
2021-11-15 20:26:31 +08:00
"time"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/internal/command"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/internal/intlog"
"github.com/gogf/gf/v2/os/gcache"
"github.com/gogf/gf/v2/os/gfsnotify"
2018-08-21 21:18:56 +08:00
)
const (
defaultCacheDuration = "1m" // defaultCacheExpire is the expire time for file content caching in seconds.
commandEnvKeyForCache = "gf.gfile.cache" // commandEnvKeyForCache is the configuration key for command argument or environment configuring cache expire duration.
2018-08-21 21:18:56 +08:00
)
var (
// Default expire time for file content caching.
cacheDuration = getCacheDuration()
// internalCache is the memory cache for internal usage.
internalCache = gcache.New()
2018-08-21 21:18:56 +08:00
)
func getCacheDuration() time.Duration {
cacheDurationConfigured := command.GetOptWithEnv(commandEnvKeyForCache, defaultCacheDuration)
d, err := time.ParseDuration(cacheDurationConfigured)
if err != nil {
panic(gerror.WrapCodef(
gcode.CodeInvalidConfiguration,
err,
`error parsing string "%s" to time duration`,
cacheDurationConfigured,
))
}
return d
}
// GetContentsWithCache 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.
func GetContentsWithCache(path string, duration ...time.Duration) string {
return string(GetBytesWithCache(path, duration...))
2018-08-21 21:18:56 +08:00
}
// GetBytesWithCache 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.
func GetBytesWithCache(path string, duration ...time.Duration) []byte {
var (
ctx = context.Background()
expire = cacheDuration
cacheKey = commandEnvKeyForCache + path
)
if len(duration) > 0 {
expire = duration[0]
2019-06-19 09:06:52 +08:00
}
r, _ := internalCache.GetOrSetFuncLock(ctx, cacheKey, func(ctx context.Context) (interface{}, error) {
b := GetBytes(path)
2019-06-19 09:06:52 +08:00
if b != nil {
// Adding this `path` to gfsnotify,
2019-06-19 09:06:52 +08:00
// it will clear its cache if there's any changes of the file.
_, _ = gfsnotify.Add(path, func(event *gfsnotify.Event) {
_, err := internalCache.Remove(ctx, cacheKey)
if err != nil {
intlog.Errorf(ctx, `%+v`, err)
}
2019-06-19 09:06:52 +08:00
gfsnotify.Exit()
})
}
return b, nil
}, expire)
2019-06-19 09:06:52 +08:00
if r != nil {
return r.Bytes()
2019-06-19 09:06:52 +08:00
}
return nil
2018-08-21 21:18:56 +08:00
}