Пример #1
0
    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 )
Пример #2
0
def _zsite_tag_po_new(zsite_id, po, cid, rank=1):
    po_id = po.id
    cid = int(cid)
    tag_po = PoZsiteTag.get_or_create(po_id=po_id, cid=po.cid, zsite_id=zsite_id)
    tag_po.rank = rank
    tag_po.save()

    user_id = po.user_id
    if user_id:
        user_rank = zsite_list_get(user_id, zsite_id, CID_TAG)
        if not user_rank:
            user_rank = zsite_list_new(user_id, zsite_id, CID_TAG)
        else:
            user_rank.rank += 1
            user_rank.save()

    mc_flush(zsite_id, po_id)

    rec_read_new(po_id, zsite_id)
    if cid in REDIS_REC_CID_DICT:
        #print zsite_id, po, cid

        p = redis.pipeline()

        #将分数放到相应的ranged set里面
        key = REDIS_TAG_CID%(zsite_id, cid)
        p.zadd(key, po_id, hot(1, 0, po.create_time))

        key = REDIS_TAG_CID_COUNT%zsite_id
        p.hincrby(key, cid, 1)

        p.execute()

    return tag_po
Пример #3
0
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()
Пример #4
0
def rss_po_new(po, user_tag_id_list):
    id = po.id
    tag2idlist_po.append_id_tag_id_list(id, user_tag_id_list)
    key = REDIS_USER_RSS_PO % po.user_id

    p = redis.pipeline()
    p.lrem(key, id)
    p.rpush(key, id)
    p.execute()
Пример #5
0
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()
Пример #6
0
    def pop_id_tag_id(self, id, tag_id):
        key_tag_id_xid_list = self.key_tag_id_xid_list
        key_xid_tag_id_list = self.key_xid_tag_id_list%id

        p = redis.pipeline()
        p.lrem(key_tag_id_xid_list%tag_id, id)
        p.lrem(key_xid_tag_id_list, tag_id)
        p.execute()

        self._flush_count(tag_id_set)
Пример #7
0
def rss_po_new(po, user_tag_id_list):
    id = po.id
    tag2idlist_po.append_id_tag_id_list(
        id , user_tag_id_list
    )
    key = REDIS_USER_RSS_PO%po.user_id

    p = redis.pipeline()
    p.lrem(key, id)
    p.rpush(key, id)
    p.execute()
Пример #8
0
    def pop_id(self, id):
        key_tag_id_xid_list = self.key_tag_id_xid_list
        key_xid_tag_id_list = self.key_xid_tag_id_list%id

        tag_id_set = redis.lrange(key_xid_tag_id_list, 0, -1)

        p = redis.pipeline()
        for tag_id in tag_id_set:
            p.lrem(key_tag_id_xid_list%tag_id, id)
        p.execute()

        redis.delete(key_xid_tag_id_list)

        self._flush_count(tag_id_set)
Пример #9
0
    def _(cid):
        for tag_id in id_list:
            #将分数放到相应的ranged set里面
            key = REDIS_TAG_CID % (tag_id, cid)
            count = max(redis.zcard(key) - 1, 0)

            p = redis.pipeline()
            p.zrem(key, po_id)

            for i in (REDIS_REC_TAG_NEW, REDIS_REC_TAG_OLD):
                key = i % tag_id
                p.zrem(key, po_id)

            p.hset(REDIS_TAG_CID_COUNT % tag_id, cid, count)
            p.execute()
Пример #10
0
    def _(cid): 
        for tag_id in id_list:
            #将分数放到相应的ranged set里面
            key = REDIS_TAG_CID%(tag_id, cid)
            count = max(redis.zcard(key) - 1, 0)

            p = redis.pipeline()
            p.zrem(key, po_id)

            for i in (REDIS_REC_TAG_NEW, REDIS_REC_TAG_OLD):
                key = i%tag_id
                p.zrem(key, po_id)

            p.hset(REDIS_TAG_CID_COUNT%tag_id, cid, count)
            p.execute()
Пример #11
0
    def id_list_by_str(self, query, limit=7):
        name_list = query.replace('`', "'").split()
        if not name_list:
            return []

        name_list.sort()

        name_key = '`'.join(name_list)
        id_list = self._get_cache(name_key)

        id_list = None #TODO REMOVE
        ZSET_CID = self.ZSET_CID

        if id_list is None:
            mkey = ZSET_CID%name_key

            if len(name_list)>1 and not redis.exists(mkey):
                p = redis.pipeline()
                p.zinterstore(mkey, map(ZSET_CID.__mod__, name_list), 'MAX')
                p.expire(mkey, EXPIRE)
                p.execute()


            id_list = redis.zrevrange(mkey, 0, 10)

            id_list = list(map(int, id_list))

            #id = self.id_by_name(query)
            #if id:
            #    id = int(id)
            #    if len(id_list) > 5:
            #        id_list.insert(4, id)
            #    else:
            #        id_list.append(id)
 
            #id_list = unique(id_list)
            
            self._set_cache(name_key, id_list)
            
            #print redis.zrevrange(mkey, 0, 10,True)
            #print id_list

        return id_list[:limit]
