redis学习笔记
参考:http://hi.baidu.com/lhuylaqqqjdqwxe/item/49839c0aa3a4ade1f45ba6ef
一 安装
$ wget http://redis.googlecode.com/files/redis-2.2.12.tar.gz $cd /root/redis-2.2.12/src $make PREFIX=/usr/local/redis $make test $root@yee:~/redis-2.6.2/src# make test You need 'tclsh8.5' in order to run the Redis test make: *** [test] Error 1
需要安装tcl :
$apt-get install tcl8.5 $make PREFIX=/usr/local/redis install
二 redis配置:
$cd /usr/local/redis/ $mkdir {etc,var} $cp ~/redis-2.6.2/redis.conf ./etc/
下面启动redis服务.
$./redis-server
注意这种方式启动redis 使用的是默认配置。也可以通过启动参数告诉redis使用指定配置文件使用下面命令启动.
$ ./redis-server redis.conf
redis.conf是一个默认的配置文件。我们可以根据需要使用自己的配置文件。
启动redis服务进程后,就可以使用测试客户端程序redis-cli和redis服务交互了.
比如
ubuntu@yee:~$ cd /usr/local/redis/ ubuntu@yee:/usr/local/redis$ ls bin etc var ubuntu@yee:/usr/local/redis$ bin/redis-cli redis 127.0.0.1:6379> set foo bar OK redis 127.0.0.1:6379> get foo "bar"
三、redis 的一些基本指令学习
3.1 redis pipeline
pipeline 将多个redis命令打包在一块,发送给服务器,服务器再逐条执行命令,打包执行结果一并返回。
假如在普通的模式下,client 发送一条命令给服务器,服务器返回一条结果给client,如果网络延迟是0.2s,那么大概1s也就只能处理5条命令,这并不能充分利用到redis的能力 。
>>> pipeline = r.pipeline() >>> pipeline.rpush('chat','one') <redis.client.Pipeline object at 0x7fa79d497c90> >>> pipeline.rpush('chat','two') <redis.client.Pipeline object at 0x7fa79d497c90> >>> pipeline.execute() [1L, 2L] >>> r.lrange('chat',0,-1) ['one', 'two'] >>>
四 python 操作redis 学习:
easy_install redis >>> import redis >>> r = redis.Redis(host='localhost',port=6379,db=0) >>> r.set('foo','bar') True >>> r.get('foo') 'bar' >>> r.delete('foo') True >>> r.dbsize() 0L >>> r.rpush('ml','aa') 1L >>> r.rpush('ml','bb') 2L >>> r.rpush('ml','cc') 3L >>> r.rpush('ml','dd') 4L >>> r.lrange('ml',0,-1) ['aa', 'ad', 'cc', 'dd'] INCR :自增变量 >>> room_id = rc.incr("KEY") >>> room_id 1 >>> room_id = rc.incr("KEY") >>> room_id 2 redis keys 还接受正则表达式: 如: redis 127.0.0.1:6379> mset one 1 two 2 three 3 OK redis 127.0.0.1:6379> keys *o* 1) "one" 2) "two" redis zadd :adds all specified members with the specified scores to the sorted set at key . Time complexity : O(log(N)) redis 127.0.0.1:6379> zadd myset 1 "one" (integer) 1 redis 127.0.0.1:6379> zadd myset 1 "uno" (integer) 1 redis 127.0.0.1:6379> zadd myset 2 "two" redis 127.0.0.1:6379> zrange myset 0 -1 1) "one" 2) "uno" 3) "two" redis 127.0.0.1:6379> zrange myset 0 -1 withscores 1) "one" 2) "1" 3) "uno" 4) "1" 5) "two" 6) "2" redis 127.0.0.1:6379> zadd myset 1 "three" (integer) 1 redis 127.0.0.1:6379> zrange myset 0 -1 1) "one" 2) "three" 3) "uno" 4) "two" redis 127.0.0.1:6379> zrange myset 0 -1 withscores 1) "one" 2) "1" 3) "three" 4) "1" 5) "uno" 6) "1" 7) "two" 8) "2"
添加 删除使用方法 :
>>> rc.zadd("online23","user1",2) 1 >>> rc.zadd("online23","user2",3) 1 >>> rc.zadd("online23","user3",3) 1 >>> rc.zrange("online23",0,-1) ['user1', 'user2','user3'] >>> rc.zrem("online23","user3") True >>> rc.zrange("online23",0,-1) ['user1', 'user2']
www.notewo.com