Beispiel #1
0
def feed_import_user_rm(user_id, feed_import_id):
    key = REDIS_FEED_IMPORT_USER_ID_LIST % user_id
    redis.srem(key, feed_import_id)
    length = redis.scard(key)
    if length:
        redis.zadd(REDIS_FEED_IMPORT_USER, user_id, length)
    else:
        redis.zrem(REDIS_FEED_IMPORT_USER, user_id)
Beispiel #2
0
def feed_import_user_rm(user_id, feed_import_id):
    key = REDIS_FEED_IMPORT_USER_ID_LIST%user_id
    redis.srem(key, feed_import_id)
    length = redis.scard(key)
    if length:
        redis.zadd(REDIS_FEED_IMPORT_USER, user_id, length)
    else:
        redis.zrem(REDIS_FEED_IMPORT_USER, user_id)
Beispiel #3
0
def rec_read_user_topic_score_incr(user_id, tag_id, score=1, tag_score=None):
    key = REDIS_REC_USER_TAG%user_id

    if score == inf:
        score_list = redis.zrevrange(key, 0, 0, True, int)
        if score_list:
            score = max(1, score_list[0][1])
        else:
            score = 1
        redis.zadd(key, tag_id, score)
    elif score == ninf:
        redis.zrem(key, tag_id)
    else:
        redis.zincrby(key, tag_id, score)

    if tag_score is None:
        tag_score = score
    redis.zincrby(REDIS_REC_TAG, tag_id, tag_score)

    # zrank <  REDIS_REC_USER_TAG_LIMIT的时候 
    # 并且不在读完的redis时候 , 进入候选的推荐主题
    if redis.zrevrank(key, tag_id) < REDIS_REC_USER_TAG_LIMIT :
        if not redis.sismember(REDIS_REC_TAG_USER_IS_EMPTY%tag_id, user_id):
            _rec_topic_new_by_user_id_topic_id_score(user_id, tag_id)
Beispiel #4
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
Beispiel #5
0
 def pop_alias(self, name, id):
     ZSET_CID = self.ZSET_CID
     for i in self._key(name):
         redis.zrem(ZSET_CID%i, id)