mirror of
https://gitee.com/johng/gf
synced 2026-07-04 13:02:36 +08:00
add file cache feature for package gfile and remove package gfcache
This commit is contained in:
@ -1,29 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/os/gfcache"
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := 0
|
||||
r := ""
|
||||
path := gfile.TempDir() + gfile.Separator + "temp"
|
||||
gfile.PutContents(path, "hello")
|
||||
|
||||
s = gfcache.GetSize()
|
||||
r = gfcache.GetContents(path)
|
||||
fmt.Println(s, r)
|
||||
|
||||
gfile.PutContentsAppend(path, " john")
|
||||
|
||||
// 等待1秒以便gfsnotify回调能处理完成
|
||||
time.Sleep(time.Second)
|
||||
|
||||
s = gfcache.GetSize()
|
||||
r = gfcache.GetContents(path)
|
||||
fmt.Println(s, r)
|
||||
}
|
||||
@ -18,7 +18,6 @@ import (
|
||||
"github.com/gogf/gf/encoding/gxml"
|
||||
"github.com/gogf/gf/encoding/gyaml"
|
||||
"github.com/gogf/gf/internal/rwmutex"
|
||||
"github.com/gogf/gf/os/gfcache"
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
"github.com/gogf/gf/text/gregex"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
@ -99,7 +98,7 @@ func Load(path string, safe ...bool) (*Json, error) {
|
||||
} else {
|
||||
path = p
|
||||
}
|
||||
return doLoadContent(gfile.Ext(path), gfcache.GetBinContents(path), safe...)
|
||||
return doLoadContent(gfile.Ext(path), gfile.GetBytesWithCache(path), safe...)
|
||||
}
|
||||
|
||||
// LoadJson creates a Json object from given JSON format content.
|
||||
|
||||
@ -1,19 +1,16 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright 2017-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
|
||||
package gfile
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/internal/cmdenv"
|
||||
"github.com/gogf/gf/os/gcache"
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
"github.com/gogf/gf/os/gfsnotify"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -29,21 +26,21 @@ var (
|
||||
// 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.
|
||||
func GetContents(path string, duration ...time.Duration) string {
|
||||
return string(GetBinContents(path, duration...))
|
||||
func GetContentsWithCache(path string, duration ...time.Duration) string {
|
||||
return string(GetBytesWithCache(path, duration...))
|
||||
}
|
||||
|
||||
// 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.
|
||||
func GetBinContents(path string, duration ...time.Duration) []byte {
|
||||
func GetBytesWithCache(path string, duration ...time.Duration) []byte {
|
||||
key := cacheKey(path)
|
||||
expire := cacheExpire
|
||||
if len(duration) > 0 {
|
||||
expire = duration[0]
|
||||
}
|
||||
r := gcache.GetOrSetFuncLock(key, func() interface{} {
|
||||
b := gfile.GetBytes(path)
|
||||
b := GetBytes(path)
|
||||
if b != nil {
|
||||
// Adding this <path> to gfsnotify,
|
||||
// it will clear its cache if there's any changes of the file.
|
||||
20
os/gfcache/gfcache_z_unit_test.go → os/gfile/gfile_z_cache_test.go
Executable file → Normal file
20
os/gfcache/gfcache_z_unit_test.go → os/gfile/gfile_z_cache_test.go
Executable file → Normal file
@ -1,27 +1,22 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright 2020 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.
|
||||
|
||||
// go test *.go -bench=".*" -benchmem
|
||||
|
||||
package gfcache_test
|
||||
package gfile_test
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/os/gfcache"
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
)
|
||||
|
||||
func TestGetContents(t *testing.T) {
|
||||
func Test_GetContentsWithCache(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
|
||||
var f *os.File
|
||||
var err error
|
||||
fileName := "test"
|
||||
@ -38,7 +33,6 @@ func TestGetContents(t *testing.T) {
|
||||
defer os.Remove(f.Name())
|
||||
|
||||
if gfile.Exists(f.Name()) {
|
||||
|
||||
f, err = gfile.OpenFile(f.Name(), os.O_APPEND|os.O_WRONLY, os.ModeAppend)
|
||||
if err != nil {
|
||||
t.Error("file open fail", err)
|
||||
@ -49,7 +43,7 @@ func TestGetContents(t *testing.T) {
|
||||
t.Error("write error", err)
|
||||
}
|
||||
|
||||
cache := gfcache.GetContents(f.Name(), 1)
|
||||
cache := gfile.GetContentsWithCache(f.Name(), 1)
|
||||
t.Assert(cache, strTest)
|
||||
}
|
||||
})
|
||||
@ -72,7 +66,7 @@ func TestGetContents(t *testing.T) {
|
||||
defer os.Remove(f.Name())
|
||||
|
||||
if gfile.Exists(f.Name()) {
|
||||
cache := gfcache.GetContents(f.Name())
|
||||
cache := gfile.GetContentsWithCache(f.Name())
|
||||
|
||||
f, err = gfile.OpenFile(f.Name(), os.O_APPEND|os.O_WRONLY, os.ModeAppend)
|
||||
if err != nil {
|
||||
@ -73,7 +73,7 @@ func getConnByPid(pid int) (*gtcp.PoolConn, error) {
|
||||
// It returns 0 if no port found for the specified pid.
|
||||
func getPortByPid(pid int) int {
|
||||
path := getCommFilePath(pid)
|
||||
content := gfcache.GetContents(path)
|
||||
content := gfile.GetContentsWithCache(path)
|
||||
return gconv.Int(content)
|
||||
}
|
||||
|
||||
|
||||
@ -68,7 +68,7 @@ func (view *View) Parse(file string, params ...Params) (result string, err error
|
||||
if resource != nil {
|
||||
content = gconv.UnsafeBytesToStr(resource.Content())
|
||||
} else {
|
||||
content = gfcache.GetContents(path)
|
||||
content = gfile.GetContentsWithCache(path)
|
||||
}
|
||||
// Monitor template files changes using fsnotify asynchronously.
|
||||
if resource == nil {
|
||||
|
||||
Reference in New Issue
Block a user