-
接着上文,不可能每次都都去实例化redis,这很麻烦,而且redis的配置没有分离,这样耦合度很高,不利于后续的代码修改,所以有修改了。
-
首先还是要启动redis-server,
-
在global.php
-
12345678910111213141516171819202122232425262728
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
)
}
},
),
),
);
之后在调用的地方
-
1234567
public
function
redisAction()
{
//获取redis
$redis
=
$this
->getServiceLocator()->get(
'Zend\Db\Adapter\Adapter\Redis'
);
$redis
->setItem(
'zzc'
,
'zhangzhican110@gmail.com'
);
die
;
}
zf2 redis 练手2
最新推荐文章于 2025-08-13 11:11:26 发布