klsoft / yii2-cache-redis
The package provides the Yii 2 cache using the Redis PHP extension
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/klsoft/yii2-cache-redis
Requires
- php: >=7.4
- ext-redis: ^6.0.0
- yiisoft/yii2: ~2.0.45
README
The package provides the Yii 2 cache using the Redis PHP extension.
Requirement
- PHP 7.4 or higher.
- Redis PHP extension 6.0.0 or higher.
Installation
composer require klsoft/yii2-cache-redis
How to use
1. Configure the Cache.
Example:
use Yii; use yii\web\CacheSession; use Klsoft\Yii2CacheRedis\RedisCache; return [ // ... 'components' => [ // ... 'session' => [ 'class' => CacheSession::class, 'cache' => new RedisCache( new Redis([ 'host' => $params['redisHost'], 'port' => $params['redisPort'], 'database' => $params['redisDatabaseSession'] ])) ], 'cache' => new RedisCache( new Redis([ 'host' => $params['redisHost'], 'port' => $params['redisPort'], 'database' => $params['redisDatabaseCache'] ])) ], ];
2. Cache the data.
Example:
namespace MyNamespace; use Yii; final class ProductRepository implements ProductRepositoryInterface { private int $cacheDuration; private const TOP_PRODUCTS = 'top_poroducts'; public function __construct(int $cacheDuration) { $this->cacheDuration = $cacheDuration; } public function getTopProducts(int $numberOfTopProducts): array { return Yii::$app->cache->getOrSet( ProductRepository::TOP_PRODUCTS, function () use ($numberOfTopProducts) { return $this->getTopProductsFromDb($numberOfTopProducts); }, $this->cacheDuration); } }