github.com/astaxie/beego@v1.12.3/cache/redis/redis_test.go (about) 1 // Copyright 2014 beego Author. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package redis 16 17 import ( 18 "fmt" 19 "testing" 20 "time" 21 22 "github.com/astaxie/beego/cache" 23 "github.com/gomodule/redigo/redis" 24 ) 25 26 func TestRedisCache(t *testing.T) { 27 bm, err := cache.NewCache("redis", `{"conn": "127.0.0.1:6379"}`) 28 if err != nil { 29 t.Error("init err") 30 } 31 timeoutDuration := 10 * time.Second 32 if err = bm.Put("astaxie", 1, timeoutDuration); err != nil { 33 t.Error("set Error", err) 34 } 35 if !bm.IsExist("astaxie") { 36 t.Error("check err") 37 } 38 39 time.Sleep(11 * time.Second) 40 41 if bm.IsExist("astaxie") { 42 t.Error("check err") 43 } 44 if err = bm.Put("astaxie", 1, timeoutDuration); err != nil { 45 t.Error("set Error", err) 46 } 47 48 if v, _ := redis.Int(bm.Get("astaxie"), err); v != 1 { 49 t.Error("get err") 50 } 51 52 if err = bm.Incr("astaxie"); err != nil { 53 t.Error("Incr Error", err) 54 } 55 56 if v, _ := redis.Int(bm.Get("astaxie"), err); v != 2 { 57 t.Error("get err") 58 } 59 60 if err = bm.Decr("astaxie"); err != nil { 61 t.Error("Decr Error", err) 62 } 63 64 if v, _ := redis.Int(bm.Get("astaxie"), err); v != 1 { 65 t.Error("get err") 66 } 67 bm.Delete("astaxie") 68 if bm.IsExist("astaxie") { 69 t.Error("delete err") 70 } 71 72 //test string 73 if err = bm.Put("astaxie", "author", timeoutDuration); err != nil { 74 t.Error("set Error", err) 75 } 76 if !bm.IsExist("astaxie") { 77 t.Error("check err") 78 } 79 80 if v, _ := redis.String(bm.Get("astaxie"), err); v != "author" { 81 t.Error("get err") 82 } 83 84 //test GetMulti 85 if err = bm.Put("astaxie1", "author1", timeoutDuration); err != nil { 86 t.Error("set Error", err) 87 } 88 if !bm.IsExist("astaxie1") { 89 t.Error("check err") 90 } 91 92 vv := bm.GetMulti([]string{"astaxie", "astaxie1"}) 93 if len(vv) != 2 { 94 t.Error("GetMulti ERROR") 95 } 96 if v, _ := redis.String(vv[0], nil); v != "author" { 97 t.Error("GetMulti ERROR") 98 } 99 if v, _ := redis.String(vv[1], nil); v != "author1" { 100 t.Error("GetMulti ERROR") 101 } 102 103 // test clear all 104 if err = bm.ClearAll(); err != nil { 105 t.Error("clear all err") 106 } 107 } 108 109 func TestCache_Scan(t *testing.T) { 110 timeoutDuration := 10 * time.Second 111 // init 112 bm, err := cache.NewCache("redis", `{"conn": "127.0.0.1:6379"}`) 113 if err != nil { 114 t.Error("init err") 115 } 116 // insert all 117 for i := 0; i < 10000; i++ { 118 if err = bm.Put(fmt.Sprintf("astaxie%d", i), fmt.Sprintf("author%d", i), timeoutDuration); err != nil { 119 t.Error("set Error", err) 120 } 121 } 122 // scan all for the first time 123 keys, err := bm.(*Cache).Scan(DefaultKey + ":*") 124 if err != nil { 125 t.Error("scan Error", err) 126 } 127 if len(keys) != 10000 { 128 t.Error("scan all err") 129 } 130 131 // clear all 132 if err = bm.ClearAll(); err != nil { 133 t.Error("clear all err") 134 } 135 136 // scan all for the second time 137 keys, err = bm.(*Cache).Scan(DefaultKey + ":*") 138 if err != nil { 139 t.Error("scan Error", err) 140 } 141 if len(keys) != 0 { 142 t.Error("scan all err") 143 } 144 }