安装redis-server
yum -y install gcc gcc-c++ #安装编译工具
cd /opt
wget -c http://download.redis.io/releases/redis-3.0.5.tar.gz #下载包
tar xf redis-3.0.5.tar.gz #解压
cd redis-3.0.5
make MALLOC=libc #编译
make PREFIX=/usr/local/redis install #安装
echo ‘PATH=/usr/local/redis/bin:$PATH‘ >> /etc/profile #配置环境变量
source /etc/profile #或 . /etc/profile
mkdir /usr/local/redis/conf #创建目录
cp /opt/redis-3.0.5/redis.conf /usr/local/redis/conf/ #拷贝配置文件
sed -i "46s#/var/run/redis.pid#/usr/local/redis/conf/redis.pid#" /usr/local/redis/conf/redis.conf
sed -i "192s#./#/usr/local/redis/conf/#" /usr/local/redis/conf/redis.conf
sed -i 509s/no/yes/ /usr/local/redis/conf/redis.conf
nohup redis-server /usr/local/redis/conf/redis.conf &> /dev/null &
echo vm.overcommit_memory = 1 >> /etc/sysctl.conf
sysctl -p
lsof -i:6379 #查看端口有没有起来
安装pip命令依赖包
wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz --no-check-certificate
cd setuptools-0.6c11
python setup.py build
python setup.py install
安装pip命令
wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5=834b2904f92d46aaa333267fb1c922bb" --no-check-certificate
tar xf pip-1.5.4.tar.gz
cd pip-1.5.4
python setup.py install
安装redis的Python api
pip install redis
测试
[[email protected] opt]# python
Python 2.6.6 (r266:84292, Nov 22 2013, 12:16:22)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import redis
>>> r = redis.Redis(host=‘10.211.55.4‘, port=6379)
>>> r.set(‘foo‘, ‘Bar‘)
>>> print r.get(‘foo‘)
"Bar"
[[email protected] opt]# redis-cli
127.0.0.1:6379> get foo
"Bar"
redis订阅和发布功能演示
[[email protected] opt]# cat redishelper.py
#!/usr/bin/env python
#coding:utf-8
import redis
class RedisHelper:
def __init__(self):
self.__conn = redis.Redis(host=‘127.0.0.1‘)
self.chan_sub = ‘fm87.7‘ #订阅频道
self.chan_pub = ‘fm87.7‘ #接收频道
def get(self,key):
return self.__conn.get(key)
def set(self,key,value):
self.__conn.set(key,value)
def public(self,msg): #在chan_pub这个频道发布消息
self.__conn.publish(self.chan_pub,msg)
return True
def subscribe(self): #订阅接收
pub = self.__conn.pubsub() #打开收音机
pub.subscribe(self.chan_sub) #订阅频道
pub.parse_response() #等待消息
return pub #开始接收
if __name__ == ‘__main__‘:
t = RedisHelper()
t.public(‘test‘) #发布了test这个消息
在一端执行脚本
python redishelper.py
另一端查看
>>> import redishelper
>>> r = redishelper.RedisHelper()
>>> r.subscribe()
<redis.client.PubSub object at 0x7f1789422950>
>>> recv = r.subscribe()
>>> recv.parse_response()
[‘message‘, ‘fm87.7‘, ‘test‘]
订布发阅演示2
#在一个终端打开这个
>>> import redis
>>> r = redis.Redis(host=‘127.0.0.1‘)
>>> chan = r.pubsub() #打开频道
>>> chan.subscribe("fm100") #调到该频道
>>> chan.parse_response() #测试监听频道
[‘subscribe‘, ‘fm100‘, 1L]
>>> chan.parse_response() #再次执行成阻塞状态等待消息的到来
>>> while True: #写成死循环
... chan.parse_response()
在另一个终端打开发布消息
>>> import redis
>>> p = redis.Redis(host=‘127.0.0.1‘)
>>> p.publish(‘fm100‘,‘Hellow World!!!‘)
1L
回到之前那终端发现已经出来消息
>>> while True:
... chan.parse_response()
...
[‘message‘, ‘fm100‘, ‘Hellow World!!!‘]