redis

  1. 作者QQ:67065435 QQ群:821635552

  2. 代码地址

  3. 参考文档

  4. 安装Bundle

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

    public function registerBundles()
    {
        $bundles = [
            ...
            new Snc\RedisBundle\SncRedisBundle(),
        ];
    }
    
  6. 在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%:'
    
  7. 服务调用

    # 控制器调用服务
    $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);
    
Copyright © 豆包嘿嘿~ 2012-∞ 冀ICP备17033181号 all right reserved,powered by Gitbook修订: 2012-12-31 12:00:00

results matching ""

    No results matching ""