前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于PhalApi的Redis拓展

基于PhalApi的Redis拓展

作者头像
喵了个咪233
发布2018-03-01 17:26:49
7510
发布2018-03-01 17:26:49
举报

#基于PhalApi的Redis拓展

##前言## 先在这里感谢phalapi框架创始人@dogstar,为我们提供了这样一个优秀的开源框架.

编写本次拓展出于的目的是为了解决并不是非常熟悉redis的童鞋能够方便的使用redis进行实际的运用 ,对原生的phpredis进行的封装优化良好的注释和例子希望能提供更好的帮助!

注:本拓展并没有开发完成,也没进行严格的测试,此版本为还处于开发阶段的鉴赏版.

附上:

官网地址:http://www.phalapi.net/

开源中国Git地址:http://git.oschina.net/dogstar/PhalApi/tree/release

##安装配置redis以及phpredis##

基于centos6.5

代码语言:javascript
复制
    //下redis解压安装
    wget http://download.redis.io/releases/redis-2.8.17.tar.gz
    tar zxvf redis-2.8.17.tar.gz
    cd redis-2.8.17
    make
    make test
    make install
    //生成6379端口以及配置文件
    cd utils
    ./install_server.sh
    Please select the redis port for this instance: [6379]
    Please select the redis config file name [/etc/redis/6379.conf]
    Please select the redis log file name [/var/log/redis_6379.log]
    Please select the data directory for this instance [/var/lib/redis/6379]
    Please select the redis executable path [/usr/local/bin/redis-server]
    //对配置文件进行配置
    vi /etc/redis/6379.conf
    databases 100                            #可以使用的库的数量修改16为100
    masterauth xxxxxxxxxxxxx                 #连接 master 的认证密码
    requirepass woyouwaimai76                #连接此redis的连接密码
    :wq
    //修改关闭redis需要密码
    vi /etc/rc.d/init.d/redis_6379
    $CLIEXEC -p $REDISPORT -a woyouwaimai76 shutdown    #stop redis需要密码
    //重启redis
    service redis_6379 restart
    //添加到系统启动项
    chkconfig redis_6379 on

     //下载phpredis解压安装
     wget https://github.com/nicolasff/phpredis/archive/master.zip
     unzip master.zip -d phpredis
     cd phpredis/phpredis-master
     phpize
     ./configure
     make && make install
     //在php.ini中注册phpredis
     extension = redis.so

     //测试
      <?php
         $auth     = 'xxxxxxxxx';
         $source   = '127.0.0.1';
         $host     = '6379';
         $redis    = new Redis();
         echo $redis->connect($host) ? "$host connect" : "$host fail";
         if($auth){
             echo $redis->auth($auth) ? " auth success" : " auth fail";
         }

##注册配置文件在Config.app文件下面## return array( //Redis配置项 'redis' => array( //Redis缓存配置项 'servers' => array( 'host' => '127.0.0.1', //Redis服务器地址 'port' => '6379', //Redis端口号 'prefix' => 'developers_', //Redis-key前缀 'auth' => 'woyouwaimai76', //Redis链接密码 ), // Redis分库对应关系 'DB' => array( 'developers' => 1, 'user' => 2, 'code' => 3, ), //使用阻塞式读取队列时的等待时间单位/秒 'blocking' => 5, ),

代码语言:javascript
复制
);

##在init入口文件注册redis拓展##

代码语言:javascript
复制
//redis链接
DI()->redis = new Redis_Lite(DI()->config->get('app.redis.servers'));

##开始使用##

代码语言:javascript
复制
//存入永久的键值队
DI()->redis->set_forever(键名,值,库名);
//获取永久的键值队
DI()->redis->get_forever(键名, 库名);

//存入一个有时效性的键值队,默认600秒
DI()->redis->set_Time(键名,值, 库名,有效时间);
//获取一个有时效性的键值队
DI()->redis->get_Time(键名, 库名);

//写入队列左边
DI()->redis->set_Lpush(队列键名,值, 库名);
//读取队列右边
DI()->redis->get_lpop(队列键名, 库名);
//读取队列右边 如果没有读取到阻塞一定时间(阻塞时间或读取配置文件blocking的值)
DI()->redis->get_Brpop(队列键名,值, 库名);

//删除一个键值队适用于所有
DI()->redis->del(键名, 库名);
//自动增长
DI()->redis->get_incr(键名, 库名);
//切换DB并且获得操作实例
DI()->redis->get_redis(键名, 库名);

更多使用方法请参考Redis_Lite文件

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档