To start using auto-cache feature you need:
- Setup any psr-16 cache implementation.
- Change parent class of your Table from
Composite\DB\AbstractTable
toComposite\DB\AbstractCachedTable
- Implement method
getFlushCacheKeys()
- Change all internal select methods to their cached versions (example:
findByPkInternal()
to_findByPkCached()
etc.)
You can also generate cached version of your table with console command:
$ php console.php composite-db:generate-table 'App\User' 'App\UsersTable' --cached
For using automatic caching you need to have configured and initialized any PSR-16 package.
If you already have one - just pass cache instance to your Composite\DB\AbstractCachedTable
,
if not - use any from GitHub search
Below you can find example with simple file-based caching package kodus/file-cache.
$ composer require kodus/file-cache
$cache = new \Kodus\Cache\FileCache('/path/to/your/cache/dir', 3600);
This method is very important, it triggers every time before saving changed entity and inside of it you must define all cache keys of lists or count() results queries where this entity participates.
Example: imagine you have table with some posts and you want to cache featured posts
class PostsTable extends AbstractCachedTable
{
protected function getConfig(): TableConfig
{
return TableConfig::fromEntitySchema(Post::schema());
}
public function findByPk(int $id): ?Post
{
return $this->_findByPkCached($id);
}
/**
* @return Post[]
*/
public function findAllFeatured(): array
{
return $this->_findAll(['is_featured' => true]);
}
public function countAllFeatured(): int
{
return $this->_countAllCached(
'is_featured = :is_featured',
['is_featured' => true],
);
}
public getFlushCacheKeys(AbstractEntity|Post $entity): array
{
// to avoid unnecessary cache flushing
// its better to check that changed post is featured or it was
if ($entity->is_featured || $entity->getOldValue('is_featured') === true) {
return [
$this->getListCacheKey(
'is_featured = :is_featured',
['is_featured' => true],
),
$this->getCountCacheKey(
'is_featured = :is_featured',
['is_featured' => true],
),
];
}
return [];
}
}