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,
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
2020-07-16 12:31:13 +08:00
package gfile
2018-08-21 21:18:56 +08:00
import (
2021-09-17 10:48:08 +08:00
"context"
2021-11-15 20:26:31 +08:00
"time"
2022-06-16 17:07:25 +08:00
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
2021-11-23 20:26:55 +08:00
"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 (
2022-06-16 17:07:25 +08:00
defaultCacheDuration = "1m" // defaultCacheExpire is the expire time for file content caching in seconds.
2021-06-26 12:08:18 +08:00
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 (
2019-12-04 14:42:09 +08:00
// Default expire time for file content caching.
2022-06-16 17:07:25 +08:00
cacheDuration = getCacheDuration ( )
2020-11-07 20:00:35 +08:00
// internalCache is the memory cache for internal usage.
internalCache = gcache . New ( )
2018-08-21 21:18:56 +08:00
)
2022-06-16 17:07:25 +08:00
func getCacheDuration ( ) time . Duration {
cacheDurationConfigured := command . GetOptWithEnv ( commandEnvKeyForCache , defaultCacheDuration )
d , err := time . ParseDuration ( cacheDurationConfigured )
2021-11-23 20:26:55 +08:00
if err != nil {
2022-06-16 17:07:25 +08:00
panic ( gerror . WrapCodef (
gcode . CodeInvalidConfiguration ,
err ,
` error parsing string "%s" to time duration ` ,
cacheDurationConfigured ,
) )
2021-11-23 20:26:55 +08:00
}
return d
}
2021-10-21 18:22:47 +08:00
// 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.
2020-07-16 12:31:13 +08:00
func GetContentsWithCache ( path string , duration ... time . Duration ) string {
return string ( GetBytesWithCache ( path , duration ... ) )
2018-08-21 21:18:56 +08:00
}
2021-10-21 18:22:47 +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.
2020-07-16 12:31:13 +08:00
func GetBytesWithCache ( path string , duration ... time . Duration ) [ ] byte {
2021-09-17 10:48:08 +08:00
var (
ctx = context . Background ( )
2022-06-16 17:07:25 +08:00
expire = cacheDuration
2021-09-17 10:48:08 +08:00
cacheKey = commandEnvKeyForCache + path
)
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
}
2021-12-17 11:46:05 +08:00
r , _ := internalCache . GetOrSetFuncLock ( ctx , cacheKey , func ( ctx context . Context ) ( interface { } , error ) {
2020-07-16 12:31:13 +08:00
b := GetBytes ( path )
2019-06-19 09:06:52 +08:00
if b != nil {
2021-10-21 18:22:47 +08:00
// 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 ) {
2021-09-17 10:48:08 +08:00
_ , err := internalCache . Remove ( ctx , cacheKey )
if err != nil {
2022-01-28 14:51:49 +08:00
intlog . Errorf ( ctx , ` %+v ` , err )
2021-09-17 10:48:08 +08:00
}
2019-06-19 09:06:52 +08:00
gfsnotify . Exit ( )
} )
}
2020-10-09 20:59:49 +08:00
return b , nil
2019-12-04 14:42:09 +08:00
} , expire )
2019-06-19 09:06:52 +08:00
if r != nil {
2021-09-16 22:13:00 +08:00
return r . Bytes ( )
2019-06-19 09:06:52 +08:00
}
return nil
2018-08-21 21:18:56 +08:00
}