Cache
webman/cache is a caching component developed based on symfony/cache, compatible with both coroutine and non-coroutine environments, and supports connection pools.
Note
This manual is for webman v2 version. If you are using webman v1 version, please refer to the v1 version manual.
Installation
composer require -W webman/cache
Example
<?php
namespace app\controller;
use support\Request;
use support\Cache;
class UserController
{
public function db(Request $request)
{
$key = 'test_key';
Cache::set($key, rand());
return response(Cache::get($key));
}
}
Configuration File Location
The configuration file is located at config/cache.php
. If it does not exist, please create it manually.
Configuration File Content
<?php
return [
'default' => 'file',
'stores' => [
'file' => [
'driver' => 'file',
'path' => runtime_path('cache')
],
'redis' => [
'driver' => 'redis',
'connection' => 'default'
],
'array' => [
'driver' => 'array'
],
'apcu' => [
'driver' => 'apcu'
]
]
];
stores.driver
supports 4 types of drivers: file, redis, array, apcu.
file Driver
This is the default driver that does not depend on other components, supports inter-process sharing of cached data, but does not support multi-server sharing of cached data.
array Driver
In-memory storage, best performance, but will consume memory. It does not support inter-process and inter-server data sharing and will become invalid after process restart. Generally used for projects with small amounts of cached data.
apcu Driver
In-memory storage, performance second only to array. It supports inter-process sharing of cached data but does not support multi-server sharing of cached data and will become invalid after process restart. Typically used for projects with small amounts of cached data.
The APCu extension needs to be installed and enabled; it is not recommended for scenarios with frequent cache writes/deletes as it can lead to noticeable performance degradation.
redis Driver
Depends on the webman/redis component and supports inter-process and inter-server sharing of cached data.
stores.redis.connection
stores.redis.connection
corresponds to the key in config/redis.php
. When using redis, it will reuse the configuration from webman/redis
, including the connection pool configuration.
It is recommended to add a separate configuration in config/redis.php
, such as cache, as follows:
<?php
return [
'default' => [
'password' => 'abc123',
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
],
'cache' => [ // <==== Added
'password' => 'abc123',
'host' => '127.0.0.1',
'port' => 6379,
'database' => 1,
'prefix' => 'webman_cache-',
]
];
Then set stores.redis.connection
to cache
, the final configuration in config/cache.php
should be as follows:
<?php
return [
'default' => 'redis', // <====
'stores' => [
'file' => [
'driver' => 'file',
'path' => runtime_path('cache')
],
'redis' => [
'driver' => 'redis',
'connection' => 'cache' // <====
],
'array' => [
'driver' => 'array'
]
]
];
Switching Storage
You can manually switch the store using code like the following, to use different storage drivers:
Cache::store('redis')->set('key', 'value');
Cache::store('array')->set('key', 'value');
Tip
Key names are restricted by PSR6 and may not contain any of the characters{}
()/\@:, but this restriction can currently be bypassed by temporarily setting the PHP ini configuration
zend.assertions=-1(up to
symfony/cache` 7.2.4).
Using Other Cache Components
Refer to ThinkCache for usage related to other databases.