jobverplanke / craftcms-components
Simple driver setup for CraftCMS components queue, cache or session
Requires
- php: ^8.2
- craftcms/cms: ^4.0|^5.0
Requires (Dev)
- craftcms/phpstan: dev-main
- phpstan/phpstan-deprecation-rules: ^1.2
- phpstan/phpstan-strict-rules: ^1.6
- sentry/sentry: ^4.9
- yiisoft/yii2-redis: ^2.0.18
Suggests
- ext-redis: Required to use the Redis cache, queue and session drivers (^6.0).
- sentry/sentry: Required to sent exceptions to Sentry (^4.9).
- yiisoft/yii2-redis: Required to use Redis with Craft CMS (^2.0.18).
Conflicts
- craftcms/cms: <4.0
README
Simple driver setup for CraftCMS components queue, cache or session for CraftCMS v4 and v5.
Available components and drivers
Redis | Database | |
---|---|---|
Cache | ✅ | ✅ |
Queue | ✅ | ❌ (default driver) |
Session | ✅ | ✅ |
Note
Craft's default queue driver is the database. Do not configure the queue component in the app.php if you want to make use of the database queue driver.
Craft v4: https://craftcms.com/docs/4.x/config/app.html#queue
Craft v5: https://craftcms.com/docs/5.x/reference/config/app.html#queue
Installation
composer require jobverplanke/craftcms-components
Make sure that the yiisoft/yii2-redis
package is installed when utilizing Redis
composer require yiisoft/yii2-redis
Configuration
To make use of a different driver per component is a breeze. Just specify the desired driver for the component you want to use in the components
config array in either config/app.php
or config/app.web.php
(session only).
Database configuration
In order to use the database
driver, configure CraftCMS as followed
Cache component
// .env CRAFT_CACHE_DRIVER=database // config/app.php use craft\helpers\App; use Verplanke\CraftComponents\Components\Cache; return [ 'components' => [ 'cache' => fn () => Cache::driver(App::env('CRAFT_CACHE_DRIVER')), ], ];
Queue component
// .env CRAFT_QUEUE_DRIVER=database // config/app.php use craft\helpers\App; use Verplanke\CraftComponents\Components\Queue; return [ 'components' => [ 'queue' => Queue::driver(App::env('CRAFT_QUEUE_DRIVER')), ], ];
Session component
// .env CRAFT_SESSION_DRIVER=database // config/app.web.php use craft\helpers\App; use Verplanke\CraftComponents\Components\Session; return [ 'components' => [ 'session' => fn () => Session::driver(App::env('CRAFT_SESSION_DRIVER')), ], ];
Redis configuration
If you want to use the Redis
driver for a specific component, make sure Redis is configured
- Add the Redis component to the
app.php
, see example below - Enable Redis by adding the following environment variables
REDIS_URL=
ORREDIS_HOST
,REDIS_PORT
andREDIS_PASSWORD
Note
REDIS_URL
takes precedence over REDIS_HOST
, REDIS_PORT
and REDIS_PASSWORD
.
// config/app.php use Verplanke\CraftComponents\Components\Redis; return [ 'components' => [ 'redis' => Redis::connect(), ], ];
Cache component
// .env CRAFT_CACHE_DRIVER=redis // config/app.php use craft\helpers\App; use Verplanke\CraftComponents\Components\Cache; return [ 'components' => [ 'cache' => fn () => Cache::driver(App::env('CRAFT_CACHE_DRIVER')), ], ];
Queue component
// .env CRAFT_QUEUE_DRIVER=redis // config/app.php use craft\helpers\App; use Verplanke\CraftComponents\Components\Queue; return [ 'components' => [ 'queue' => Queue::driver(App::env('CRAFT_QUEUE_DRIVER')), ], ];
Session component
// .env CRAFT_SESSION_DRIVER=redis // config/app.web.php use craft\helpers\App; use Verplanke\CraftComponents\Components\Session; return [ 'components' => [ 'session' => fn () => Session::driver(App::env('CRAFT_SESSION_DRIVER')), ], ];
Environment Variable Configuration
The following environment variables are available for further configuration of the components
It's recommended to leave the following set to false
when using Heroku. Heroku issue and Github issue
REDIS_SSL
REDIS_SSL_VERIFY_PEER
REDIS_SSL_VERIFY_PEER_NAME
Required | Type | Default Value | Description / Remark | |
---|---|---|---|---|
CRAFT_CACHE_DRIVER |
✅ | string | Possible values redis or database |
|
CRAFT_SESSION_DRIVER |
✅ | string | Possible values redis or database |
|
CRAFT_QUEUE_DRIVER |
✅ | string | Possible values redis or database |
|
REDIS_URL |
✅ | string | Takes precedence over REDIS_HOST , REDIS_PORT and REDIS_PASSWORD . |
|
REDIS_HOST |
string | Only when REDIS_URL is not used |
||
REDIS_PORT |
string | 6379 | Only when REDIS_URL is not used |
|
REDIS_PASSWORD |
string | Only when REDIS_URL is not used |
||
REDIS_DB |
integer | 0 | Default database to use, this database will be used for all Redis connections | |
REDIS_RETRIES |
integer | 1 | Th..ount of times to retry connecting after connection has timed out | |
REDIS_SSL |
boolean | false | Use SSL connection when connecting with Redis. Recommended to use false when using Heroku |
|
REDIS_SSL_VERIFY_PEER |
boolean | false | Verify peer SSL certificate. Recommended to use false when using Heroku |
|
REDIS_SSL_VERIFY_PEER_NAME |
boolean | false | Verify peer name of SSL certificate. Recommended to use false when using Heroku |
|
REDIS_CACHE_DB |
integer | 1 | Which database to use for the cache database. Make sure the cache database is separated from the queue and session database | |
REDIS_QUEUE_DB |
integer | 3 | Which database to use for the queue database | |
REDIS_QUEUE_CHANNEL |
string | queue | Queue channel to use | |
REDIS_QUEUE_TTR |
integer | 300 | Max time for job execution, unit in seconds | |
REDIS_QUEUE_ATTEMPTS |
integer | 3 | Max number of attempts | |
REDIS_SESSION_DB |
integer | 1 | Which database to use for the session database. |