123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- package mananger
- import (
- "app.yhyue.com/moapp/jybase/redis"
- "fmt"
- "github.com/robfig/cron/v3"
- "leadGeneration/entity/activeUsers"
- "leadGeneration/entity/search"
- "leadGeneration/public"
- "leadGeneration/vars"
- "log"
- "sync"
- "time"
- )
- //AheadManager 超前项目管理
- type AheadManager struct {
- Conf vars.AheadConfig
- UserGroup map[string]int
- sync.RWMutex
- }
- const (
- //缓存
- AheadCacheDb = "newother"
- AheadRequestFrequencyCacheKey = "leadGeneration_AheadRequest_%d_%s"
- AheadRequestTimesLong = 60 * 60 * 24 //缓存一天
- )
- // InitAheadManager 初始化
- func InitAheadManager(conf vars.AheadConfig) *AheadManager {
- manager := &AheadManager{
- Conf: conf,
- UserGroup: make(map[string]int),
- }
- go manager.ScheduledTasks()
- return manager
- }
- // GetData 获取查询数据
- func (this *AheadManager) GetData(userId, keyWords string, isNew bool) map[string]interface{} {
- if !(isNew || this.checkGroupUser(userId)) {
- return nil
- }
- //校验每日请求次数
- cacheKey := fmt.Sprintf(AheadRequestFrequencyCacheKey, time.Now().Format(public.TimeFormat), userId)
- if this.Conf.DailyTimes > redis.GetInt(AheadCacheDb, fmt.Sprintf(AheadRequestFrequencyCacheKey, time.Now().Format(public.TimeFormat))) {
- return nil
- }
- //查询数据
- rDate := search.AdvancedProject(userId, keyWords)
- //if err != nil {
- // log.Printf("[ERROR]AheadManager %s %s GetData Error %v\n", userId, keyWords, err)
- // return nil
- //}
- //累计请求计数
- if rDate != nil && len(rDate) > 0 {
- if num := redis.Incr(AheadCacheDb, cacheKey); num == 1 {
- _ = redis.SetExpire(AheadCacheDb, cacheKey, AheadRequestTimesLong)
- }
- }
- return rDate
- }
- //CheckGroupUser 校验用户是否有资格展示
- func (this *AheadManager) checkGroupUser(userId string) (exists bool) {
- this.RLock()
- defer this.RUnlock()
- _, exists = this.UserGroup[userId]
- return
- }
- //ScheduledTasks 定时任务
- func (this *AheadManager) ScheduledTasks() {
- if this.Conf.UpdateCron != "" {
- // 给对象增加定时任务
- if _, err := cron.New().AddFunc(this.Conf.UpdateCron, this.UpdateUserGroupJob); err != nil {
- panic(err)
- }
- }
- //首次运行圈选用户
- go this.UpdateUserGroupJob()
- }
- //UpdateUserGroupJob 更新用户群组
- func (this *AheadManager) UpdateUserGroupJob() {
- if this.Conf.Prop <= 0 {
- return
- }
- //查询月活用户
- userArr := activeUsers.GetWeekActiveFreeUsers()
- this.Lock()
- defer this.Unlock()
- //查询上批次活跃用户
- newMap := map[string]int{}
- //测试账户
- if len(vars.Config.TestUid) > 0 {
- for _, uid := range vars.Config.TestUid {
- newMap[uid] = 0
- }
- }
- //if len(this.UserGroup) != 0 {
- // for uId, num := range this.UserGroup {
- // if num > this.Conf.SaveClickTimes {
- // newMap[uId] = 0
- // }
- // }
- //}
- //新圈用户
- for _, uId := range userArr {
- newMap[uId] = 0
- }
- this.UserGroup = newMap
- log.Printf("AheadManager NewGroup %v\n", newMap)
- }
- func sortUserByBatchAndGetFinal() {
- }
|