Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

SessionAdapter

$session = new PhalconSessionAdapterRedis([
    'prefix' => '',
    'lifetime' => 86400,
    'host' => '127.0.0.1',
    'port' => 6379,
    'persistent' => false
]);

ModelMetaDataAdapter

$redisMetaData = new PhalconMvcModelMetaDataRedis([
    'prefix' => '',
    'lifetime' => 86400,
    'host' => '127.0.0.1',
    'port' => 6379,
    'persistent' => false
]);

Cache

$cache = new PhalconCacheBackendRedis([
    'prefix' => '',
    'lifetime' => 86400,
    'host' => '127.0.0.1',
    'port' => 6379,
    'persistent' => false
]);

如果这些都是用的同一台Redis,那一个请求不就会创建3个连接出来
可以设置共享同一个连接吗?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
506 views
Welcome To Ask or Share your Answers For Others

1 Answer

你可以用di注入,注入的时候用setShared
简单的例子

$di = new PhalconDiFactoryDefault();
$di->setShared('redis', function () {
    $redis = new PhalconCacheBackendRedis([
        'prefix' => '',
        'lifetime' => 86400,
        'host' => '127.0.0.1',
        'port' => 6379,
        'persistent' => false
    ]);
    return $redis;
});

用的时候直接从Di里面取就好

$di = PhalconDi::getDefault();
$redis = $di['redis'];    // 这个redis实例就是注入的实例,多次调用也是共享同一个连接

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...