Redis缓存数据库的安装与配置(2)

1.为php安装redis客户端扩展

wget https://github.com/nicolasff/phpredis/archive/master.zip

tar xf phpredis-master.tar.gz -C /usr/src/

cd /usr/src/phpredis-master/

/usr/local/php/bin/phpize

./configure --with-php-config=/usr/local/php/bin/php-config

make && make install

修改php.ini设置,重启php

#将php.ini配置文件中的extension_dir修改成如下:
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/"

echo "extension = redis.so" >> /usr/local/php/lib/php.ini

2. 开发php程序操作redis

[root@redis01 scripts]# cat redis.php
#!/bin/bash

<?php

$redis = new Redis();
$redis -> connect("172.16.1.2",6379);
$redis -> auth("123456");
$redis -> set("name","yunjisuan");
$var = $redis -> get("name");
echo "$var ";

?>
安装Python redis客户端操作redis

tar xf redis-2.10.1.tar.gz
cd redis-2.10.1
python setup.py install

[root@redis01 redis-2.10.1]# 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        #引用redis支持库
>>> r = redis.Redis(host='10.0.0.135',port='6379',password='yunjisuan') #建立redis数据库的连接对象(面向对象方式)
>>> r.set('name','benet')   #操作对象调用set方法写入数据
True
>>> r.get('name')           #操作对象调用get方式读取数据
'benet'
>>> r.dbsize()              #操作对象查看redis数据库的数据条数
1L
>>> r.keys()                #查看所有的key
['name']
>>> exit()                  #退出

通过Web界面连接Python程序展示redis

开发Python脚本

cat python-redis.py
#/usr/bin/python

from wsgiref.simple_server import make_server
import redis

def get_redis():
    r = redis.Redis(host='10.0.0.135',port='6379',password='yunjisuan',db=0)
    r.set('name','yunyunyun')
    return r.get('name')
   
def hello_world_app(environ,start_response):
    status = '200 OK'   #HTTP Status
    headers = [('Content-type','text/plain')]   #HTTP Headers
    start_response(status,headers)
   
    # The returned object is going to be printed
    return get_redis()

httpd = make_server('',8000,hello_world_app)
print "Serving on port 8000..."

# Server until process is killed
httpd.serve_forever()

原文地址:https://www.cnblogs.com/mushou/p/9436321.html