simple_cache_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package cache
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. func TestSimpleCache_Set(t *testing.T) {
  7. s := NewSimpleCache()
  8. err := s.Set("key", "value", 0)
  9. if err != nil {
  10. t.Fatalf("写入失败")
  11. }
  12. err = s.Set("key", 111, 0)
  13. if err != nil {
  14. t.Fatalf("写入失败")
  15. }
  16. }
  17. func TestSimpleCache_Get(t *testing.T) {
  18. s := NewSimpleCache()
  19. err := s.Set("key", "value", 0)
  20. value := ""
  21. err = s.Get("key", &value)
  22. fmt.Println("value", value)
  23. if err != nil {
  24. t.Fatalf("读取失败")
  25. }
  26. err = s.Set("key1", 11, 0)
  27. value1 := 0
  28. err = s.Get("key1", &value1)
  29. fmt.Println("value1", value1)
  30. if err != nil {
  31. t.Fatalf("读取失败")
  32. }
  33. err = s.Set("key2", []byte{'a', 'b'}, 0)
  34. value2 := []byte{}
  35. err = s.Get("key2", &value2)
  36. fmt.Println("value2", string(value2))
  37. if err != nil {
  38. t.Fatalf("读取失败")
  39. }
  40. err = s.Set("key3", 33.33, 0)
  41. var value3 int
  42. err = s.Get("key3", &value3)
  43. fmt.Println("value3", value3)
  44. if err != nil {
  45. t.Fatalf("读取失败")
  46. }
  47. }
  48. type r struct {
  49. A string `json:"a"`
  50. B string `json:"b"`
  51. R *rr `json:"r"`
  52. }
  53. type r2 struct {
  54. A string `json:"a"`
  55. B string `json:"b"`
  56. }
  57. type rr struct {
  58. AA string `json:"aa"`
  59. BB string `json:"bb"`
  60. }
  61. func TestSimpleCache_GetStruct(t *testing.T) {
  62. s := NewSimpleCache()
  63. old_rr := &rr{
  64. AA: "aa", BB: "bb",
  65. }
  66. old := &r{
  67. A: "ab", B: "cdc",
  68. R: old_rr,
  69. }
  70. err := s.Set("key", old, 300)
  71. if err != nil {
  72. t.Fatalf("写入失败")
  73. }
  74. res := &r{}
  75. err2 := s.Get("key", res)
  76. fmt.Println("res", res)
  77. if err2 != nil {
  78. t.Fatalf("读取失败" + err2.Error())
  79. }
  80. //修改原始值,看后面是否会变化
  81. old.A = "aa"
  82. old_rr.AA = "aaa"
  83. fmt.Println("old", old)
  84. res2 := &r{}
  85. err3 := s.Get("key", res2)
  86. fmt.Println("res2", res2, res2.R.AA, res2.R.BB)
  87. if err3 != nil {
  88. t.Fatalf("读取失败" + err3.Error())
  89. }
  90. //if reflect.DeepEqual(res, old) {
  91. // t.Fatalf("读取错误")
  92. //}
  93. }