redis

  • 代码地址
  • 参考文档

  • 安装Bundle

     composer require snc/redis-bundle 2.1.3
     composer require predis/predis ^1.0
    
  • 在app/AppKernel.php注册Bundle

     public function registerBundles()
     {
         $bundles = [
             ...
             new Snc\RedisBundle\SncRedisBundle(),
         ];
     }
    
  • 在app/config/config.yml

     snc_redis:
         clients:
             default:
                 type: predis
                 alias: default
                 dsn: 'redis://%redis_host1%:%redis_port1%'
                 options:
                     prefix: '%redis_prefix1%:'
             other:
                 type: predis
                 alias: other
                 dsn: 'redis://%redis_host2%:%redis_port2%'
                 options:
                     prefix: '%redis_prefix2%:'
    
  • 服务调用

     # 控制器调用服务
     $redis = $this->get('snc_redis.default');
     # 存
         # 永久有效
         $redis->set('key','value');
         # 改为1小时有效
         $redis->expire('key', 3600);
         # 存并设置1小时有效(EX为单位:秒)
         $redis->set('key','value', 'EX', 3600);
    
     # 取
         $value = $redis->get('key');
    
     # hash存
         # 永久有效
         $redis->hset('main_key', 'key','value');
         # 改为1小时有效
         $redis->expire('main_key', 3600);
    
     # hash取
         $value = $redis->hget('main_key', 'key');
    
     # 集合存
         $redis->lpush('key', 'value');
    
     # 集合取(0首个元素, -1最终元素, 范围自由指定)
         $value = $redis->lrange('key', 0, -1);
    
     # 删
         $redis->del('key');
    
     # 键匹配
         $keys = $redis->keys('ke*');
         print_r($keys);
    

results matching ""

    No results matching ""