Redis 是一个高性能的key-value数据库。 redis的出现,很大程度补偿了memcached这类keyvalue存储的不足,在部 分场合可以对关系数据库起到很好的补充作用。它提供了Python,Ruby,Erlang,PHP客户端,使用很方便。问题是这个项目还很新,可能还不足够稳定,而且没有在实际的一些大型系统应用的实例。此外,缺乏mc中批量get也是比较大的问题,始终批量获取跟多次获取的网络开销是不一样的。

Redis 的写入和读取性能相当不错,在一台主流配置的 1U Linux 服务器上,基本可以达到 11 万次每秒的写入和 8.1 万次的读取。能够取得这样的性能是因为 Redis 在运行时会把整个数据集都放在内存中进行操作,通过定时或不定时的方式将数据写到磁盘。



Rediska Document:


在 Ubuntu下非常简单
aptitude install redis-server


cd /usr/src
[ -d Rediska ] || git clone git://
cd Rediska && git pull && rsync -a ./library/ /usr/share/php/

如果是使用Zend Framework的用户,安装有点不同,过程如下:

  1. 首先将Rediska放到Library文件夹中;
  2. 然后编辑 application.ini,增加下面代码:
    autoloaderNamespaces[] = “Rediska”
    pluginpaths.Rediska_Zend_Application_Resource = “Rediska/Zend/Application/Resource”
    resources.rediska.namespace = “Application_” = ‘’
    resources.rediska.servers.0.port = 6379



现在我们开始尝试存储一些数据。下面分别演示四种 Redis 数据类型的使用方法:Keys, Lists, Sets和 Sorted Sets.

Keys (单值)


$firstname = 'kevin';

要存储这个值,首先初始化一个Key’,命名为firstname’ ;

require_once 'Rediska/Key.php';
$Key = new Rediska_Key('firstname');



只需要使用setValue()就可以将 ‘kevin’立即存储到Redis的内存中,随后可能会存储到磁盘后。操作非常简单。


echo $Key->getValue();

Lists (无索引序列)

Lists 是指无序序列。


// Names
$list = <a href="">array</a>(

Adding new elements to a Redis lists happens in realtime and at constant speed. Meaning that adding an item to a 10 elements list, happens at the same speed as adding an element to the head of a 10 million elements list.


What’s the downside? Looking up an list item by index is less fast.

So use Redis lists every time you require to access data in the same order they are added. Also see Redis Data Types

Rediska Example

// Init
require_once 'Rediska/Key/List.php';
$List = new Rediska_List('names');

// Set
$List[] = 'john'; // Also works

// Get (this could be done at any time, by any process, just initialize the List again)
foreach ($List as $name {
    <a href="">echo</a> $name;

Sets (有索引无序序列)

Are collections of unique unsorted elements. You can think at this as a hash table
where all the keys are set to the ‘true’ value.

In PHP terms, a Set could be thought of as:

// Names
$set = <a href="">array</a>(
    'kevin' => true,
    'john' => true,

Because you now add items as keys, they will be unique, and you can perform all kinds of operations on them you can’t on lists.

Examples are:

  • Testing if a given element already exists
  • performing the intersection
  • union
  • difference between multiple sets and so forth.
  • etc

Ok time for that Rediska Example.

// Init
require_once 'Rediska/Key/Set.php';
$Set = new Rediska_Set('names');

// Set
$Set[] = 'john'; // Also works

// Get
foreach ($Set as $name) {
    <a href="">echo</a> $name;

Sorted Sets (有索引有序序列)

Are always ordered by their ‘score’ in memory. So any time you retrieve such a set, it’s already sorted no matter what you have added.

In PHP terms, a Sorted Set could be thought of as:

// Names with birthyears
$zset = <a href="">array</a>(
    'john' => 1979,
    'kevin' => 1983,

If we start adding more names & birthyears, old people will automatically be stored on top. Young at the bottom.

Rediska Example

// Init
require_once 'Rediska/Key/SortedSet.php';
$ZSet = new Rediska_Key_SortedSet('birthyears');

// Set
$ZSet['kevin'] = 1983;
$ZSet->add('john', 1979); // Also works

// Get
foreach ($ZSet as $name) {
    <a href="">echo</a> $name;
    <a href="">echo</a> $ZSet->getScore($name);


Before starting to use this in production, you want to know how you can keep your data safe.

Well, just copy the DB file to a safe place. On ubuntu the file is in /var/lib/redis/.

cp, rsync or scp will all do the trick. Redis only does active writing in a temp file so you don’t have to worry about data corruption. Also see the Redis FAQ.

( ! ) Warning: Missing argument 1 for cwppos_show_review(), called in /data/www/enjoyphp/wp-content/themes/flat/content-single.php on line 29 and defined in /data/www/enjoyphp/wp-content/plugins/wp-product-review/includes/legacy.php on line 18
Call Stack
10.0000355192{main}( ).../index.php:0
20.0001355480require( '/data/www/enjoyphp/wp-blog-header.php' ).../index.php:17
30.165610170080require_once( '/data/www/enjoyphp/wp-includes/template-loader.php' ).../wp-blog-header.php:19
40.166810241224include( '/data/www/enjoyphp/wp-content/themes/flat/single.php' ).../template-loader.php:74
50.245510805504get_template_part( ).../single.php:5
60.245510806024locate_template( ).../general-template.php:167
70.245510806120load_template( ).../template.php:643
80.245610806504require( '/data/www/enjoyphp/wp-content/themes/flat/content-single.php' ).../template.php:686
90.253210791600cwppos_show_review( ).../content-single.php:29

1 评论


电子邮件地址不会被公开。 必填项已用*标注