Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5096534
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类: Python/Ruby

2011-03-04 00:35:55

这是一个用Redis存储好友关系的python下使用Redis的例子。作者是Eric Florenzano (@)。相信对各位人生苦短用python的同学是一个好的入门例子。
例子说明:其中虽然用了follow和block等字眼,但是他的关系不是一个微博系统的单向关注,而是类似SNS的双向关系。

  1. """
  2. I've been thinking lately about how perfect Redis would be for storing a
  3. simple social graph. I posited that it would be relatively few lines of code,
  4. and that it'd be clean code too. So here it is: a basic social graph built on Redis.
  5. """

  6. class FriendGraph(object):

  7.     def __init__(self, ring):
  8.         self.ring = ring

  9.         # These keys are intentionally short, so as to save on memory in redis
  10.         self.FOLLOWS_KEY = 'F'
  11.         self.FOLLOWERS_KEY = 'f'
  12.         self.BLOCKS_KEY = 'B'
  13.         self.BLOCKED_KEY = 'b'

  14.     def follow(self, from_user, to_user):
  15.         forward_key = '%s:%s' % (self.FOLLOWS_KEY, from_user)
  16.         forward = self.ring.sadd(forward_key, to_user)
  17.         reverse_key = '%s:%s' % (self.FOLLOWERS_KEY, to_user)
  18.         reverse = self.ring.sadd(reverse_key, from_user)
  19.         return forward and reverse

  20.     def unfollow(self, from_user, to_user):
  21.         forward_key = '%s:%s' % (self.FOLLOWS_KEY, from_user)
  22.         forward = self.ring.srem(forward_key, to_user)
  23.         reverse_key = '%s:%s' % (self.FOLLOWERS_KEY, to_user)
  24.         reverse = self.ring.srem(reverse_key, from_user)
  25.         return forward and reverse

  26.     def block(self, from_user, to_user):
  27.         forward_key = '%s:%s' % (self.BLOCKS_KEY, from_user)
  28.         forward = self.ring.sadd(forward_key, to_user)
  29.         reverse_key = '%s:%s' % (self.BLOCKED_KEY, to_user)
  30.         reverse = self.ring.sadd(reverse_key, from_user)
  31.         return forward and reverse

  32.     def unblock(self, from_user, to_user):
  33.         forward_key = '%s:%s' % (self.BLOCKS_KEY, from_user)
  34.         forward = self.ring.srem(forward_key, to_user)
  35.         reverse_key = '%s:%s' % (self.BLOCKED_KEY, to_user)
  36.         reverse = self.ring.srem(reverse_key, from_user)
  37.         return forward and reverse

  38.     def get_follows(self, user):
  39.         follows = self.ring.smembers('%s:%s' % (self.FOLLOWS_KEY, user))
  40.         blocked = self.ring.smembers('%s:%s' % (self.BLOCKED_KEY, user))
  41.         return list(follows.difference(blocked))

  42.     def get_followers(self, user):
  43.         followers = self.ring.smembers('%s:%s' % (self.FOLLOWERS_KEY, user))
  44.         blocks = self.ring.smembers('%s:%s' % (self.BLOCKS_KEY, user))
  45.         return list(followers.difference(blocks))

  46.     def get_blocks(self, user):
  47.         return list(self.ring.smembers('%s:%s' % (self.BLOCKS_KEY, user)))

  48.     def get_blocked(self, user):
  49.         return list(self.ring.smembers('%s:%s' % (self.BLOCKED_KEY, user)))

 

 

阅读(1378) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~