hyperf/docs/en/cache.md

75 lines
1.7 KiB
Markdown
Raw Normal View History

2019-03-08 12:04:46 +08:00
# Cache
[https://github.com/hyperf/cache](https://github.com/hyperf/cache)
2019-03-08 12:04:46 +08:00
## Config
2019-03-19 14:52:21 +08:00
```php
2019-03-08 12:04:46 +08:00
<?php
return [
'default' => [
'driver' => Hyperf\Cache\Driver\RedisDriver::class,
'packer' => Hyperf\Cache\Packer\PhpSerializer::class,
],
];
2019-03-19 14:52:21 +08:00
```
2019-03-08 12:04:46 +08:00
## How to use
Components provide Cacheable annotation to configure cache prefix, expiration times, listeners, and cache groups.
For example, UserService provides a user method to query user information. When the Cacheable annotation is added, the Redis cache is automatically generated with the key value of `user:id` and the timeout time of 9000 seconds. When querying for the first time, it will be fetched from DB, and when querying later, it will be fetched from Cache.
2019-03-19 14:52:21 +08:00
```php
2019-03-08 12:04:46 +08:00
<?php
namespace App\Services;
use App\Models\User;
use Hyperf\Cache\Annotation\Cacheable;
class UserService
{
#[Cacheable(key: "user", ttl: 9000, listener: "user-update")]
2019-03-08 12:04:46 +08:00
public function user($id)
{
$user = User::query()->where('id',$id)->first();
if($user){
return $user->toArray();
}
return null;
}
}
2019-03-19 14:52:21 +08:00
```
2019-03-08 12:04:46 +08:00
## Clear Cache
Of course, if the data changes, how to delete the cache? Here we need to use the listener. Next, a new Service provides a way to help us deal with it.
2019-03-19 14:52:21 +08:00
```php
2019-03-08 12:04:46 +08:00
<?php
declare(strict_types=1);
namespace App\Service;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Cache\Listener\DeleteListenerEvent;
use Psr\EventDispatcher\EventDispatcherInterface;
class SystemService
{
#[Inject]
protected EventDispatcherInterface $dispatcher;
2019-03-08 12:04:46 +08:00
public function flushCache($userId)
{
$this->dispatcher->dispatch(new DeleteListenerEvent('user-update', [$userId]));
return true;
}
}
```