zuroc avatar zuroc committed 7b3dcd2

f

Comments (0)

Files changed (2)

Add a comment to this file

misc/python-libmemcached/dist/python_libmemcached-0.40dev-py2.7-linux-x86_64.egg

Binary file modified.

+#coding:utf-8
+import _env
+from config import Model , ModelMc, redis, redis_key
+from time import time
+
+FEED_LIMIT = 1042
+
+class Feed(object):
+
+    def __init__(self, prefix):
+        #所有发布微博用户的id和最新时间
+        self.REDIS_FEED_LAST = redis_key('FeedLast%s'%prefix)
+
+        #某个用户发布的  id
+        self.REDIS_FEED_USER = redis_key('FeedUser%s'%prefix , '%s')
+
+        #某个用户合并最新的 id 之后的消息流
+        self.REDIS_FEED_MERGE = redis_key('FeedMerge%s'%prefix, '%s')
+
+        #每个用户最后一次merge的id
+        self.REDIS_FEED_MERGE_LAST = redis_key('FeedMergeLast%s'%prefix, '%s')
+
+    def follow_new(self, from_id, to_id):
+        redis.zadd(self.REDIS_FEED_MERGE_LAST%from_id, 0, to_id)
+
+    def new(self, id, user_id):
+        REDIS_FEED_USER = self.REDIS_FEED_USER
+        REDIS_FEED_LAST = self.REDIS_FEED_LAST
+
+        now = time()
+
+        key = REDIS_FEED_USER%user_id
+
+        p = redis.pipeline()
+
+        p.zadd(REDIS_FEED_LAST, now, user_id)
+        p.zadd(key, now, id)
+        p.zremrangebyrank(key, 0, -FEED_LIMIT)
+        p.execute()
+
+    def merge_by_follow_user_id_list(
+        self, user_id, follow_user_id_list, last_id=None, limit=42
+    ):
+        if not follow_user_id_list:
+            return []
+        REDIS_FEED_MERGE = self.REDIS_FEED_MERGE
+        REDIS_FEED_MERGE_LAST = self.REDIS_FEED_MERGE_LAST
+        REDIS_FEED_USER = self.REDIS_FEED_USER
+        REDIS_FEED_LAST = self.REDIS_FEED_LAST
+
+        #print REDIS_FEED_LAST, REDIS_FEED_USER, REDIS_FEED_MERGE_LAST, REDIS_FEED_MERGE
+
+        key = REDIS_FEED_MERGE%user_id
+
+        key_po_list = REDIS_FEED_MERGE_LAST%user_id
+
+        p = redis.pipeline()
+        if redis.exists(key):
+            key_po_list_tmp = key_po_list+'.'
+            redis.zinterstore(key_po_list_tmp , {key_po_list:-1, REDIS_FEED_LAST:1})
+            id_list = redis.zrangebyscore(key_po_list_tmp, '(0', '+inf')
+
+            p.zunionstore(key_po_list, (key_po_list_tmp, key_po_list))
+
+            #找到更新内容 , 并追加到key
+            key_list = [REDIS_FEED_USER%i for i in id_list]
+            key_list.append(key)
+            p.zunionstore(key, key_list, 'MAX')
+
+            p.delete(key_po_list_tmp)
+        else:
+            r = []
+            for i in follow_user_id_list:
+                r.append(0)
+                r.append(i)
+
+            p.delete(key_po_list)
+            p.zadd( key_po_list , * r)
+
+            p.zunionstore(key, tuple(REDIS_FEED_USER%i for i in follow_user_id_list))
+            p.zinterstore(key_po_list , (key_po_list, REDIS_FEED_LAST), 'MAX')
+
+        p.zremrangebyrank(key, 0, -FEED_LIMIT)
+        p.execute()
+
+        if last_id:
+            begin = redis.zrevrank(key, last_id)
+            if begin is None:
+                begin = 0
+            else:
+                begin += 1
+        else:
+            begin = 0
+
+        return redis.zrevrange(key, begin, begin+limit)
+
+
+    def rm(self, user_id, id):
+        redis.zrem(self.REDIS_FEED_USER%user_id, id)
+
+    def flush(self, user_id):
+        redis.delete(self.REDIS_FEED_MERGE%user_id)
+
+
+if __name__ == '__main__':
+    #po_feed = Feed('Po')
+    user_id = 2
+    po_id = 1
+    #po_feed.new(po_id, user_id)
+
+    #print po_feed.merge([2], 2)
+    print lnav_reply(2, 3, 1)
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.