PHP code example of codecible / yii2-phpredis

1. Go to this page and download the library: Download codecible/yii2-phpredis library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

codecible / yii2-phpredis example snippets

 php
vendor\codecible\yii2-phpredis\Connection.php

 php
'codecible/yii2-phpredis' =>
    array (
        'name' => 'codecible/yii2-phpredis',
        'version' => '1.0.0.0',
        'alias' =>
        array (
            '@codecible/phpredis' => $vendorDir . '/codecible/yii2-phpredis',
        ),
    ),

 php

return [
    'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
    'charset' => 'utf-8',
    'language' => 'zh-CN',
    'timeZone' => 'PRC',
    'components' => [
        'cache' => [
            //'class' => 'codecible\caching\FileCache',
            'class' => 'codecible\phpredis\Cache',
        ],
        'redis' => [
            'class' => 'codecible\phpredis\Connection',
            'hostname' => 'localhost',
            'port' => 6379,
            'database' => 0,
        ],
        'session' => [
            'class' => 'codecible\phpredis\Session',
            // 'class' => 'codecible\web\DbSession',
            // 'db' => 'mydb',  // 数据库连接的应用组件ID,默认为'db'.
            // 'sessionTable' => 'my_session', // session 数据表名,默认为'session'.
        ],
	'queue' => [
              'class' => 'codecible\phpredis\Queue',
              'keyPrefix' => 'saas.queue:',
              'maxRunTimelong' => 299,//进程最大运行时间,单位秒
              'phpbin' => 'php',
              'consoleStartAction' => 'queue/start',//把此命令加到crontab里
              'consoleListenAction' => 'queue/listen',
              'channels' => [
                  'mq/test-func' => 2,//启动的最大进程数,最少2个
              ],
          ],
        
 ....

 
 php
	$key = 'aaaaa';
        $value = 'aaaa1111';
        if (Yii::$app->cache->exists($key))
        {
            var_dump('get');
            var_dump(Yii::$app->cache->get($key));
            
        }
        else
        {
            var_dump('set');
            Yii::$app->cache->set($key, $value, 3000);
            
        }
        
        $key = 'ccc';
        $value = 'ccccc111';
        
        if (Yii::$app->session->get($key))
        {
            var_dump('get');
            var_dump(Yii::$app->session->get($key));
            
        }
        else
        {
            var_dump('set');
            Yii::$app->session->set($key, $value);
            
        }

 php

//队列示例

把 QueueController.php放到console\controller文件夹内

//添加到计划任务
php ./yii queue/start

//添加业务到队列
Yii::$app->queue->publish('queue/test-func', ['starttime' => $_logs['starttime1']]);


 php
2018-03-05 17:28:51 [-][-][-][error][yii\db\Exception] exception 'yii\db\Exception' with message 'Failed to read from socket.
Redis command was: GET b4e9e33d1c3bc2b09b4b0cecd175e8d8' in /code/vendor/yiisoft/yii2-redis/Connection.php:663
Stack trace:
#0 /www/code/vendor/yiisoft/yii2-redis/Connection.php(652): yii\redis\Connection->parseResponse('GET b4e9e33d1c3...')
#1 /www/code/vendor/yiisoft/yii2-redis/Cache.php(102): yii\redis\Connection->executeCommand('GET', Array)
#2 /www/code/vendor/yiisoft/yii2/caching/Cache.php(114): yii\redis\Cache->getValue('b4e9e33d1c3bc2b...')
#3 /www/code/vendor/yiisoft/yii2/db/Schema.php(137): yii\caching\Cache->get(Array)
#4 /www/code/vendor/yiisoft/yii2/db/QueryBuilder.php(247): yii\db\Schema->getTableSchema('task_item')