update unit test cases of package gins

This commit is contained in:
John 2019-03-14 23:28:56 +08:00
parent 45a83fc53c
commit cb8362d447
8 changed files with 70 additions and 105 deletions

View File

@ -12,6 +12,7 @@ import (
"github.com/gogf/gf/g/os/gfile"
"github.com/gogf/gf/g/test/gtest"
"testing"
"time"
)
func Test_Database(t *testing.T) {
@ -54,6 +55,9 @@ test = "v=2"
defer gfile.Remove(path)
defer gins.Config().Reload()
// for gfsnotify callbacks to refresh cache of config file
time.Sleep(time.Second)
gtest.Case(t, func() {
fmt.Println("gins Test_Database", gins.Config().Get("test"))

View File

@ -12,6 +12,7 @@ import (
"github.com/gogf/gf/g/os/gfile"
"github.com/gogf/gf/g/test/gtest"
"testing"
"time"
)
func Test_Redis(t *testing.T) {
@ -54,6 +55,9 @@ test = "v=3"
defer gfile.Remove(path)
defer gins.Config().Reload()
// for gfsnotify callbacks to refresh cache of config file
time.Sleep(time.Second)
gtest.Case(t, func() {
fmt.Println("gins Test_Redis", gins.Config().Get("test"))

View File

@ -10,52 +10,52 @@
package gfcache
import (
"github.com/gogf/gf/g/container/gmap"
"github.com/gogf/gf/g/container/gtype"
"github.com/gogf/gf/g/internal/cmdenv"
"github.com/gogf/gf/g/os/gcache"
"github.com/gogf/gf/g/os/gfile"
"github.com/gogf/gf/g/os/gfsnotify"
)
type Cache struct {
cap *gtype.Int // 缓存容量(byte)设置为0表示不限制
size *gtype.Int // 缓存大小(Byte)
cache *gmap.StringInterfaceMap // 缓存对象
}
const (
// 默认的缓存容量(10MB)
gDEFAULT_CACHE_CAP = 10*1024*1024
// 默认的缓存超时时间(60秒)
gDEFAULT_CACHE_EXPIRE = 60
)
var (
// 默认的缓存容量
cacheCap = cmdenv.Get("gf.gfcache.cap", gDEFAULT_CACHE_CAP).Int()
// 默认的文件缓存对象
cache = New()
// 默认的缓存时间(秒)
cacheExpire = cmdenv.Get("gf.gfcache.expire", gDEFAULT_CACHE_EXPIRE).Int()*1000
)
func New(cap ... int) *Cache {
c := cacheCap
if len(cap) > 0 {
c = cap[0]
// 获得文件内容 stringexpire参数为缓存过期时间单位为秒。
func GetContents(path string, expire...int) string {
return string(GetBinContents(path, expire...))
}
// 获得文件内容 []byteexpire参数为缓存过期时间单位为秒。
func GetBinContents(path string, expire...int) []byte {
k := cacheKey(path)
e := cacheExpire
if len(expire) > 0 {
e = expire[0]
}
return &Cache {
cap : gtype.NewInt(c),
size : gtype.NewInt(),
cache : gmap.NewStringInterfaceMap(),
r := gcache.GetOrSetFuncLock(k, func() interface{} {
b := gfile.GetBinContents(path)
if b != nil {
// 添加文件监控,如果文件有任何变化,立即清空缓存
gfsnotify.Add(path, func(event *gfsnotify.Event) {
gcache.Remove(k)
gfsnotify.Exit()
})
}
return b
}, e*1000)
if r != nil {
return r.([]byte)
}
return nil
}
// 获得已缓存的文件大小(byte)
func GetSize() int {
return cache.GetSize()
}
// 获得文件内容 string
func GetContents(path string) string {
return cache.GetContents(path)
}
// 获得文件内容 []byte
func GetBinContents(path string) []byte {
return cache.GetBinContents(path)
// 生成缓存键名
func cacheKey(path string) string {
return "gf.gfcache:" + path
}

View File

@ -1,62 +0,0 @@
// Copyright 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
import (
"github.com/gogf/gf/g/os/gfile"
"github.com/gogf/gf/g/os/gfsnotify"
)
// 设置容量大小(byte)
func (c *Cache) SetCap(cap int) {
c.cap.Set(cap)
}
// 获得缓存容量大小(byte)
func (c *Cache) GetCap() int {
return c.cap.Val()
}
// 获得已缓存的文件大小(byte)
func (c *Cache) GetSize() int {
return c.size.Val()
}
// 获得文件内容 string
func (c *Cache) GetContents(path string) string {
return string(c.GetBinContents(path))
}
// 获得文件内容 []byte
func (c *Cache) GetBinContents(path string) []byte {
if v := c.cache.Get(path); v != nil {
return v.([]byte)
}
b := gfile.GetBinContents(path)
// 读取到内容,并且没有超过缓存容量限制时才会执行缓存
if len(b) > 0 && (c.cap.Val() == 0 || c.size.Val() < c.cap.Val()) {
c.size.Add(len(b))
c.addMonitor(path)
c.cache.Set(path, b)
}
return b
}
// 添加文件监控,一旦文件有变化立即清除缓存,下一次读取的时候再执行缓存。
func (c *Cache) addMonitor(path string) {
// 防止多goroutine同时调用
if c.cache.Contains(path) {
return
}
gfsnotify.Add(path, func(event *gfsnotify.Event) {
if r := c.cache.Get(path); r != nil {
c.cache.Remove(path)
c.size.Add(-len(r.([]byte)))
}
})
}

View File

@ -24,7 +24,7 @@ func GetContents(path string) string {
return string(GetBinContents(path))
}
// (二进制)读取文件内容
// (二进制)读取文件内容如果文件不存在或者读取失败返回nil。
func GetBinContents(path string) []byte {
data, err := ioutil.ReadFile(path)
if err != nil {

View File

@ -59,7 +59,8 @@ const (
)
const (
REPEAT_EVENT_FILTER_INTERVAL = 1 // (毫秒)重复事件过滤间隔
REPEAT_EVENT_FILTER_INTERVAL = 1 // (毫秒)重复事件过滤间隔
gFSNOTIFY_EVENT_EXIT = "exit" // 是否退出回调执行
)
var (
@ -113,3 +114,8 @@ func RemoveCallback(callbackId int) error {
defaultWatcher.RemoveCallback(callbackId)
return nil
}
// 在回调方法中调用该方法退出回调注册
func Exit() {
panic(gFSNOTIFY_EVENT_EXIT)
}

View File

@ -74,7 +74,7 @@ func (w *Watcher) getCallbacks(path string) (callbacks []*Callback) {
return
}
// 事件循环
// 事件循环(核心逻辑)
func (w *Watcher) startEventLoop() {
go func() {
for {
@ -126,10 +126,22 @@ func (w *Watcher) startEventLoop() {
}
// 执行回调处理,异步处理
for _, callback := range callbacks {
go callback.Func(event)
for _, v := range callbacks {
go func(callback *Callback) {
defer func() {
// 是否退出监控
if err := recover(); err != nil {
switch err {
case gFSNOTIFY_EVENT_EXIT:
w.RemoveCallback(callback.Id)
default:
panic(err)
}
}
}()
callback.Func(event)
}(v)
}
} else {
break
}

View File

@ -2,10 +2,11 @@ package main
import (
"fmt"
"github.com/gogf/gf/g/os/gtime"
"github.com/gogf/gf/g/util/gconv"
)
func main() {
t := gconv.GTime("2010-10-10 00:00:01")
fmt.Println(t.String())
//t := gconv.GTime("2010-10-10 00:00:01")
fmt.Println(gconv.String(gtime.Millisecond()))
}