redis_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. package redis
  15. import (
  16. "testing"
  17. "time"
  18. "github.com/astaxie/beego/cache"
  19. "github.com/garyburd/redigo/redis"
  20. )
  21. func TestRedisCache(t *testing.T) {
  22. bm, err := cache.NewCache("redis", `{"conn": "127.0.0.1:6379"}`)
  23. if err != nil {
  24. t.Error("init err")
  25. }
  26. timeoutDuration := 10 * time.Second
  27. if err = bm.Put("astaxie", 1, timeoutDuration); err != nil {
  28. t.Error("set Error", err)
  29. }
  30. if !bm.IsExist("astaxie") {
  31. t.Error("check err")
  32. }
  33. time.Sleep(11 * time.Second)
  34. if bm.IsExist("astaxie") {
  35. t.Error("check err")
  36. }
  37. if err = bm.Put("astaxie", 1, timeoutDuration); err != nil {
  38. t.Error("set Error", err)
  39. }
  40. if v, _ := redis.Int(bm.Get("astaxie"), err); v != 1 {
  41. t.Error("get err")
  42. }
  43. if err = bm.Incr("astaxie"); err != nil {
  44. t.Error("Incr Error", err)
  45. }
  46. if v, _ := redis.Int(bm.Get("astaxie"), err); v != 2 {
  47. t.Error("get err")
  48. }
  49. if err = bm.Decr("astaxie"); err != nil {
  50. t.Error("Decr Error", err)
  51. }
  52. if v, _ := redis.Int(bm.Get("astaxie"), err); v != 1 {
  53. t.Error("get err")
  54. }
  55. bm.Delete("astaxie")
  56. if bm.IsExist("astaxie") {
  57. t.Error("delete err")
  58. }
  59. //test string
  60. if err = bm.Put("astaxie", "author", timeoutDuration); err != nil {
  61. t.Error("set Error", err)
  62. }
  63. if !bm.IsExist("astaxie") {
  64. t.Error("check err")
  65. }
  66. if v, _ := redis.String(bm.Get("astaxie"), err); v != "author" {
  67. t.Error("get err")
  68. }
  69. //test GetMulti
  70. if err = bm.Put("astaxie1", "author1", timeoutDuration); err != nil {
  71. t.Error("set Error", err)
  72. }
  73. if !bm.IsExist("astaxie1") {
  74. t.Error("check err")
  75. }
  76. vv := bm.GetMulti([]string{"astaxie", "astaxie1"})
  77. if len(vv) != 2 {
  78. t.Error("GetMulti ERROR")
  79. }
  80. if v, _ := redis.String(vv[0], nil); v != "author" {
  81. t.Error("GetMulti ERROR")
  82. }
  83. if v, _ := redis.String(vv[1], nil); v != "author1" {
  84. t.Error("GetMulti ERROR")
  85. }
  86. // test clear all
  87. if err = bm.ClearAll(); err != nil {
  88. t.Error("clear all err")
  89. }
  90. }