redis.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package parallel
  2. import (
  3. "context"
  4. "unsafe"
  5. "go-common/library/cache/redis"
  6. "go-common/library/log"
  7. )
  8. // RedisTask .
  9. type RedisTask struct {
  10. ctx *context.Context
  11. name string
  12. pool *redis.Pool
  13. cmd string
  14. args []interface{}
  15. }
  16. // NewRedisTaskWithName new redis parallel task
  17. func NewRedisTaskWithName(ctx *context.Context, name string, pool *redis.Pool, cmd string, args ...interface{}) *RedisTask {
  18. return &RedisTask{
  19. ctx: ctx,
  20. name: name,
  21. pool: pool,
  22. cmd: cmd,
  23. args: args,
  24. }
  25. }
  26. // NewRedisTask new redis parallel task
  27. func NewRedisTask(ctx *context.Context, pool *redis.Pool, cmd string, args ...interface{}) *RedisTask {
  28. return &RedisTask{
  29. ctx: ctx,
  30. pool: pool,
  31. cmd: cmd,
  32. args: args,
  33. }
  34. }
  35. // Run .
  36. func (rt *RedisTask) Run() (result *[]byte) {
  37. conn := rt.pool.Get(*rt.ctx)
  38. defer conn.Close()
  39. reply, err := conn.Do(rt.cmd, rt.args...)
  40. if err != nil {
  41. log.Error("RedisTask Run error:[%+v]", err)
  42. return
  43. }
  44. switch reply := reply.(type) {
  45. case []byte:
  46. result = &reply
  47. case string:
  48. b := []byte(reply)
  49. result = &b
  50. default:
  51. result = (*[]byte)(unsafe.Pointer(&reply))
  52. }
  53. return
  54. }