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"
"github.com/gogf/gf/internal/intlog"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/os/gcache"
2020-12-04 23:44:54 +08:00
"github.com/gogf/gf/os/gcmd"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/os/gfsnotify"
2020-07-16 12:31:13 +08:00
"time"
2018-08-21 21:18:56 +08:00
)
const (
2021-06-26 12:08:18 +08:00
defaultCacheExpire = time . Minute // 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 (
2019-12-04 14:42:09 +08:00
// Default expire time for file content caching.
2021-06-26 12:08:18 +08:00
cacheExpire = gcmd . GetOptWithEnv ( commandEnvKeyForCache , defaultCacheExpire ) . Duration ( )
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
)
2021-06-26 12:08:18 +08:00
// GetContentsWithCache returns string content of given file by <path> from cache.
2019-06-11 20:57:43 +08:00
// 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-09-01 15:02:58 +08:00
// GetBytesWithCache returns []byte content of given file by <path> from cache.
2019-06-11 20:57:43 +08:00
// 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 ( )
expire = cacheExpire
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-09-17 10:48:08 +08:00
r , _ := internalCache . GetOrSetFuncLock ( ctx , cacheKey , func ( ) ( interface { } , error ) {
2020-07-16 12:31:13 +08:00
b := 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 ) {
2021-09-17 10:48:08 +08:00
_ , err := internalCache . Remove ( ctx , cacheKey )
if err != nil {
intlog . Error ( ctx , err )
}
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
}