PHPSocialNetwork / phpfastcache

A high-performance backend cache system. It is intended for use in speeding up dynamic web applications by alleviating database load. Well implemented, it can drops the database load to almost nothing, yielding faster page load times for users, better resource utilization. It is simple yet powerful.
https://www.phpfastcache.com
MIT License
2.36k stars 452 forks source link

Support custom native RedisCluster client #906

Closed mocxi closed 8 months ago

mocxi commented 8 months ago

What type of feature would you like?

New backend support

The feature request

The current set function new RedisConfig())->setRedisClient($redisClient) only accept Redis object I'd like to request support RedisCluster so I can set custom native redis cluster client for it

Code sample (optional)

$obj_cluster = new RedisCluster(NULL, Array('host:7000', 'host:7001', 'host:7003'));
$cacheInstance = CacheManager::getInstance('Redis', (new RedisConfig())->setRedisClient($obj_cluster));

References (optional)

https://github.com/phpredis/phpredis/blob/develop/cluster.md

Do you have anything more you want to share? (optional)

I'm using phpfastcache v7

github-actions[bot] commented 8 months ago

Hello curious contributor ! Since it seems to be your first contribution, make sure that you've been:

Geolim4 commented 8 months ago

Hello, could be implemented as of 9.2 version as a new feature indeed.

Geolim4 commented 8 months ago

I'm using phpfastcache v7

Just FYI @mocxi, v7 is no longer supported, upgrade at least to v8 for security fixes support or v9 for extended support:

https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV4%CB%96%5D-Global-support-timeline

Geolim4 commented 8 months ago

RedisCluster Class have some different signatures than Redis Class.

So I think that I will may create a new driver that extends the Redis one.

Geolim4 commented 8 months ago

Can you test the 9.2 dev branch v9.2.x-dev please and telle me if you make it working ?

Geolim4 commented 8 months ago

Syntax:

use Phpfastcache\Drivers\RedisCluster\Config as RedisConfig;
use Phpfastcache\CacheManager;

    $config =  new RedisConfig();
    $config->setClusters( '127.0.0.1:7001', '127.0.0.1:7002', '127.0.0.1:7003', '127.0.0.1:7004', '127.0.0.1:7005', '127.0.0.1:7006');
    $config->setOptPrefix('pfc_');; // optional 
    $config->setSlaveFailover(\RedisCluster::FAILOVER_ERROR); // optional 
    $cacheInstance = CacheManager::getInstance('RedisCluster', $config);
mocxi commented 8 months ago

It worked as intended. Thanks for your help!

Geolim4 commented 8 months ago

Everything looks good for you ? You managed to configure it easily @mocxi ?

Btw 9.2 should be shipped by January.

mocxi commented 8 months ago

Yes, I pulled the dev branch, set up a Redis cluster, connected to it and everything is good. The thing now is I need to upgrade my code base to PHP 8 in order to use the latest 9.x phpfastcache