mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
hot fix gfcache issue
This commit is contained in:
@ -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()
|
||||
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@ -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"))
|
||||
}
|
||||
Reference in New Issue
Block a user