1
0

memcache_test.go 2.6 KB

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