redistest.go 563 B

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