Clera Redis Cache pool: How to change namespace - symfony

I'm trying to clear redis cache via command line : bin/console cache:pool:clear cache.my_redis
1/ I created a custom provider as already mentioned under the doc: https://symfony.com/doc/4.4/cache.html#custom-provider-options
app.my_custom_redis_provider:
class: \Redis
factory: ['Symfony\Component\Cache\Adapter\RedisAdapter', 'createConnection']
arguments:
- 'redis://redis:6379'
2/ I configured my framework cache key:
framework:
cache:
app: cache.adapter.redis
default_redis_provider: app.my_custom_redis_provider
pools:
cache.my_redis:
adapter: cache.adapter.redis
provider: app.my_custom_redis_provider
After executing bin/console cache:pool:clear cache.my_redis the cache is notreally cleared.
I get: Clearing cache pool: cache.my_redis => Cache was successfully cleared But when I verify my redis storage, nothing is cleared, and it show me these keys:
PHPREDIS_SESSION_o5lpfttunv4r6sfsiktr046s1m
sf_sfmagu8rpbsmm3oplmhnuhl9slp

Related

Doctrine query_cache auto expire

I'm using symfony 3.4 + doctrine + redis by using the composer bundle sncRedisBundle
In my config.yml I enabled doctrine query cache:
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
auto_mapping: false
metadata_cache_driver: redis
query_cache_driver: redis
result_cache_driver: redis # !!!
snc_redis:
clients:
doctrine:
type: predis
alias: doctrine_redis
dsn: "%redis_dsn_doctrine%"
logging: false
options:
connection_persistent: true
doctrine:
query_cache: #!!!!!!!!
#it caches the translation from Doctrine's DQL into SQL.
client: doctrine_redis
namespace: shop_doctrine_query
entity_manager: [default, legacy]
The keys set by doctrine for it's result_cache never expire:
How can I call automatically \Doctrine\ORM\Query::setQueryCacheLifetime or make that cache expire. (Note I don't have access to set redis eviction policies on this server).
I think that Doctrine query cache lifetime aren't configured in the doctrine package nor in the redis package, but in the framework package, where you set your pool of cache.
You should have a look on this issue where a user is asking how to configure RedisCluster with Doctrine Cache. B-Galati answers with a configuration example where it defines lifetime for doctrine result cache.
Could you try something like:
framework:
cache:
pools:
cache.doctrine.orm.default.query: ## I'm not sure of this name
adapter: cache.app
default_lifetime: 25200 # 1 week
Or
framework:
cache:
pools:
doctrine.query_cache_pool:
adapter: cache.app
default_lifetime: 25200 # 1 week
The symfony console cache:pool:list command could help you to identify your pools and the symfony console cache:pool:prune could help you to see if out of date queries are well deleted.

Redis connection (in Doctrine cache) gets initialized on each command

I have a Symfony 4.1 app, where I use Doctrine and want to setup Redis cache for Doctrine.
Here is a part of composer.json
"snc/redis-bundle": "^2.1",
"symfony/doctrine-bridge": "^4.1",
"symfony/proxy-manager-bridge": "^4.1",
Here is yml config file:
snc_redis:
clients:
doctrine_cache:
type: phpredis
alias: doctrine_cache
dsn: '%my_dsn%'
doctrine:
metadata_cache:
client: doctrine_cache
entity_manager: default
The problem is: on the very first time when Symfony tries to generate cache for all DI containers, it initializes Redis connection. This means for example when I run any console command, it tries to connect to redis. Example:
// very first command after git clone and composer install
php bin/console about
Output:
In PhpredisClientFactory.php line 64:
php_network_getaddresses: getaddrinfo failed: No such host is known.
I expect that Redis cache service will be initialized lazily, otherwise I cannot run other build commands on independent (non having Redis) environment.
Can someone advise please?
If this happens after composer install then you might have composer run the default commands:
- "post-install-cmd"
- "post-update-cmd"
Try to remove them and add see if the Redis works... If it works then add a script to your deployment entrypoint to run them at the end.
PS: pay attention to doctrine and Redis if you are using migrations: then you should also clear doctrine cache.

Sqlite file db for codeception testing fails

Attempts to use a sqlite file database in the app/cache/tests directory fail. This is determined by clearing the dev MySQl database and populating the test environment database via the console. Test database being populated was confirmed by an external Sqlite Manager. [Tests performed without the sqlite configuration pass.]
codeception.yml:
actor: Tester
paths:
tests: tests
log: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
settings:
bootstrap: _bootstrap.php
colors: false
memory_limit: 1024M
extensions:
enabled:
- Codeception\Extension\RunFailed
modules:
config:
- Db:
dsn: 'sqlite:./app/cache/test/test.sqlite'
user: ''
password: ''
dump: tests/_data/test.sql
populate: true
cleanup: false
reconnect: true
Edit:
In an Ubuntu VM, adding the Symfony2 configuration to acceptance.yml allows for partial success - the test uses the Sqlite db but does not rewrite from the specified dump (test.sql). In Windows adding the Symfony configuration makes no difference.
acceptance.yml:
class_name: AcceptanceTester
modules:
enabled:
- PhpBrowser:
url: http://vol
- \Helper\Acceptance
# adding these lines enables Ubuntu to use sqlite db
- Symfony2:
app_path: ./app
environment: test
You haven't enabled Db module in acceptance.yml
modules:
enabled:
- Db
- PhpBrowser:
url: http://vol
- \Helper\Acceptance
Also don't add PhpBrowser and Symfony2 at the same time,
only one can be used.

Can one use different database credentials for Doctrine migrations in Symfony2?

How can one configure Symfony's DoctrineMigrationsBundle to use different database authentication credentials to its DoctrineBundle—or at very least, a different DoctrineBundle connection to that used elsewhere in the app?
We would like the app to connect to the database with only limited permissions, e.g. no ability to issue DDL commands such as CREATE, ALTER or DROP. However, migrations will need to execute such DDL commands and so should connect as a user with elevated permissions. Is this possible?
I know it's a very old post, but as it is the one which shows on a Google search on the subject, I add my solution, working with Symfony 4.
First, you just have to define a new database connection in config/doctrine.yml (a new entity manager is NOT needed):
doctrine:
dbal:
default_connection: default
connections:
default:
# This will be the connection used by the default entity manager
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_pgsql'
server_version: '11.1'
charset: UTF8
migrations:
# This will be the connection used for playing the migrations
url: '%env(resolve:DATABASE_MIGRATIONS_URL)%'
driver: 'pdo_pgsql'
server_version: '11.1'
charset: UTF8
orm:
# As usual...
You also have to define the DATABASE_MIGRATIONS_URL with the admin credentials in the .env file or in environnement variables:
###> doctrine/doctrine-bundle ###
# Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
DATABASE_URL=postgresql://app_user:app_user_pass#localhost:5432/db
# Database url used for migrations (elevated rights)
DATABASE_MIGRATIONS_URL=postgresql://admin_user:admin_user_pass#localhost:5432/db
###< doctrine/doctrine-bundle ###
Then, just execute your migrations with the --db option, passing the name of your new connection:
php bin/console doctrine:migrations:migrate --db=migrations
Yes. Just define a new entity manager with the correct connection details and then use that entity manager when running migration commands
$ php app/console doctrine:migrations:version --em=new_entity_manager

Symfony2 configure DB storage sessions

UPDATE: problem solved, see the comments (many issues, the versions differences was but one of them).
I'm trying to configure sessions in Symfony2 in config.yml file. I have the following configuration:
session:
default_locale: %locale%
lifetime: 7200
auto_start: true
storage_id: session.storage.pdo
parameters:
pdo.db_options:
db_table: session
db_id_col: session_id
db_data_col: session_value
db_time_col: session_time
services:
pdo:
class: PDO
arguments:
- "mysql:dbname=%database_name%"
- %database_user%
- %database_password%
session.storage.pdo:
class: Symfony\Component\HttpFoundation\SessionStorage\PdoSessionStorage
arguments: [#pdo, %session.storage.options%, %pdo.db_options%]
It's based on Symfony2's cookbook http://symfony.com/doc/2.0/cookbook/configuration/pdo_session_storage.html
I've created exactly the same table as in the given link.
However, it doesn't work. I get some "blank" error (no error message, but "PDO Exception" and "Error Exception"). I admit I have no much knowledge on configuring the Symfony2 or any info (that's why I'm using cookbook). I lost a lot of time and see no much documentation about it in the internet, not mentioning the fact that internet is quite silent about this case (having session storaged to DB table in Symfony2).
My NetBeans is "shouting" sth about the last line:
arguments: [#pdo, %session.storage.options%, %pdo.db_options%]
"ScannerException while scanning for the next token we had this found character #(64) that cannot start any token".
UPDATE:
Hmm now I'm not sure if it's about the configuration. I can see that Symfony2's cookbook (use... ) example doesn't match actually the file structure in the Symfony2's bundle. In a word, there is no such file-path, but after putting the real one it still doesn't work.
I had the same problem as you with Symfony 2.5. Turns out my solution was to disable session.auto_start in php.ini. The PdoSessionStorage will not take over if the session is being started before PDO has the opportunity to take control of it. I had overlooked this at first because I was modifying the wrong one (I had two copies of php.ini). To check if this is the problem on yours, run this command:
echo ini_get('session.auto_start');
If that returns a '1' or a 'true', then be sure to set this in your php.ini:
session.auto_start = 0
So for reference, here is my setup with Symfony 2.5 to make this work:
config.yml
framework:
session:
handler_id: session.handler.pdo
parameters:
pdo.db_options:
db_table: session
db_id_col: session_id
db_data_col: session_value
db_time_col: session_time
services:
session.handler.pdo:
class: Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler
arguments: ["#pdo", "%pdo.db_options%"]
pdo:
class: PDO
arguments:
dsn: "mysql:host=%database_host%;port=%database_port%;dbname=%database_name%"
user: "%database_user%"
password: "%database_password%"
calls:
- [setAttribute, [3, 2]] # \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION

Resources