PHP code example of xuepengdong / phalapiredis

1. Go to this page and download the library: Download xuepengdong/phalapiredis 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/ */

    

xuepengdong / phalapiredis example snippets


  以下为例子:
  
  $getRedis =  \PhalApi\DI()->redis->getRedis();
  $getRedis->select(1);
  return $getRedis->set('key20220806', 'value20220806', 20000);
  

    \PhalApi\DI()->redis->set_forever(键名, 值, 库名);
    

    \PhalApi\DI()->redis->get_forever(键名, 库名);
    

    \PhalApi\DI()->redis->get_getSet(键名, 值, 库名);
    

// 惰性加载Redis
$di->redis = function () {
    return new \Xuepengdong\Phalapiredis\Lite(\PhalApi\DI()->config->get("app.redis.servers"));
};

    /**
     * 扩展类库 - Redis扩展
     */
    'redis' => array(
        //Redis链接配置项
        'servers'  => array(
            'host'   => '127.0.0.1',        //Redis服务器地址
            'port'   => '6379',             //Redis端口号
            'prefix' => 'PhalApi_',         //Redis-key前缀
            'auth'   => 'phalapi',          //Redis链接密码
        ),
        // Redis分库对应关系操作时直接使用名称无需使用数字来切换Redis库
        'DB'       => array(
            'developers' => 1,
            'user'       => 2,
            'code'       => 3,
        ),
        //使用阻塞式读取队列时的等待时间单位/秒
        'blocking' => 5,
    ),


    \PhalApi\DI()->redis->set_list(数组, 库名);
    例子:\PhalApi\DI()->redis->set_list(array('key0' => 'value0', 'key1' => 'value1'), 'users');
    

    \PhalApi\DI()->redis->get_list(数组, 库名);
    例子:\PhalApi\DI()->redis->get_list(array('key0' , 'key1'), 'users');
    

    \PhalApi\DI()->redis->get_time_ttl(键名, 库名);
    

    \PhalApi\DI()->redis->get_lRange(队列键名, $start, $end);
    

    \PhalApi\DI()->redis->get_lTrim(键名,$start, $end, 库名);
    

    \PhalApi\DI()->redis->HSET($key, $field, $value, $tablename);
    例子:\PhalApi\DI()->redis->HSET('hash1', 'field2', 'value2', 1);
    

    \PhalApi\DI()->redis->HDEL($key, $field, $tablename);
    

    \PhalApi\DI()->redis->HEXISTS($key, $field, $tablename);
    

    \PhalApi\DI()->redis->HMGET($key, $field, $tablename);
    备注:Redis Hmget 命令用于返回哈希表中,一个或多个给定字段的值。如果指定的字段不存在于哈希表,那么返回一个 nil 值。可以传数组
    

    \PhalApi\DI()->redis->HGET($key, $field, $tablename);
    备注:只传一个字段