From 4dd12434b7b9b9d5237a27f675bba3dcde8a9b35 Mon Sep 17 00:00:00 2001 From: john Date: Thu, 16 Jul 2020 12:31:13 +0800 Subject: [PATCH] add file cache feature for package gfile and remove package gfcache --- .example/os/gfcache/gfcache.go | 29 ------------------- encoding/gjson/gjson_api_new_load.go | 3 +- .../gfcache.go => gfile/gfile_cache.go} | 17 +++++------ .../gfile_z_cache_test.go} | 20 +++++-------- os/gproc/gproc_comm.go | 2 +- os/gview/gview_parse.go | 2 +- 6 files changed, 17 insertions(+), 56 deletions(-) delete mode 100644 .example/os/gfcache/gfcache.go rename os/{gfcache/gfcache.go => gfile/gfile_cache.go} (80%) rename os/{gfcache/gfcache_z_unit_test.go => gfile/gfile_z_cache_test.go} (84%) mode change 100755 => 100644 diff --git a/.example/os/gfcache/gfcache.go b/.example/os/gfcache/gfcache.go deleted file mode 100644 index d66d345ca..000000000 --- a/.example/os/gfcache/gfcache.go +++ /dev/null @@ -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) -} diff --git a/encoding/gjson/gjson_api_new_load.go b/encoding/gjson/gjson_api_new_load.go index 6abce4b5d..9caf40081 100644 --- a/encoding/gjson/gjson_api_new_load.go +++ b/encoding/gjson/gjson_api_new_load.go @@ -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. diff --git a/os/gfcache/gfcache.go b/os/gfile/gfile_cache.go similarity index 80% rename from os/gfcache/gfcache.go rename to os/gfile/gfile_cache.go index 962f15adb..c34b42659 100644 --- a/os/gfcache/gfcache.go +++ b/os/gfile/gfile_cache.go @@ -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 from cache. // If there's no content in the cache, it will read it from disk file specified by . // The parameter 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 from cache. // If there's no content in the cache, it will read it from disk file specified by . // The parameter 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 to gfsnotify, // it will clear its cache if there's any changes of the file. diff --git a/os/gfcache/gfcache_z_unit_test.go b/os/gfile/gfile_z_cache_test.go old mode 100755 new mode 100644 similarity index 84% rename from os/gfcache/gfcache_z_unit_test.go rename to os/gfile/gfile_z_cache_test.go index 6d96b6a72..eb0e30005 --- a/os/gfcache/gfcache_z_unit_test.go +++ b/os/gfile/gfile_z_cache_test.go @@ -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 { diff --git a/os/gproc/gproc_comm.go b/os/gproc/gproc_comm.go index 1e81c1ee1..0b962ffa5 100644 --- a/os/gproc/gproc_comm.go +++ b/os/gproc/gproc_comm.go @@ -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) } diff --git a/os/gview/gview_parse.go b/os/gview/gview_parse.go index 4184aef9d..094a0e776 100644 --- a/os/gview/gview_parse.go +++ b/os/gview/gview_parse.go @@ -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 {