cache_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. "time"
  7. "go-common/library/cache/redis"
  8. "go-common/library/container/pool"
  9. xtime "go-common/library/time"
  10. )
  11. var p *redis.Pool
  12. var config *redis.Config
  13. var d *Dao
  14. var ctx = context.TODO()
  15. func init() {
  16. config = getConfig()
  17. p = redis.NewPool(config)
  18. d = &Dao{redis: p}
  19. }
  20. func getConfig() (c *redis.Config) {
  21. c = &redis.Config{
  22. Name: "test",
  23. Proto: "tcp",
  24. Addr: "127.0.0.1:6379",
  25. DialTimeout: xtime.Duration(time.Second),
  26. ReadTimeout: xtime.Duration(time.Second),
  27. WriteTimeout: xtime.Duration(time.Second),
  28. }
  29. c.Config = &pool.Config{
  30. Active: 20,
  31. Idle: 2,
  32. IdleTimeout: xtime.Duration(90 * time.Second),
  33. }
  34. return
  35. }
  36. func TestDao_Get(t *testing.T) {
  37. v, e := d.Get(ctx, "golang")
  38. fmt.Println(v, e)
  39. }
  40. func TestDao_Set(t *testing.T) {
  41. fmt.Println(d.Set(ctx, "golang", 22))
  42. }
  43. func TestDao_SetEx(t *testing.T) {
  44. got, err := d.SetEx(ctx, "golang", "b", 10)
  45. fmt.Println(got, err)
  46. }
  47. func TestDao_Del(t *testing.T) {
  48. got, err := d.Del(ctx, "golang")
  49. fmt.Println(got, err)
  50. }
  51. func TestDao_HMSet(t *testing.T) {
  52. m := map[string]interface{}{"id": 1, "uid": 2, "content": "sss"}
  53. got, err := d.HMSet(ctx, "myhash", m)
  54. fmt.Println(got, err)
  55. v, _ := d.HGetAll(ctx, "myhash")
  56. fmt.Printf("%+v\n", v)
  57. }
  58. func TestDao_Expire(t *testing.T) {
  59. got, err := d.Expire(ctx, "ttl", 10)
  60. fmt.Println(got, err)
  61. }
  62. func TestDao_HGetAll(t *testing.T) {
  63. got, err := d.HGetAll(ctx, "myhash")
  64. if err != nil && err != ErrEmptyMap {
  65. fmt.Println(err.Error())
  66. }
  67. fmt.Printf("%+v ,%+v", got, err)
  68. }
  69. func TestDao_SetWithNxEx(t *testing.T) {
  70. fmt.Println(d.SetWithNxEx(ctx, "python", "1", 100))
  71. }