Redis与PHP的安装与使用

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

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

本文介绍了Redis的安装,以及PHP使用Redis的安装过程,以帮助PHP开发者迈入Redis世界。

相关链接:

Redis:http://code.google.com/p/redis/
Rediska:http://rediska.geometria-lab.net/
Rediska Document:http://rediska.geometria-lab.net/documentation

安装Redis服务端程序

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

安装PHP客户端程序

有很多PHP客户端实现方式,这里我推荐Rediska。它功能完善而且使用方便。用下面的命令把最新版的Rediska下载到/usr/share/php/目录下:
cd /usr/src
[ -d Rediska ] || git clone git://github.com/Shumkov/Rediska.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.host = ‘127.0.0.1’
    resources.rediska.servers.0.port = 6379

 

使用

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

Keys (单值)

在PHP中,Key就是下面的类型:

$firstname = 'kevin';

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

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

然后给它赋值:

$Key->setValue('kevin');

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

然后就可以从Redis中读取这个值:

echo $Key->getValue();

Lists (无索引序列)

Lists 是指无序序列。

在PHP中,Lists是指下面的形式:

// Names
$list = <a href="http://www.php.net/array">array</a>(
    'kevin',
    'john',
);

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.

Excellent.

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->append('kevin');
$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="http://www.php.net/echo">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="http://www.php.net/array">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->add('kevin');
$Set[] = 'john'; // Also works

// Get
foreach ($Set as $name) {
    <a href="http://www.php.net/echo">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="http://www.php.net/array">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="http://www.php.net/echo">echo</a> $name;
    <a href="http://www.php.net/echo">echo</a> $ZSet->getScore($name);
}

Backup

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.

1 评论

发表评论

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