zf2 redis 练手2

来源:互联网 发布:中国现状 知乎 编辑:程序博客网 时间:2024/05/18 04:16
  1. 接着上文,不可能每次都都去实例化redis,这很麻烦,而且redis的配置没有分离,这样耦合度很高,不利于后续的代码修改,所以有修改了。

  2. 首先还是要启动redis-server,

  3. 在global.php


  4. ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
      return array(
         'redis_config'=>array(
            'Server'   =>'127.0.0.1:6379',
            'Database'    =>'0',
            'Namespace'=>'session',
            'Writable' =>true,
            'Readable' =>true,
            ),
     
        'service_manager' => array(
                'factories' => array(
                    'Zend\Db\Adapter\Adapter\Redis' => function ($sm) {
                       $config $sm->get('Configuration');
                       if(isset($config['redis_config'])){
                           //redis option
                       $config_redis $config['redis_config'];
                       $redisOption new \Zend\Cache\Storage\Adapter\RedisOptions();
                       $redisOption->setServer($config_redis['Server'])
                           ->setDatabase($config_redis['Database'])
                           ->setNamespace($config_redis['Namespace'])
                           ->setWritable($config_redis['Writable'])
                           ->setReadable($config_redis['Readable']);
                           return new \Zend\Cache\Storage\Adapter\Redis($redisOption)
                       }
                    },
                ),
            ),
    );

    之后在调用的地方

  5. ?
    1
    2
    3
    4
    5
    6
    7
     public function redisAction()
        {
            //获取redis
            $redis $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter\Redis');
            $redis->setItem('zzc''zhangzhican110@gmail.com');
            die;
        }

0 0
原创粉丝点击