discord-php / DiscordPHP

An API to interact with the popular messaging app Discord
MIT License
985 stars 236 forks source link

Add CacheConfig, replacing cacheInterface, cacheSweep and cacheCompress #1003

Closed SQKo closed 1 year ago

SQKo commented 1 year ago

This also takes benefit of object reference to be used in multiple repository for same configuration. And in future library can add more options easily.

This also solve problem with PSR-6 implementation that cant be detected as redis/memcached to set the prefix separator, so user can manually specify it.

i.e. before

$cache = new Redis($redis, 'dphp:');
'cacheInterface' => $cache,
'cacheSweep' = > true,
'cacheCompres' => true,

after:

$cache = new Redis($redis, 'dphp:');
'cache' => new CacheConfig($cache, true, true, ':');

or the cool PHP 8 way

'cache' => new CacheConfig(interface: $cache, compress: true, sweep: true, separator: ':');

Of course specifying repository still work like before:

$cache = new Redis($redis, 'dphp:');
$redisCacheConfig = new CacheConfig($cache, $separator = ':');
'cache' => [
    AbstractRepository::class => new CacheConfig(new ArrayCache()), // Default use ArrayCache
    MessageRepository::class => $redisCacheConfig, // Message use Redis
    ReactionRepository::class => $redisCacheConfig, // Reactions too
]

The $discord->cache getter is removed though since at first time it is irrelevant and there can be different cache interfaces.

I need opinion about the CacheConfig properties visibility though, either make it public to allow set/get or just allow getter/read only.