480aa521 by astaxie

fix #430

1 parent d57557dc
...@@ -5,7 +5,7 @@ import ( ...@@ -5,7 +5,7 @@ import (
5 "time" 5 "time"
6 ) 6 )
7 7
8 func Test_cache(t *testing.T) { 8 func TestCache(t *testing.T) {
9 bm, err := NewCache("memory", `{"interval":20}`) 9 bm, err := NewCache("memory", `{"interval":20}`)
10 if err != nil { 10 if err != nil {
11 t.Error("init err") 11 t.Error("init err")
...@@ -51,3 +51,51 @@ func Test_cache(t *testing.T) { ...@@ -51,3 +51,51 @@ func Test_cache(t *testing.T) {
51 t.Error("delete err") 51 t.Error("delete err")
52 } 52 }
53 } 53 }
54
55 func TestFileCache(t *testing.T) {
56 bm, err := NewCache("file", `{"CachePath":"/cache","FileSuffix":".bin","DirectoryLevel":2,"EmbedExpiry":0}`)
57 if err != nil {
58 t.Error("init err")
59 }
60 if err = bm.Put("astaxie", 1, 10); err != nil {
61 t.Error("set Error", err)
62 }
63 if !bm.IsExist("astaxie") {
64 t.Error("check err")
65 }
66
67 if v := bm.Get("astaxie"); v.(int) != 1 {
68 t.Error("get err")
69 }
70
71 if err = bm.Incr("astaxie"); err != nil {
72 t.Error("Incr Error", err)
73 }
74
75 if v := bm.Get("astaxie"); v.(int) != 2 {
76 t.Error("get err")
77 }
78
79 if err = bm.Decr("astaxie"); err != nil {
80 t.Error("Incr Error", err)
81 }
82
83 if v := bm.Get("astaxie"); v.(int) != 1 {
84 t.Error("get err")
85 }
86 bm.Delete("astaxie")
87 if bm.IsExist("astaxie") {
88 t.Error("delete err")
89 }
90 //test string
91 if err = bm.Put("astaxie", "author", 10); err != nil {
92 t.Error("set Error", err)
93 }
94 if !bm.IsExist("astaxie") {
95 t.Error("check err")
96 }
97
98 if v := bm.Get("astaxie"); v.(string) != "author" {
99 t.Error("get err")
100 }
101 }
......
...@@ -61,6 +61,7 @@ func (this *FileCache) StartAndGC(config string) error { ...@@ -61,6 +61,7 @@ func (this *FileCache) StartAndGC(config string) error {
61 var cfg map[string]string 61 var cfg map[string]string
62 json.Unmarshal([]byte(config), &cfg) 62 json.Unmarshal([]byte(config), &cfg)
63 //fmt.Println(cfg) 63 //fmt.Println(cfg)
64 //fmt.Println(config)
64 if _, ok := cfg["CachePath"]; !ok { 65 if _, ok := cfg["CachePath"]; !ok {
65 cfg["CachePath"] = FileCachePath 66 cfg["CachePath"] = FileCachePath
66 } 67 }
...@@ -135,7 +136,7 @@ func (this *FileCache) Get(key string) interface{} { ...@@ -135,7 +136,7 @@ func (this *FileCache) Get(key string) interface{} {
135 return "" 136 return ""
136 } 137 }
137 var to FileCacheItem 138 var to FileCacheItem
138 Gob_decode([]byte(filedata), &to) 139 Gob_decode(filedata, &to)
139 if to.Expired < time.Now().Unix() { 140 if to.Expired < time.Now().Unix() {
140 return "" 141 return ""
141 } 142 }
...@@ -177,7 +178,7 @@ func (this *FileCache) Delete(key string) error { ...@@ -177,7 +178,7 @@ func (this *FileCache) Delete(key string) error {
177 func (this *FileCache) Incr(key string) error { 178 func (this *FileCache) Incr(key string) error {
178 data := this.Get(key) 179 data := this.Get(key)
179 var incr int 180 var incr int
180 fmt.Println(reflect.TypeOf(data).Name()) 181 //fmt.Println(reflect.TypeOf(data).Name())
181 if reflect.TypeOf(data).Name() != "int" { 182 if reflect.TypeOf(data).Name() != "int" {
182 incr = 0 183 incr = 0
183 } else { 184 } else {
...@@ -210,8 +211,7 @@ func (this *FileCache) IsExist(key string) bool { ...@@ -210,8 +211,7 @@ func (this *FileCache) IsExist(key string) bool {
210 // Clean cached files. 211 // Clean cached files.
211 // not implemented. 212 // not implemented.
212 func (this *FileCache) ClearAll() error { 213 func (this *FileCache) ClearAll() error {
213 //this.CachePath .递归删除 214 //this.CachePath
214
215 return nil 215 return nil
216 } 216 }
217 217
...@@ -271,7 +271,7 @@ func Gob_encode(data interface{}) ([]byte, error) { ...@@ -271,7 +271,7 @@ func Gob_encode(data interface{}) ([]byte, error) {
271 } 271 }
272 272
273 // Gob decodes file cache item. 273 // Gob decodes file cache item.
274 func Gob_decode(data []byte, to interface{}) error { 274 func Gob_decode(data []byte, to *FileCacheItem) error {
275 buf := bytes.NewBuffer(data) 275 buf := bytes.NewBuffer(data)
276 dec := gob.NewDecoder(buf) 276 dec := gob.NewDecoder(buf)
277 return dec.Decode(&to) 277 return dec.Decode(&to)
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!