I'm testing Memcached on my Symfony2 application
I set it to cache doctrine's queries, results and metadata:
orm:
entity_managers:
default:
metadata_cache_driver:
type: service
id: doctrine.cache.memcache2
query_cache_driver:
type: service
id: doctrine.cache.memcache2
result_cache_driver:
type: service
id: doctrine.cache.memcache2
services:
memcache:
class: Memcache
calls:
- [ addserver, [ 'localhost', 11211 ]]
doctrine.cache.memcache2:
class: Doctrine\Common\Cache\MemcacheCache
calls:
- [setMemcache, [#memcache]]
Until now, everything works fine.
I was wondering how does doctrine behaves if the Memcached server goes down. As far as I could see, the application breaks. In dev mode I get the following message:
Notice: MemcachePool::get(): Server localhost (tcp 11211, udp 0) failed with: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
(10060)
500 Internal Server Error - ContextErrorException
In production mode I'm also presented with an http 500.
Is there a way to tell doctrine to bypass/ignore the Memcached server and go directly to the database, instead of returning 500s?
You can use The Second Level Cache available in doctrine/orm 2.5
see:
http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/second-level-cache.html
The idea to keep you default options for orm and add a memcache as second level cache:
orm:
entity_managers:
default:
auto_mapping: true
second_level_cache:
region_cache_driver:
type: service
id: doctrine.cache.memcache2
enabled: true
regions:
region_name:
cache_driver:
type: service
id: doctrine.cache.memcache2
When turned on, entities will be first searched in cache and if they are not found, a database query will be fired and then the entity result will be stored in a cache provider.
Related
Hey guys so im trying to connect to my databse that's already created in postgresql (I'm using PGAdmin4 for the user interface of postgresql).
I'm on Symfony CLI version v4.16.3 get three different Timeout.
In details i have a .env file that take the my database url :
DATABASE_URL=postgresql://root:password#127.0.0.1:35583/nd2d?serverVersion=12.3
My doctrice.yaml is set up like this :
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
I had issue but found a way to fix they were related to pdo drivers for postgresql.
but after i launch the command bin/console make:migration i get those 3 errors:
In AbstractPostgreSQLDriver.php line 73:
An exception occurred in driver: SQLSTATE[08006] [7] timeout expired
In PDOConnection.php line 31:
SQLSTATE[08006] [7] timeout expired
In PDOConnection.php line 27:
SQLSTATE[08006] [7] timeout expired
I avoided working on heavyn program with PHP or any framework related to it but this time i don't have a choice, soo much configuration i cant take it anymore, been on this for soo many hours now. What am i missing.
Thank you :)
Error came from listening on the wrong port, i was listening to the port 35583, which is the port that PgAdmin is showed on the web browse.
DATABASE_URL=postgresql://root:password#127.0.0.1:35583/nd2d?serverVersion=12.3
The port i should have been listening to is the default postgresql port of 5432
I am adding Memcached to my app and despite some very confusing setup and apparent duplicate it seemed to work, if only barely.
Then I needed to clear the cache after some changes using
./bin/console cache:clear --env=dev ( or =prod )
That's when I got the error:
[Symfony\Component\Cache\Exception\CacheException]
Memcached >= 2.2.0 is required
Well, according to phpinfo(), the Memcached version I've installed is 3.0.4, so I am not sure where that error is coming from.
EDIT: Adding that I am have also got the error below when attempting the cache:clear command:
PHP Fatal error: Class 'Memcached' not found in /Users/user/Sites/Symfony1/vendor/doctrine/doctrine-cache-bundle/Tests/Functional/Fixtures/Memcached.php on line 6
As said the configuration is not at all clear. There's apparent overlap and my research on SO and elsewhere did not help much.
Also, I am using the memcached adapter on the Controller like so:
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
which comes from Symfony's cache component, which does not seem to require any configuration on config.yml.
In any case, here's the configuration cache wise.:
/app/config.yml
framework:
cache:
app: cache.adapter.memcached
default_memcached_provider: "memcached://127.0.0.1:11211"
...
orm:
...
entity_managers:
default:
metadata_cache_driver: memcached
result_cache_driver:
type: memcached
host: 127.0.0.1
port: 11211
instance_class: Memcached
query_cache_driver: memcached
doctrine_cache:
aliases:
mem_cached_meta: my_memcached_cache_metadata
mem_cached_query: my_memcached_cache_query
mem_cached_result: my_memcached_cache_result
providers:
my_memcached_cache_metadata:
type: memcached
namespace: metadata_cache_ns
aliases:
- mem_cached_meta
my_memcached_cache_query:
type: memcached
namespace: query_cache_ns
aliases:
- mem_cached_query
my_memcached_cache_result:
type: memcached
namespace: result_cache_ns
aliases:
- mem_cached_result
I am using MongoDB Atlas cloud services with my Symfony 3 application and I am having lots of warnings in my PHP logs saying MongoClient::__construct(): SSL: Connection reset by peer. Even though I have warnings some times, I can connect successfully and read/write to the server.
I use Doctrine ODM which uses MongoClient for connecting to the server. I have configured the replicaSet according to MongoDB Atlas configuration.
When I look at MongoDB logs, I do not see any strange thing that appears to be linked to my issue.
Here is my connection string: mongodb://<USERNAME>:<PASSWORD>#datalake-shard-00-00-lnres.mongodb.net:27017,datalake-shard-00-01-lnres.mongodb.net:27017,datalake-shard-00-02-lnres.mongodb.net:27017/test?ssl=true&replicaSet=DataLake-shard-0&authSource=admin
Here is my Doctrine ODM config
doctrine_mongodb:
connections:
default:
server: "mongodb://<USERNAME>:<PASSWORD>#datalake-shard-00-00-lnres.mongodb.net:27017,datalake-shard-00-01-lnres.mongodb.net:27017,datalake-shard-00-02-lnres.mongodb.net:27017/test?ssl=true&replicaSet=DataLake-shard-0&authSource=admin"
options:
authMechanism: SCRAM-SHA-1
authSource: admin
connect: true
ssl: true
password: "<PASSWORD>"
username: "<USERNAME>"
readPreference: secondaryPreferred
replicaSet: "DataLake-shard-0"
default_database: "test"
document_managers:
default:
auto_mapping: true
retry_connect: 4
retry_query: 4
I start using Redis on me project (php-redis). Is a Symfony2 project and i found the:
https://github.com/snc/SncRedisBundle
I follow the installation process and i configured:
Some clients to store no-sql data and cache
Sessions storage
Doctrine metada, result and query cache
I create a new entity in a bundle and i fail because i create it at yml and i have all others with annotation system, so i delete yml format and create the annotation.
Every change i make on the annotation class (change the table name for example), is not affecting the schema or the database, even i recreate the database or try to execute cache:clear with all the options.
If i just comment the redis doctrine configuration lines, it works and i can see the changes on the schema.
Im maybe forgetting something, or i cant really find how to clean that doctrine redis cache.
¿I have to manually clean any position on the redis client use for caching?
Here is the configuration:
#Snc Redis Bundle
snc_redis:
clients:
d2a:
type: phpredis
alias: d2a
dsn: redis://localhost/1
cache:
type: phpredis
alias: cache
dsn: redis://localhost
logging: true
session:
client: d2a
prefix: redis_session
doctrine:
metadata_cache:
client: cache
entity_manager: default # the name of your entity_manager connection
document_manager: default # the name of your document_manager connection
result_cache:
client: cache
entity_manager: [default, read] # you may specify multiple entity_managers
query_cache:
client: cache
entity_manager: default
The easiest way but not the best one is to flush redis db with doctrine cache. Run
php app/console redis:flushdb --client=cache
(Not tested!) Another way is to setup doctrine metadata cache in doctrine config http://symfony.com/doc/current/reference/configuration/doctrine.html#caching-drivers
orm:
entity_managers:
# A collection of different named entity managers (e.g. some_em, another_em)
some_em:
metadata_cache_driver:
type: array # Required
host: ~
port: ~
instance_class: ~
class: ~
We are using symfony 1.4 on our development machine
traditional way to cache partial in symfony is by editing cache.yml
something:
enabled: true
lifetime: true
this will store cache on the disk
but we want to store cache on memcache instead on disk.
so, the question is how to cache symfony partial in memcache
Symfony partial cache work like all other cache in symfony: it refers to view_cache part of apps/frontend/config/factories.yml.
For example, if you want to store your cache in SQLite database:
all:
view_cache:
class: sfSQLiteCache
param:
database: %SF_TEMPLATE_CACHE_DIR%/cache.db
So if you want to store these information into Memcached, you should use the sfMemcacheCache.class.php class:
all:
view_cache:
class: sfMemcacheCache
param:
servers:
server1:
host: localhost
port: 11211
persistent: true
OR
all:
view_cache:
class: sfMemcacheCache
param:
host: localhost
port: 11211
persistent: true