1
0

cache_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 cache
  15. import (
  16. "os"
  17. "testing"
  18. "time"
  19. )
  20. func TestCache(t *testing.T) {
  21. bm, err := NewCache("memory", `{"interval":20}`)
  22. if err != nil {
  23. t.Error("init err")
  24. }
  25. timeoutDuration := 10 * time.Second
  26. if err = bm.Put("astaxie", 1, timeoutDuration); err != nil {
  27. t.Error("set Error", err)
  28. }
  29. if !bm.IsExist("astaxie") {
  30. t.Error("check err")
  31. }
  32. if v := bm.Get("astaxie"); v.(int) != 1 {
  33. t.Error("get err")
  34. }
  35. time.Sleep(30 * time.Second)
  36. if bm.IsExist("astaxie") {
  37. t.Error("check err")
  38. }
  39. if err = bm.Put("astaxie", 1, timeoutDuration); err != nil {
  40. t.Error("set Error", err)
  41. }
  42. if err = bm.Incr("astaxie"); err != nil {
  43. t.Error("Incr Error", err)
  44. }
  45. if v := bm.Get("astaxie"); v.(int) != 2 {
  46. t.Error("get err")
  47. }
  48. if err = bm.Decr("astaxie"); err != nil {
  49. t.Error("Decr Error", err)
  50. }
  51. if v := bm.Get("astaxie"); v.(int) != 1 {
  52. t.Error("get err")
  53. }
  54. bm.Delete("astaxie")
  55. if bm.IsExist("astaxie") {
  56. t.Error("delete err")
  57. }
  58. //test GetMulti
  59. if err = bm.Put("astaxie", "author", timeoutDuration); err != nil {
  60. t.Error("set Error", err)
  61. }
  62. if !bm.IsExist("astaxie") {
  63. t.Error("check err")
  64. }
  65. if v := bm.Get("astaxie"); v.(string) != "author" {
  66. t.Error("get err")
  67. }
  68. if err = bm.Put("astaxie1", "author1", timeoutDuration); err != nil {
  69. t.Error("set Error", err)
  70. }
  71. if !bm.IsExist("astaxie1") {
  72. t.Error("check err")
  73. }
  74. vv := bm.GetMulti([]string{"astaxie", "astaxie1"})
  75. if len(vv) != 2 {
  76. t.Error("GetMulti ERROR")
  77. }
  78. if vv[0].(string) != "author" {
  79. t.Error("GetMulti ERROR")
  80. }
  81. if vv[1].(string) != "author1" {
  82. t.Error("GetMulti ERROR")
  83. }
  84. }
  85. func TestFileCache(t *testing.T) {
  86. bm, err := NewCache("file", `{"CachePath":"cache","FileSuffix":".bin","DirectoryLevel":2,"EmbedExpiry":0}`)
  87. if err != nil {
  88. t.Error("init err")
  89. }
  90. timeoutDuration := 10 * time.Second
  91. if err = bm.Put("astaxie", 1, timeoutDuration); err != nil {
  92. t.Error("set Error", err)
  93. }
  94. if !bm.IsExist("astaxie") {
  95. t.Error("check err")
  96. }
  97. if v := bm.Get("astaxie"); v.(int) != 1 {
  98. t.Error("get err")
  99. }
  100. if err = bm.Incr("astaxie"); err != nil {
  101. t.Error("Incr Error", err)
  102. }
  103. if v := bm.Get("astaxie"); v.(int) != 2 {
  104. t.Error("get err")
  105. }
  106. if err = bm.Decr("astaxie"); err != nil {
  107. t.Error("Decr Error", err)
  108. }
  109. if v := bm.Get("astaxie"); v.(int) != 1 {
  110. t.Error("get err")
  111. }
  112. bm.Delete("astaxie")
  113. if bm.IsExist("astaxie") {
  114. t.Error("delete err")
  115. }
  116. //test string
  117. if err = bm.Put("astaxie", "author", timeoutDuration); err != nil {
  118. t.Error("set Error", err)
  119. }
  120. if !bm.IsExist("astaxie") {
  121. t.Error("check err")
  122. }
  123. if v := bm.Get("astaxie"); v.(string) != "author" {
  124. t.Error("get err")
  125. }
  126. //test GetMulti
  127. if err = bm.Put("astaxie1", "author1", timeoutDuration); err != nil {
  128. t.Error("set Error", err)
  129. }
  130. if !bm.IsExist("astaxie1") {
  131. t.Error("check err")
  132. }
  133. vv := bm.GetMulti([]string{"astaxie", "astaxie1"})
  134. if len(vv) != 2 {
  135. t.Error("GetMulti ERROR")
  136. }
  137. if vv[0].(string) != "author" {
  138. t.Error("GetMulti ERROR")
  139. }
  140. if vv[1].(string) != "author1" {
  141. t.Error("GetMulti ERROR")
  142. }
  143. os.RemoveAll("cache")
  144. }