1.wget http://zsj-linux.oss-cn-hangzhou.aliyuncs.com/redis-3.2.9.tar.gz 2.tar -zxvf redis-3.2.9.tar.gz 3.rm -rf redis-3.2.9.tar.gz 4.cd redis-3.2.9/ 5.Make 6.vim redis.conf 修改daemonize为yes 7../redis-server /opt/redis-3.2.9/redis.conf 8.ps -aux |grep redis
一、Redis中的字符串linux
命令:redis
GET:获取存储在给定键中的值vim
get hello world
SET:设置存储在给定键中的值缓存
set hello
DEL:删除存储在给定键中的值(这个命令能够用于全部类型)数据结构
del hello
二、redis中的列表app
命令:函数
RPUSH:将给定值推入列表的右端post
rpush list-key item rpush list-key item2 rpush list-key item
LRANGE:获取列表在给定范围上的全部值spa
lrange list-key 0 -1
LINDEX:获取列表在给定位置上的单个元素code
lindex list-key 1
LPOP:从列表的左端弹出一个值,并返回被弹出的值
lpop list-key
3. redis的集合
SADD:将给定元素添加到集合
sadd set-key item sadd set-key item2 sadd set-key item
返回1证实添加成功,返回0则表示元素已经存在
SMEMBERS:返回集合包含的全部元素
smembers set-key
SISMEMBER:检查给定元素是否存在集合中
sismember set-key item sismember set-key item3
返回0证实不存在,1为存在
SREM:若是给定的元素存在于集合中,那么移除这个元素
srem set-key
3. redis散列
HSET:在散列里面关联起给定的键值对
hset hash-key sub-key1 value1 hset hash-key sub-key2 value2 hset hash-key sub-key1 value1
返回0,键值已存在,1为存在。添加成功
HGET:获取指定散列键的值
hget hash-key sub-key2
HGETALL:获取散列包含的全部键值对
hgetall hash-key
HDEL:若是给定健存在于散列里面,那么移除这个键
hdel hash-key sub-key1
1.对文章进行投票
准备好须要用到的常量,计算文章的投票截止时间,检查是否还能够对文章进行投票,从article:id里面取出文章的id,若是用户第一次投票,增长投票数和评分
article_id = article.partition(':')[-1]
ONE_WEEK_IN_SECONDS = 7*86400 VOTE_SCORE = 432
def article_vote(conn,user,article); cutoff=time.time - ONE_WEEK_IN_SECONDS
if conn.zscore('time:',article)<cutoff; return;
article_id = article.partition(':')[-1]
if conn.sadd('voted:'+article_id,user); conn.zincrby('score:',article,VOTE_SCORE) conn.hincrby(article,'votes',1)
2.发布并获取文章
生成一个新的ID,将发布文章的用户添加到文章的已投票用户名单里,而后这个名单过时时间为一周,将文章信息存储到一个散列里面,将文章添加到根据发布时间排序的有序集合和根据评分排序的有序集合里面
def post_article(conn,user,title,link); article_id = str(con.incr('article:'))
voted = 'voted:' +article_id conn.sadd(voted,user) conn.expire(voted,ONE_WEEK_IN_SECONDS) now = time.time()
article = 'article:'+article_id conn.hmset(article,{ 'title':title, 'link':link, 'poster':user, 'time':now, 'votes':1, })
conn.zadd('score:',article,now+VOTE_SCORE) conn.zadd('time:',article,now) return article_id
设置获取文章的起始索引和结束索引,获取多个文章id,根据文章ID获取文章的详细信息
ARTICLES_PER_PAGE = 25 def get_articles(conn,page,order='score:'); start = (page-1)*ARTICLES_PER_PAGE end = start + ARTICLES_PER_PAGE -1
ids = conn.zrevrange(order,start,end)
articles = [] for id in ids: article_data = conn.hgetall(id) article_data['id'] = id articles.append(article_data) return articles
3.对文章进行分组
构建存储文章信息的键名,将文章添加到它所属的群组里面,从群组里面移除文章
def add_remove_groups(conn,article_id,to_add[],to_remove=[]); article = 'article:'+article_id
for group in to_add: conn.sadd('group:' +group,article)
for group in to_remove: conn.srem('group:'+group,article)
为每一个群组的每种排序顺序都建立一个键,检查是否有已缓存的排序结果若是没用的话就如今进行排序,根据评分或发布时间对群组文章进行排序,让redis在60秒以后自动删除这个有序排序,调用以前定义的get_articles()函数来进行分页并获取文章数据
def get_group_articles(conn,group,page,order='score:'): key = order+group
if not conn.exists(key):
conn.zinterstore(key, ['group:'+group,order], aggregate = 'max', )
conn.expire(key,60)
return get_articles(conn,page,key)