Skip to content

Latest commit

 

History

History
88 lines (72 loc) · 2.7 KB

cache.md

File metadata and controls

88 lines (72 loc) · 2.7 KB

Automatic cache

To start using auto-cache feature you need:

  1. Setup any psr-16 cache implementation.
  2. Change parent class of your Table from Composite\DB\AbstractTable to Composite\DB\AbstractCachedTable
  3. Implement method getFlushCacheKeys()
  4. 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

Configuration

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);

What is getFlushCacheKeys() for

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 [];
    }
}