#python下redis使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
安装python redis插件:
apt
-
get install python
-
redis
编辑python连接redis脚本
vi redis_conn.py
#!/usr/bin/env python
import
redis
r
=
redis.Redis(host
=
'localhost'
,port
=
6379
,db
=
0
)
#r['yourkey']='yourvalue'
def
get_redis(host_ip
=
'localhost'
,port
=
6379
,db
=
0
):
return
redis.Redis(host
=
host_ip,port
=
port,db
=
db)
进入python命令下执行:
>>>
import
tab
>>>
import
redis_conn
>>>redis_conn.r.
set
(
'key1'
,
'value1'
)
>>>redis_conn.r[
'key1'
]
=
'value1'
>>>redis_conn.r.keys()
#查看所有的key
>>>redis_conn.r.delete(
'key1'
)
#删除一个key
>>>redis_conn.r.[
'key1'
]
#获取key1的value
>>>redis_conn.r.get(
'key1'
)
|
本文转自 王家东哥 51CTO博客,原文链接:http://blog.51cto.com/xiaodongge/1865021