Пример #12
0
    def append_id_tag_id_list(self, id, tag_id_list):
        if not id or not tag_id_list:
            return
        key_tag_id_xid_list = self.key_tag_id_xid_list
        key_xid_tag_id_list = self.key_xid_tag_id_list%id

        tag_id_set = set(tag_id_list)
        key_list = []
        p = redis.pipeline()

        for tag_id in tag_id_set:
            p.lrem(key_xid_tag_id_list, tag_id)
            p.lpush(key_xid_tag_id_list, tag_id)

            key = key_tag_id_xid_list%tag_id
            p.lrem(key, id)
            p.lpush(key, id)

        p.execute()

        self._flush_count(tag_id_set)
Пример #13
0
def _zsite_tag_po_new(zsite_id, po, cid, rank=1):
    po_id = po.id
    cid = int(cid)
    tag_po = PoZsiteTag.get_or_create(po_id=po_id,
                                      cid=po.cid,
                                      zsite_id=zsite_id)
    tag_po.rank = rank
    tag_po.save()

    user_id = po.user_id
    if user_id:
        user_rank = zsite_list_get(user_id, zsite_id, CID_TAG)
        if not user_rank:
            user_rank = zsite_list_new(user_id, zsite_id, CID_TAG)
        else:
            user_rank.rank += 1
            user_rank.save()

    mc_flush(zsite_id, po_id)

    rec_read_new(po_id, zsite_id)
    if cid in REDIS_REC_CID_DICT:
        #print zsite_id, po, cid

        p = redis.pipeline()

        #将分数放到相应的ranged set里面
        key = REDIS_TAG_CID % (zsite_id, cid)
        p.zadd(key, po_id, hot(1, 0, po.create_time))

        key = REDIS_TAG_CID_COUNT % zsite_id
        p.hincrby(key, cid, 1)

        p.execute()

    return tag_po
Пример #14
0
def _po_tag_id_cid_new(po, tag_id_list, cid):
    po_id = po.id
    old_cid = tag_cid_by_po_id(po_id)

    set_cid = False
    if old_cid:
        if old_cid != cid:
            score = po_score(po)

            p = redis.pipeline()
            for tag_id in tag_id_list:
                old_key = REDIS_TAG_CID % (tag_id, old_cid)
                p.zrem(old_key, po_id)

                count_key = REDIS_TAG_CID_COUNT % tag_id
                p.hincrby(count_key, old_cid, -1)

                if cid in REDIS_REC_CID_DICT:
                    new_key = REDIS_TAG_CID % (tag_id, cid)
                    p.zadd(new_key, po_id, score)

                    p.hincrby(count_key, cid, 1)

            p.execute()
Пример #15
0
def _po_tag_id_cid_new(po, tag_id_list, cid):
    po_id = po.id
    old_cid = tag_cid_by_po_id(po_id)

    set_cid = False
    if old_cid:
        if old_cid != cid:
            score = po_score(po)

            p = redis.pipeline()
            for tag_id in tag_id_list:
                old_key = REDIS_TAG_CID%(tag_id, old_cid)
                p.zrem(old_key, po_id)

                count_key = REDIS_TAG_CID_COUNT%tag_id
                p.hincrby(count_key, old_cid, -1)

                if cid in REDIS_REC_CID_DICT:
                    new_key = REDIS_TAG_CID%(tag_id, cid)
                    p.zadd(new_key, po_id, score)

                    p.hincrby(count_key, cid, 1)

            p.execute()
Пример #16
0
def id_host_new(id, host):
    with redis.pipeline() as p:
        p.hset(R_ID_HOST, id, host)
        p.hset(R_HOST_ID, host, id)
        p.execute()
Пример #17
0
def rec_read_po_read_rm(po_id, tag_id_list):
    from model.po_pos import po_viewed_list
    p = redis.pipeline()
    for i in po_viewed_list(po_id):
        p.zrem(REDIS_REC_USER_LOG%i, po_id)
    p.execute()
Пример #18
0
def rec_read_po_tag_rm(po_id, tag_id_list):
    p = redis.pipeline()
    for tag_id in tag_id_list:
        p.zrem(REDIS_REC_TAG_OLD%tag_id, po_id)
        p.srem(REDIS_REC_TAG_NEW%tag_id, po_id)
    p.execute()
Пример #19
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
Пример #20
0
def id_host_new(id, host):
    with redis.pipeline() as p:
        p.hset(R_ID_HOST, id, host)
        p.hset(R_HOST_ID, host, id)
        p.execute()