def tag_admin_new(id, tag_id_list, rank): id = str(id) for tag_id in tag_id_list: key = REDIS_TAG_ADMIN_TAG_ID%tag_id if not redis.zrank(key, id): p = redis.pipeline() p.zadd(key, id, rank) p.zincrby(REDIS_TAG_ADMIN, tag_id, 1) p.execute()
def tag_admin_rm(id, tag_id_list): id = str(id) for tag_id in tag_id_list: key = REDIS_TAG_ADMIN_TAG_ID%tag_id if redis.zrank(key, id): p = redis.pipeline() p.zrem(key, id) p.zincrby(REDIS_TAG_ADMIN, tag_id, -1) p.execute()
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