redis的PHP扩展包安装方法

试用Redis安装、php环境连接、测试 

 

Redis介绍

 

    Redis本质上一个Key/Value数据库,与Memcached类似的NoSQL型数据库,但是他的数据可以持久化的保存在磁盘上,解决了服务重启后数据不丢失的问题,他的值可以是string(字符串)、list(列表)、sets(集合)或者是ordered  sets(被排序的集合),所有的数据类型都具有push/pop、add/remove、执行服务端的并集、交集、两个sets集中的差别等等操作,这些操作都是具有原子性的,Redis还支持各种不同的排序能力

 

    Redis 2.0更是增加了很多新特性,如:提升了性能、增加了新的数据类型、更少的利用内存(AOF和VM)

 

    Redis支持绝大部分主流的开发语言,如:C、Java、C#、PHP、Perl、Python、Lua、Erlang、Ruby等等

 

    官网:http://code.google.com/p/redis/

 

 

 

安装过程

 

最新稳定版,Redis 2.0.4 stable

 

wget http://redis.googlecode.com/files/redis-2.0.4.tar.gz

 

tar zxf redis-2.0.4.tar.gz

 

cd redis-2.0.4

 

与其它软件不同的是,不需要configure。

 

make

 

装完了。

 

 

 

创建一个目录

 

mkdir /usr/local/redis2

 

cp redis-server redis-benchmark redis-cli redis.conf   /usr/local/redis2

 

 

 

启动:

 

./redis-server > /dev/null &

 

 

 

测试:

 

    存值:

 

./redis-cli set hx value

 

取值:

 

./redis-cli get hx

 

 

 

安装phpredis模块

 

 

 

https://github.com/owlient/phpredis

 

 

 

下载phpredis

 

解压

 

shell> cd phpredis

 

shell> /usr/local/php/bin/phpize 这个phpize是安装php模块的

 

shell> ./configure –with-php-config=/usr/local/php/bin/php-config

 

shell> make

 

shell> make install

 

接下来在php.ini中添加extension=redis.so 先要看看有没有extension_dir=/…….

 

重启apache或者nginx

 

 

 

php代码测试

 

$redis = new Redis();

 

$redis->connect(‘127.0.0.1′,6379);

 

$redis->set(‘test’,'hello world!’);

 

echo $redis->get(‘test’);

 

?>

 

   输出hello world!

 

   http://code.google.com/p/php-redis/

 

 

 

Redis主从配置

 

REDIS主从配置相当简单,一些文章啰里罗嗦的写了一大篇,其实就两句话:

 

打开从机的redis.conf

 

 Port 6381 (注:不能跟主机的一样)

 

 Sleverof 10.0.0.149 6383 (注:ip为主机IP,6383为主机REDIS端口号)

 

先重启主机,再重启从机

 

运行./redis-server redis.conf

 

若出现:

 

 

 

 

的样子,说明配置成功

 

--------------------------------------

php-redis客户端使用方法

<?php
require 'redis.php';
require 'redis_pool.php';
require 'redis_peer.php';
class note extends redis_peer {}
$note = new note();
# Create note, primary key is generated automatically
$id = $note->insert( array('title' => 'Hello', 'body' => 'world!') );
# Update note
$id = $note->update( $id, array('body' => 'wwwwworld!') );
# Get some note by primary key
$note_data = $note->get_by_id( $id );
# Delete note $note->delete( $id );

 

posted @ 2015-02-03 23:23  谦信君  阅读(288)  评论(0编辑  收藏  举报