I want to use Doctrine DBAL Cache Adapter as a fallback cache to my other cache options to store external data. Not to store doctrine db results!
The documentation gives me this: https://symfony.com/doc/6.4/components/cache/adapters/doctrine_dbal_adapter.html
However I don't find any information if I have to create the caching table myself. Do I? Or is it created automatically? Do I have to choose a name for the table? Is there a standard? How do I pass in my default db connection?
What I have done so far is I configured it as a part of my already complex caching configuration:
framework:
cache:
system: cache.adapter.filesystem
app: cache.adapter.filesystem
pools:
cache.sap:
adapter: cache.adapter.filesystem
cache.pers:
adapter: cache.adapter.filesystem
cache.db: # <-- this is the new one
adapter: app.cache.adapter.db_cache
services:
app.cache.adapter.db_cache:
parent: 'cache.adapter.doctrine_dbal'
tags:
- { name: 'cache.pool', namespace: 'sapData', default_lifetime: '0' }
cache.db being the new db cache I want to use.
I also have set up the saving and fetching of the data in the code.
What I haven't found is any current example configuration of a setup.
I found examples for the PDO cache like this: Symfony3: How to enable PDO/Doctrine cache adapter, the right way? But this is symfony3 and other examples are even older.
This was a nightmare to find out! After 3 weeks trying around this is a working solution. May it be of use to whoever wants to use a db as cache in the year 2024!
framework:
cache:
system: cache.adapter.apcu
app: cache.adapter.apcu
pools:
cache.db:
adapter: app.cache.custom_db_cache
default_lifetime: 2592000
services:
app.cache.custom_db_provider:
class: Symfony\Component\Cache\Adapter\DoctrineDbalAdapter
app.cache.custom_db_cache:
parent: 'cache.adapter.doctrine_dbal'
tags:
- { name: 'cache.pool', namespace: 'sapDataDb' }
Be sure to check the migrations. At some point There was a new one and I am not sure if it was caused by a call of bin/console do:mi:di
However I will add the generated migrations also here so you ave an idea how the caching table might look like. Those are the default names as I saw no reason to change them:
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE cache_items (item_id VARBINARY(255) NOT NULL, item_data MEDIUMBLOB NOT NULL, item_lifetime INT UNSIGNED DEFAULT NULL, item_time INT UNSIGNED NOT NULL, PRIMARY KEY(item_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('DROP TABLE cache_items');
}