redistest.go 520 B

12345678910111213141516171819202122232425262728
  1. package redistest
  2. import (
  3. "time"
  4. "github.com/alicebob/miniredis/v2"
  5. "github.com/tal-tech/go-zero/core/lang"
  6. "github.com/tal-tech/go-zero/core/stores/redis"
  7. )
  8. func CreateRedis() (r *redis.Redis, clean func(), err error) {
  9. mr, err := miniredis.Run()
  10. if err != nil {
  11. return nil, nil, err
  12. }
  13. return redis.NewRedis(mr.Addr(), redis.NodeType), func() {
  14. ch := make(chan lang.PlaceholderType)
  15. go func() {
  16. mr.Close()
  17. close(ch)
  18. }()
  19. select {
  20. case <-ch:
  21. case <-time.After(time.Second):
  22. }
  23. }, nil
  24. }