標籤:
試用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 );
redis的PHP擴充包安裝方法