CentOS 7
Ads

Redis : Use it on Python
2016/07/21
 
This is the example to use Redis on Python.
[1] Install Python Redis Client Library.
# install from EPEL

[root@dlp ~]#
yum --enablerepo=epel -y install python-redis
[2] Basic usage on Python.
[cent@dlp ~]$
vi use_redis.py
#!/usr/bin/env python

import redis

client = redis.StrictRedis(host='127.0.0.1', port=6379, db=0, password='password')

# set and get Key
client.set("key01", "value01")
print "key01.value :", client.get("key01")

# append and get Key
client.append("key01", ",value02")
print "key01.value :", client.get("key01")

client.set("key02", 1)

# increment
client.incr("key02", 100)
print "key02.value :", client.get("key02")

# decrement
client.decr("key02", 51)
print "key02.value :", client.get("key02")

# list
client.lpush("list01", "value01", "value02", "value03")
print "list01.value :", client.lrange("list01", "0", "2")

# hash
client.hmset("hash01", {"key01": "value01", "key02": "value02", "key03": "value03"})
print "hash01.value :", client.hmget("hash01", ["key01", "key02", "key03"])

# set
client.sadd("set01", "member01", "member02", "member03")
print "set01.value :", client.smembers("set01")

# run

[cent@dlp ~]$
python use_redis.py

key01.value : value01
key01.value : value01,value02
key02.value : 101
key02.value : 50
list01.value : ['value03', 'value02', 'value01']
hash01.value : ['value01', 'value02', 'value03']
set01.value : set(['member02', 'member03', 'member01'])
 
Tweet