Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5003994
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类: 数据库开发技术

2011-03-26 17:29:57

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:
Rediska:
Rediska Document: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,增加下面代码:
   
  1. autoloaderNamespaces[] = “Rediska”
  2. pluginpaths.Rediska_Zend_Application_Resource = “Rediska/Zend/Application/Resource”
  3. resources.rediska.namespace = “Application_”
  4. resources.rediska.servers.0.host = ‘127.0.0.1′
  5. resources.rediska.servers.0.port = 6379

使用
现在我们开始尝试存储一些数据。下面分别演示四种 Redis 数据类型的使用方法:Keys, Lists, Sets和 Sorted Sets.
Keys (单值)
在PHP中,Key就是下面的类型:
  1. $firstname = 'kevin';

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

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

然后给它赋值:
 
  1. $Key->setValue('kevin');


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

  1. echo $Key->getValue();

Lists (无索引序列)
Lists 是指无序序列。
在PHP中,Lists是指下面的形式:

  1. // Names
  2.  $list = array(
  3.         'kevin',
  4.         'john',
  5.  );

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

  1. // Init
  2. require_once 'Rediska/Key/List.php';
  3. $List = new Rediska_List('names');
  4.   
  5. // Set
  6. $List->append('kevin');
  7. $List[] = 'john'; // Also works
  8.   
  9. // Get (this could be done at any time, by any process, just initialize the List again)
  10. foreach ($List as $name {
  11.     echo $name;
  12. }

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:

     
  1.     // Names
  2.     $set = array(
  3.         'kevin' => true,
  4.         'john' => true,
  5.     );

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.

  1. // Init
  2. require_once 'Rediska/Key/Set.php';
  3. $Set = new Rediska_Set('names');
  4.    
  5. // Set
  6. $Set->add('kevin');
  7. $Set[] = 'john'; // Also works
  8.   
  9. // Get
  10. foreach ($Set as $name) {
  11.     echo $name;
  12. }

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:

     
  1.     // Names with birthyears
  2.     $zset = array(
  3.         'john' => 1979,
  4.         'kevin' => 1983,
  5.     );

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

  1. // Init
  2. require_once 'Rediska/Key/SortedSet.php';
  3. $ZSet = new Rediska_Key_SortedSet('birthyears');
  4.   
  5. // Set
  6. $ZSet['kevin'] = 1983;
  7. $ZSet->add('john', 1979); // Also works
  8.   
  9. // Get
  10. foreach ($ZSet as $name) {
  11.    echo $name;
  12.    echo $ZSet->getScore($name);
  13. }
阅读(2112) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~