user_area_job.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "go-common/app/job/live/recommend-job/internal/conf"
  8. "go-common/app/service/live/recommend/recconst"
  9. "go-common/library/cache/redis"
  10. "go-common/library/log"
  11. )
  12. // UserAreaJob 把用户分区偏好导入到redis
  13. type UserAreaJob struct {
  14. JobConf *conf.JobConfig
  15. RedisConf *redis.Config
  16. HadoopConf *conf.HadoopConfig
  17. }
  18. // Run ...
  19. func (j *UserAreaJob) Run() {
  20. log.Info("UserAreaJob Start")
  21. processFile(j.JobConf, j.HadoopConf, j.RedisConf, writeUserAreaToRedis)
  22. log.Info("UserAreaJob End")
  23. }
  24. func writeUserAreaToRedis(line string, pool *redis.Pool) (err error) {
  25. var split = strings.Split(line, ",")
  26. var uid = split[0]
  27. uid = strings.Trim(uid, "\"")
  28. var areaIds = split[1]
  29. areaIds = strings.Trim(areaIds, "\"")
  30. var ctx = context.Background()
  31. var conn = pool.Get(ctx)
  32. defer conn.Close()
  33. uidInt, _ := strconv.Atoi(uid)
  34. var key = fmt.Sprintf(recconst.UserAreaKey, uidInt)
  35. _, err = conn.Do("SETEX", key, 86400*7, areaIds)
  36. if err != nil {
  37. log.Error("writeUserAreaToRedis err +%v, key=%s", err, key)
  38. }
  39. return
  40. }