首页 > 系统 > Linux > 正文

Linux下Redis+crontab实现任务队列

2020-03-22 19:05:05
字体:
来源:转载
供稿:网友
  • 前段时间由于一个控制方法要实现的逻辑任务太多了,无论怎么优化都还是有瓶颈。网上介绍可以使用任务队列的机制,把一些不是立即需要相应的逻辑处理放在队列中,让某个程序时时去执行。举个例子:用户上来我的网站注册,注册完后,我需要给用户的邮箱帐号推送一些邮件,这个推送邮件所发的时间可能远比注册保存用户资料所花的时间多些,也不是立即就需要响应到前端给客户知道。所以,是可以把推送邮件这一动作作为一个任务添加到队列中。

    说明下我测试调试的环境是在Ubuntu12.04下的,安装redis和添加crontab命令是通过终端命令的,我测试的项目是基于yii框架,调用redis是通过框架提供的yiiredis插件,可能不太符合你的环境,但其中的思路或许还是可以借鉴的。

    1、安装Redis
    进入终端命令窗口(快捷键Ctrl+Alt+T)切换至/usr/local/src(命令:cd /usr/local/src)下,下载并安装redis:

    $ wget http://redis.googlecode.com/files/redis-2.6.12.tar.gz$ tar xzf redis-2.6.12.tar.gz$ cd redis-2.6.12$ make
    进入redis-2.6.12(命令:cd redis-2.6.12)目录,修改redis.conf:
    daemonize yes 
    启动服务端:
    $ src/redis-server redis.conf
    进入命令行验证服务是否启动:
    $ src/redis-cliredis> set foo barOKredis> get foo"bar"

    2、安装Yii的Redis插件
    目前主要有两种Yii插件:
    1> Rediscache:基于predis(Redis的纯PHP实现客户端),无需安装Redis for PHP扩展。
    2> YiiRedis:基于phpredis客户端,需要安装Redis for PHP扩展。
    这里采用Rediscache插件,避免线上安装Redis for PHP扩展。
    从以下地址下载Rediscache插件:
    http://www.yiiframework.com/extension/rediscache/files/redis.zip
    将插件解压到helloyii/app/protected/extensions中:
    插件文件部署后的位置应为:helloyii/app/protected/extensions/redis/CredisCache.php
    配置Rediscache
    return array( 'components' => array(   …   'cache'=>array(    'html' target='_blank'>class'=>'ext.redis.CRedisCache', //对应protected/extensions/redis/CredisCache.php    'servers'=>array(     array(      'host'=>'127.0.0.1',      'port'=>6379,     ),    ),   ),  ),  … );

    3、测试redis使用

    编写一个读写缓存的控制器IndexController进行测试。

    class IndexController extends CController{  public function actionSetRedisValue() {        $key = $_POST['key'];        $value = $_POST['value'];        if ( !empty( $key ) && !empty( $value ) ) {            try {                $redis = Yii::app()->cache;                $data = ( array ) $redis->get( 'test' );                $data[$key] = $value;                $redis->set( 'test', $data );                die( json_encode( array( 'status' => 1, 'msg' => 'set ok!' ) ) );            } catch ( Exception $e ) {                die( json_encode( array( 'status' => 0, 'msg' => 'set faile!' ) ) );            }        } else {            die( json_encode( array( 'status' => 0, 'msg' => 'must input!' ) ) );        }   }   public function actionGetRedisValue() {        try {            $redis = Yii::app()->cache;            $data = ( array ) $redis->get( 'test' );            $log_file = Yii::app()->runtimePath . 'edis_log.txt';            if ( file_exists( $log_file ) ) {                $handle = fopen( $log_file, "a+" );                $log = "----" . date( 'Y-m-d H:i:s' ) . "-----" . "";                foreach ( $data as $key => $value ) {                    $log .= '->' . $key . ' = ' . $value . "";                }                fwrite( $handle, $log );                fclose( $handle );            }        } catch ( Exception $e ) {            echo $e->getMessage();        }   }}

    视图文件index内容。
    <?php Yii::app()->clientScript->registerCoreScript( 'jquery' ); ?><p>Redis</p><form id="redis_form" action="<?php echo $this->createUrl( '/cata/index/setRedisValue' ); ?>" method="post">    Key:<input id="key" type="text" value=""/>    Value:<input id="val" type="text" value=""/>    <input id="set" type="button" value="set"/>    <input id="get" type="button" value="get"/>    <span></span></form><script>    $(document).ready(function() {        $('#redis_form #set').on('click', function() {            var redisForm = $('#redis_form');            var action = redisForm.attr('action');            var key = redisForm.children('#key');            var value = redisForm.children('#val');            $.post(action, {key: key.val(), value: value.val()}, function(data) {                redisForm.find('span').html(data.msg);                setTimeout(function() {                    redisForm.find('span').html('');                }, 3000);            }, 'json');        });        $('#redis_form #get').on('click', function() {            $.post('<?php echo $this->createUrl( '/cata/index/getRedisValue' ); ?>', {}, function(data) {            }, 'json');        });    });</script>        

    郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

  • 发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表