merge master

This commit is contained in:
John 2019-01-31 18:14:02 +08:00
commit 0e611ae94b
4 changed files with 30 additions and 19 deletions

View File

@ -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()

View File

@ -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)
}
}
})

View File

@ -1,10 +1,19 @@
package main
import (
<<<<<<< HEAD
"gitee.com/johng/gf/g/os/gspath"
"gitee.com/johng/gf/g/util/gutil"
)
func main() {
gutil.Dump(gspath.Get("/Users/john/Temp/config").AllPaths())
=======
"fmt"
"gitee.com/johng/gf/g/os/gfile"
)
func main() {
fmt.Println(gfile.RealPath("config"))
>>>>>>> 104613b056b4b2f85af786638d37aa50d7bc06e2
}

View File

@ -1,5 +1,5 @@
package gf
const VERSION = "v1.4.6"
const VERSION = "v1.4.6.1"
const AUTHORS = "john<john@goframe.org>"