cache_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package cache
  2. import (
  3. "fmt"
  4. "github.com/go-redis/redis/v8"
  5. "reflect"
  6. "testing"
  7. )
  8. func TestSimpleCache(t *testing.T) {
  9. type st struct {
  10. A string
  11. B string
  12. }
  13. items := map[string]interface{}{}
  14. items["a"] = "b"
  15. items["b"] = "c"
  16. ab := &st{
  17. A: "a",
  18. B: "b",
  19. }
  20. items["ab"] = *ab
  21. a := items["a"]
  22. fmt.Println(a)
  23. b := items["b"]
  24. fmt.Println(b)
  25. ab.A = "aa"
  26. ab2 := st{}
  27. ab2 = (items["ab"]).(st)
  28. fmt.Println(ab2, reflect.TypeOf(ab2))
  29. }
  30. func TestFileCacheSet(t *testing.T) {
  31. fc := New("file")
  32. err := fc.Set("123", "ddd", 0)
  33. if err != nil {
  34. fmt.Println(err.Error())
  35. t.Fatalf("写入失败")
  36. }
  37. }
  38. func TestFileCacheGet(t *testing.T) {
  39. fc := New("file")
  40. err := fc.Set("123", "45156", 300)
  41. if err != nil {
  42. t.Fatalf("写入失败")
  43. }
  44. res := ""
  45. err = fc.Get("123", &res)
  46. if err != nil {
  47. t.Fatalf("读取失败")
  48. }
  49. fmt.Println("res", res)
  50. }
  51. func TestRedisCacheSet(t *testing.T) {
  52. rc := NewRedis(&redis.Options{
  53. Addr: "192.168.1.168:6379",
  54. Password: "", // no password set
  55. DB: 0, // use default DB
  56. })
  57. err := rc.Set("123", "ddd", 0)
  58. if err != nil {
  59. fmt.Println(err.Error())
  60. t.Fatalf("写入失败")
  61. }
  62. }
  63. func TestRedisCacheGet(t *testing.T) {
  64. rc := NewRedis(&redis.Options{
  65. Addr: "192.168.1.168:6379",
  66. Password: "", // no password set
  67. DB: 0, // use default DB
  68. })
  69. err := rc.Set("123", "451156", 300)
  70. if err != nil {
  71. t.Fatalf("写入失败")
  72. }
  73. res := ""
  74. err = rc.Get("123", &res)
  75. if err != nil {
  76. t.Fatalf("读取失败")
  77. }
  78. fmt.Println("res", res)
  79. }