设为首页 收藏本站
查看: 1595|回复: 0

[经验分享] Redis与PHP的安装与使用Redis与PHP的安装与使用

[复制链接]

尚未签到

发表于 2018-11-7 13:37:14 | 显示全部楼层 |阅读模式
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就是下面的类型:
  view source
  print?
  1    $firstname = 'kevin';
  要存储这个值,首先初始化一个Key’,命名为firstname’ ;
  view source
  print?
  1    require_once 'Rediska/Key.php';
  2    $Key = new Rediska_Key('firstname');
  然后给它赋值:
  view source
  print?
  1    $Key->setValue('kevin');
  只需要使用setValue()就可以将 ‘kevin’立即存储到Redis的内存中,随后可能会存储到磁盘后。操作非常简单。
  然后就可以从Redis中读取这个值:
  view source
  print?
  1    echo $Key->getValue();
  Lists (无索引序列)
  Lists 是指无序序列。
  在PHP中,Lists是指下面的形式:
  view source
  print?
  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
  view source
  print?
  01    // Init
  02    require_once 'Rediska/Key/List.php';
  03    $List = new Rediska_List('names');
  04
  05    // Set
  06    $List->append('kevin');
  07    $List[] = 'john'; // Also works
  08
  09    // 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:
  view source
  print?
  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.
  view source
  print?
  01    // Init
  02    require_once 'Rediska/Key/Set.php';
  03    $Set = new Rediska_Set('names');
  04
  05    // Set
  06    $Set->add('kevin');
  07    $Set[] = 'john'; // Also works
  08
  09    // 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:
  view source
  print?
  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
  view source
  print?
  01    // Init
  02    require_once 'Rediska/Key/SortedSet.php';
  03    $ZSet = new Rediska_Key_SortedSet('birthyears');
  04
  05    // Set
  06    $ZSet['kevin'] = 1983;
  07    $ZSet->add('john', 1979); // Also works
  08
  09    // Get
  10    foreach ($ZSet as $name) {
  11        echo $name;
  12        echo $ZSet->getScore($name);
  13    }
  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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-632009-1-1.html 上篇帖子: redis的简单使用和介绍 下篇帖子: 初试redis,比memcached快10倍速
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表