Python Redis数据结构服务器

首先下载redis:

pip install redis
在开始使用redis时会出现以下的问题:

Python Redis数据结构服务器_第1张图片

原因是redis没有启动,解决方法是安装了redis-tool,运行redis-cli

如果此时发生如下错误:


你要安装redis-server:

sudo apt-get install redis-server
再次运行redis-cli时出现如下的效果即为正常启动:

Python Redis数据结构服务器_第2张图片

接下来进行字符串的一些操作:set、get、getset、getrange、setrange、mset、delete、incr、incrbyfloat、decr

>>> import redis
>>> conn = redis.Redis()
>>> conn.keys('*')
[]
>>> conn.set('secret','ni!')
True
>>> conn.set('carats',24)
True
>>> conn.set('fever','101.5')
True
>>> conn.get('secret')
'ni!'
>>> conn.incr('carats')
25
>>> conn.getset('secret','icky-icky-icky-ptang-zoop-boing!')
'ni!'
>>> conn.getrange('secret',-6,-1)
'boing!'
>>> conn.setrange('secret',0,'ICKY')
32
>>> conn.get('secret')
'ICKY-icky-icky-ptang-zoop-boing!'
>>> conn.delete('fever')
1
>>> conn.set('fever','101.5')
True
>>> conn.incrbyfloat('fever')
102.5


列表操作:

>>> conn.lpush('zoo','bear')
1L
>>> conn.lpush('zoo','alligator','duck')
3L
>>> conn.linsert('zoo','before','bear','beaver')
4
>>> conn.linsert('zoo','after','bear','cassowary')
5
>>> conn.lset('zoo',2,'marmoset')
True
>>> conn.rpush('zoo','yak')
6L
>>> conn.lindex('zoo',3)
'bear'
>>> conn.lrange('zoo',0,2)
['duck', 'alligator', 'marmoset']
>>> conn.ltrim('zoo',1,4)
True
>>> conn.lrange('zoo',0,-1)
['alligator', 'marmoset', 'bear', 'cassowary']

哈希表操作:创建一个Redis的哈希表song,并对它进行操作

>>> conn.hmset('song',{'do':'a deer','re':'about a deer'})
True
>>> conn.hset('song','mi','a note to follow re')
1L
>>> conn.hget('song','mi')
'a note to follow re'
>>> conn.hmget('song','re','do')
['about a deer', 'a deer']
>>> conn.hkeys('song')
['do', 're', 'mi']
>>> conn.hvals('song')
['a deer', 'about a deer', 'a note to follow re']
>>> conn.hlen('song')
3
>>> conn.hgetall('song')
{'do': 'a deer', 'mi': 'a note to follow re', 're': 'about a deer'}
>>> conn.hsetnx('song','fa','a note that rhymes wirh la')
1L


集合操作:

如果重新用conn.sadd('zoo','duck','goat','turkey'),会出现冲突问题

Python Redis数据结构服务器_第3张图片

>>> conn.sadd('boo','duck','goat','turkey')
3
>>> conn.scard('boo')
3
>>> conn.smembers('boo')
set(['turkey', 'goat', 'duck'])
>>> conn.srem('boo','turkey')
1
>>> conn.sadd('better_boo','tiger','wolf','duck')
3
>>> conn.sinter('boo','better_boo')
set(['duck'])
>>> conn.sinterstore('fowl_boo','boo','better_boo')
1
>>> conn.sunion('boo','better_boo')
set(['tiger', 'wolf', 'goat', 'duck'])
>>> conn.sunionstore('fabulous_boo','boo','better_boo')
4
>>> conn.sdiff('boo','better_boo')
set(['goat'])


有序集合:   以时间序列为例子,利用时间戳跟踪用户的登录。这里时间用unix的epoch值

>>> import time
>>> now=time.time()
>>> now
1454420094.849293
>>> conn.zadd('logins','smeagol',now)
1
>>> conn.zadd('logins','sauron',now+(5*60))
1
>>> conn.zadd('logins','bilbo',now+(2*60*60))
1
>>> conn.zadd('logins','treebeard',now+(24*60*60))
1
>>> conn.zrank('logins','bilbo')
2
>>> conn.zscore('logins','bilbo')
1454427294.849293
>>> conn.zrange('logins',0,-1)
['smeagol', 'sauron', 'bilbo', 'treebeard']
>>> conn.zrange('logins',0,-1,withscores=True)
[('smeagol', 1454420094.849293), ('sauron', 1454420394.849293), ('bilbo', 1454427294.849293), ('treebeard', 1454506494.849293)]


位图:省空间且快速的处理超大集合数字的方式

>>> days=['2016-01-31','2016-02-01','2016-02-02']
>>> big_spender=1089
>>> tire_kicker=40459
>>> late_joiner=550212
>>> conn.setbit(days[0],big_spender,1)
0
>>> conn.setbit(days[0],tire_kicker,1)
0
>>> conn.setbit(days[1],big_spender,1)
0
>>> conn.setbit(days[2],big_spender,1)
0
>>> conn.setbit(days[2],late_joiner,1)
0
>>> for day in days
  File "<stdin>", line 1
    for day in days
                  ^
SyntaxError: invalid syntax
>>> for day in days:
... conn.bitcount(day)
  File "<stdin>", line 2
    conn.bitcount(day)
       ^
IndentationError: expected an indented block
>>> for day in days:
...     conn.bitcount(day)
... 
2
1
2
>>> conn.getbit(days[1],tire_kicker)
0
>>> conn.bitop('and','everyday',*days)
68777L
>>> conn.bitcount('everyday')
1
>>> conn.getbit('everyday',big_spender)
1
>>> conn.bitop('or','everyday',*days)
68777L
>>> conn.bitcount('alldays')
0
>>> conn.bitop('or','alldays',*days)
68777L
>>> conn.bitcount('alldays')
3

缓存和过期:expire()函数

你可能感兴趣的:(Python Redis数据结构服务器)