def _user_tag_old_rank(po_id, tag_id, times=None): if times is None: times = redis.hget(REDIS_REC_PO_TIMES, po_id) score = redis.hget(REDIS_REC_PO_SCORE, po_id) or 0 rank = float(score) / int(times) redis.zadd(REDIS_REC_TAG_OLD%tag_id, po_id, rank)
def rank_update(self, id , rank): name_rank = redis.hget(self.ID2NAME, id) if name_rank: name, _rank = name_rank.rsplit('`', 1) self.append(name, id, rank) for i in tag_alias_by_id_query(id): self.append_alias(i, id , rank)
def po_tag_id_list_new(po, tag_id_list, cid=0): cid = int(cid) po_id = po.id if not cid: cid = redis.hget(REDIS_PO_ID2TAG_CID, po_id) or 0 cid = int(cid) if not cid: if po.cid == CID_NOTE: txt = po.txt if len(txt) > 420: cid = REDIS_REC_CID_NOTE else: cid = REDIS_REC_CID_TALK if cid and cid in REDIS_REC_CID_DICT: #将po放在相应的po_id=>cid中 redis.hset(REDIS_PO_ID2TAG_CID, po_id, cid) new_tag_id_list = set(map(int, tag_id_list)) old_tag_id_list = set(tag_id_list_by_po_id(po_id)) to_add = new_tag_id_list - old_tag_id_list to_rm = old_tag_id_list - new_tag_id_list _po_tag_id_cid_new(po, old_tag_id_list - to_rm, cid) user_id = po.user_id _tag_rm_by_user_id_list(po, user_id, to_rm) for tag_id in to_add: _zsite_tag_po_new(tag_id, po, cid)
def rec_read_new(po_id, tag_id): mq_rec_topic_has_new(tag_id) times = redis.hget(REDIS_REC_PO_TIMES, po_id) if times >= REDIS_REC_PO_SHOW_TIMES: _user_tag_old_rank(po_id, tag_id, times) else: redis.sadd(REDIS_REC_TAG_NEW%tag_id, po_id)
def append(self, name, id , rank=1): from zkit.fanjian import ftoj name = ftoj(name.decode('utf-8', 'ignore')) ID2NAME = self.ID2NAME if rank is None: rank = 0 value = redis.hget(ID2NAME, id) _append = False if value: value_name, value_rank = value.rsplit('`', 1) if value_name != name: ZSET_CID = self.ZSET_CID CACHE = self.CACHE NAME2ID = self.NAME2ID p = redis.pipeline() for i in self._key(value_name): p.delete(CACHE%i) p.zrem(ZSET_CID%i, id) p.hdel(NAME2ID, value_name) p.execute() _append = True elif int(rank) != int(value_rank): _append = True else: _append = True if _append: if name: self._append(name, id, rank) tag_name = name.replace('`', "'").strip() redis.hset(ID2NAME, id, '%s`%s'%(tag_name, rank)) self._name2id_set(name,id )
def tag_by_name(name): if name.startswith('-'): name = name[1:] low = name.lower() id = redis.hget(REDIS_ALIAS_NAME2ID, low) if not id: id = tag_new(name) return id
def tag_cid_count(tag_id, cid=None): key = REDIS_TAG_CID_COUNT%tag_id if cid is None: count_dict = redis.hgetall(key) r = [] for k, v in count_dict.iteritems(): v = int(v) k = int(k) if v: r.append((k, v)) r.sort(key=itemgetter(0)) return r else: return redis.hget(key, cid)
def tag_cid_count(tag_id, cid=None): key = REDIS_TAG_CID_COUNT % tag_id if cid is None: count_dict = redis.hgetall(key) r = [] for k, v in count_dict.iteritems(): v = int(v) k = int(k) if v: r.append((k, v)) r.sort(key=itemgetter(0)) return r else: return redis.hget(key, cid)
def tag_alias_new(id, name): from model.autocomplete import autocomplete_tag #添加别名 low = name.lower() oid = redis.hget(REDIS_ALIAS_NAME2ID, low) if oid: return tag_alias = TagAlias.get_or_create(name=name) # if not id: # print id, name # raw_input() tag_alias.tag_id = id tag_alias.save() #print "!!!" _tag_alias_new(id, name)
def rec_limit_by_time(user_id, limit): now = time_new_offset() last = redis.hget(REDIS_REC_LAST_TIME, user_id) or 0 times = int((now - int(last) + 59)//60) redis.hset(REDIS_REC_LAST_TIME, user_id, now) return min(times, limit)
def id_by_name(self, name): return redis.hget(self.NAME2ID, name)
def po_score(po): po_id = po.id score = int(redis.hget(REDIS_REC_PO_SCORE, po_id) or 0) return hot(score, 0, po.create_time)
def tag_cid_by_po_id(po_id): if not po_id: return 0 return redis.hget(REDIS_PO_ID2TAG_CID, po_id) or 0
def host_by_id(id): return redis.hget(R_ID_HOST, id) or 0
def rec_read_by_user_id_tag_id(user_id, tag_id): po_id = 0 from_new = False now = time_new_offset() ut_id = (user_id, tag_id) key_to_rec = REDIS_REC_USER_PO_TO_REC%ut_id key_readed = REDIS_REC_USER_TAG_READED%ut_id exists_key_to_rec = redis.exists(key_to_rec) cache_key_to_rec = False for i in xrange(7): #如果有可以推荐的缓存 , 读取缓存 if exists_key_to_rec: po_id_list = redis.zrevrange(key_to_rec, 0, 0) if po_id_list: po_id = po_id_list[0] redis.zrem(key_to_rec, po_id) else: break else: key_tag_new = REDIS_REC_TAG_NEW%tag_id po_id = redis.srandmember(key_tag_new) #print 'srandmember' , po_id if po_id: from_new = True last = redis.zrevrange(key_readed, 0 , 0 , True) if last and (last[0][1] - now) < ONE_HOUR: cache_key_to_rec = True else: cache_key_to_rec = True if cache_key_to_rec: #生成缓存 有效期1天 推荐文章 p = redis.pipeline() #p = redis p.zunionstore(key_to_rec, {key_readed:-1, REDIS_REC_TAG_OLD%tag_id:1}) p.zremrangebyscore(key_to_rec, '-inf', 0) p.expire(key_to_rec, ONE_DAY) p.execute() exists_key_to_rec = True #方便没有的时候跳出循环 #print 'redis.zcard(key_readed)', redis.zcard(key_to_rec) if po_id: redis.zadd(key_readed, po_id, now) if redis.zrank(REDIS_REC_USER_LOG%user_id, po_id) is not None: po_id = 0 if po_id: break if po_id: redis.hincrby(REDIS_REC_PO_TIMES, po_id, 1) if from_new: if redis.hget(REDIS_REC_PO_TIMES, po_id) >= REDIS_REC_PO_SHOW_TIMES: redis.srem(key_tag_new, po_id) _user_tag_old_rank(po_id, tag_id) #else: #redis.zincrby(key, po_id, 1) else: k = random() if k < 0.01: _user_tag_old_rank(po_id, tag_id) return po_id
def _name2id_set(self, name, id): NAME2ID = self.NAME2ID if not redis.hget(NAME2ID, name): redis.hset(NAME2ID, name, id)