예제 #1
0
 def _append(self, name, id, rank=1):
     CACHE = self.CACHE
     ZSET_CID = self.ZSET_CID
     for i in self._key(name):
         redis.delete(CACHE%i)
         #print ZSET_CID%i, id, rank
         redis.zadd(ZSET_CID%i, id, rank)
예제 #2
0
def rec_read(user_id, limit):
    limit = rec_limit_by_time(user_id, limit)
    result = set()
    if limit > 0:
        key_log = REDIS_REC_USER_LOG%user_id
        picker = RecTagPicker(user_id)
        for i in xrange(limit):

            tag_id = picker()
            if not tag_id:
                break

            po_id = rec_read_by_user_id_tag_id(user_id, tag_id)
            if not po_id:
                picker.delete(tag_id)
                continue

            result.add(po_id)

        if result:
            now = time_new_offset()
            t = []
            offset = 0
            for i in result:
                t.append(i)
                t.append(offset+now)
                offset += 0.01

            redis.zadd(key_log, *t)

    return result
예제 #3
0
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)
예제 #4
0
    def _flush_count(self, tag_id_set):
        key_tag_id_xid_list = self.key_tag_id_xid_list
        key_tag_count = self.key_tag_count

        for tag_id in tag_id_set:
            key = key_tag_id_xid_list%tag_id
            redis.zadd(key_tag_count, tag_id, redis.llen(key))
예제 #5
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)
예제 #6
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)
예제 #7
0
def po_score_incr(po, user_id, score=1):
    po_id = po.id
    cid = tag_cid_by_po_id(po_id)
    tag_id_list = tag_id_list_by_po_id(po_id=po_id)
    if tag_id_list:
        redis.hincrby(REDIS_REC_PO_SCORE, po_id, score)
        for tag_id in tag_id_list:
            rec_read_user_topic_score_incr(user_id, tag_id, score)
            if cid:
                key = REDIS_TAG_CID % (tag_id, cid)
                redis.zadd(key, po_id, po_score(po))
예제 #8
0
def po_score_incr(po, user_id, score=1):
    po_id = po.id
    cid = tag_cid_by_po_id(po_id)
    tag_id_list = tag_id_list_by_po_id(po_id=po_id)
    if tag_id_list:
        redis.hincrby(REDIS_REC_PO_SCORE, po_id, score)
        for tag_id in tag_id_list:
            rec_read_user_topic_score_incr(user_id, tag_id, score)
            if cid:
                key = REDIS_TAG_CID%(tag_id, cid)
                redis.zadd(key, po_id, po_score(po))
예제 #9
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)
예제 #10
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
예제 #11
0
 def add(self, name, id, rank=1):
     name = name.lower().strip()
     redis.zadd(self.ZSET_CID%name, id, rank)