From 99577ad87411bf846ed1382a96cf338c86279ba4 Mon Sep 17 00:00:00 2001 From: John Date: Thu, 31 Jan 2019 13:55:53 +0800 Subject: [PATCH] hot fix gfcache issue --- g/os/gfcache/gfcache.go | 11 +++++------ g/os/gfcache/gfcache_cache.go | 27 +++++++++++++++------------ geg/other/test.go | 21 +++------------------ 3 files changed, 23 insertions(+), 36 deletions(-) diff --git a/g/os/gfcache/gfcache.go b/g/os/gfcache/gfcache.go index ffd7891d9..36a429889 100644 --- a/g/os/gfcache/gfcache.go +++ b/g/os/gfcache/gfcache.go @@ -10,14 +10,14 @@ package gfcache import ( + "gitee.com/johng/gf/g/container/gmap" "gitee.com/johng/gf/g/container/gtype" - "gitee.com/johng/gf/g/os/gcache" ) type Cache struct { - cap *gtype.Int // 缓存容量(byte),设置为0表示不限制 - size *gtype.Int // 缓存大小(Byte) - cache *gcache.Cache // 缓存对象 + cap *gtype.Int // 缓存容量(byte),设置为0表示不限制 + size *gtype.Int // 缓存大小(Byte) + cache *gmap.StringInterfaceMap // 缓存对象 } const ( @@ -38,11 +38,10 @@ func New(cap ... int) *Cache { return &Cache { cap : gtype.NewInt(c), size : gtype.NewInt(), - cache : gcache.New(), + cache : gmap.NewStringInterfaceMap(), } } - // 获得已缓存的文件大小(byte) func GetSize() int { return cache.GetSize() diff --git a/g/os/gfcache/gfcache_cache.go b/g/os/gfcache/gfcache_cache.go index 91981fa01..0021150f9 100644 --- a/g/os/gfcache/gfcache_cache.go +++ b/g/os/gfcache/gfcache_cache.go @@ -12,7 +12,7 @@ import ( "gitee.com/johng/gf/g/os/gfsnotify" ) -// 设置容量大小(MB) +// 设置容量大小(byte) func (c *Cache) SetCap(cap int) { c.cap.Set(cap) } @@ -34,15 +34,15 @@ func (c *Cache) GetContents(path string) string { // 获得文件内容 []byte func (c *Cache) GetBinContents(path string) []byte { - v := c.cache.Get(path) - if v != nil { + if v := c.cache.Get(path); v != nil { return v.([]byte) } b := gfile.GetBinContents(path) - if b != nil && (c.cap.Val() == 0 || c.size.Val() < c.cap.Val()) { - c.addMonitor(path) - c.cache.Set(path, b, 0) + // 读取到内容,并且没有超过缓存容量限制时才会执行缓存 + if len(b) > 0 && (c.cap.Val() == 0 || c.size.Val() < c.cap.Val()) { c.size.Add(len(b)) + c.cache.Set(path, b) + c.addMonitor(path) } return b } @@ -55,19 +55,22 @@ func (c *Cache) addMonitor(path string) { } gfsnotify.Add(path, func(event *gfsnotify.Event) { //glog.Debug("gfcache:", event) - r := c.cache.Get(path).([]byte) + length := 0 + if r := c.cache.Get(path); r != nil { + length = len(r.([]byte)) + } // 是否删除 if event.IsRemove() { c.cache.Remove(path) - c.size.Add(-len(r)) + c.size.Add(-length) + return } // 更新缓存内容 if c.cap.Val() == 0 || c.size.Val() < c.cap.Val() { b := gfile.GetBinContents(path) - if b != nil { - dif := len(b) - len(r) - c.cache.Set(path, b, 0) - c.size.Add(dif) + if len(b) > 0 { + c.size.Add(len(b) - length) + c.cache.Set(path, b) } } }) diff --git a/geg/other/test.go b/geg/other/test.go index 5457752ea..a86d0f7c3 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -1,25 +1,10 @@ package main import ( - "gitee.com/johng/gf/g" - "gitee.com/johng/gf/g/net/ghttp" + "fmt" + "gitee.com/johng/gf/g/os/gfile" ) -func Handler(r *ghttp.Request) { - if r.Request.Method == "OPTIONS" { - return - } - r.Response.WriteJson(g.Map{"name" : "john"}) -} - func main() { - s := g.Server() - s.BindHookHandler("/*any", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) { - r.Response.SetAllowCrossDomainRequest("*", "PUT,GET,POST,DELETE,OPTIONS") - r.Response.Header().Set("Access-Control-Allow-Credentials", "true") - r.Response.Header().Set("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, token") - }) - s.Group("/v1").ALL("*", Handler, ghttp.HOOK_BEFORE_SERVE) - s.SetPort(6789) - s.Run() + fmt.Println(gfile.RealPath("config")) } \ No newline at end of file