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
Related
In my symfony project i use the predis package to connect with redis;
In my config.yml :
doctrine_cache:
providers:
redis_cache:
predis:
host: 127.0.0.1
port: 6379
In my config_dev.yml :
doctrine_cache:
providers:
redis_cache:
type: array
This works fine,
However, when the redis server is down, my production doesnt work either.
Is there a way for me to go through redis, but when the connection fails just act like there is no caching, just like in dev?
Thanks in advance, let me know if i need to clarify my question more.
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.
I am having difficulty configuring the Doctrine Extension Taggable provided here:
https://github.com/FabienPennequin/DoctrineExtensions-Taggable
My project is using Symfony 2 Fullstack and my configuration is using yaml while my doctrine entities are using annotation. I installed DoctrineExtensions using composer. Adding "fpn/doctrine-extensions-taggable": "dev-master" to the require section on composer.json and then running composer update. This installed without issue.
I then become lost at this section: https://github.com/FabienPennequin/DoctrineExtensions-Taggable#setup-doctrine
I understand that the metadata is a Doctrine Entity however as previously mentioned I am using yaml for my symfony configuration as well as entity managers. Here is the excerpt from my config.yml file:
# Doctrine Configuration
doctrine:
dbal:
default_connection: default
connections:
default:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
orm:
default_entity_manager: main
auto_generate_proxy_classes: "%kernel.debug%"
entity_managers:
main:
connection: default
mappings:
VendorMainBundle:
prefix: Vendor\MainBundle\Entity
taggable:
connection: default
mappings:
taggable:
type: xml
prefix: DoctrineExtensions\Taggable\Entity
dir: %kernel.root_dir%/../vendor/fpn/doctrine-extensions-taggable/metadata
However, when I run php app/console doctrine:mapping:info --em=taggable I get the error:
[Exception]
You do not have any mapped Doctrine ORM entities according to the current configuration. If you have entities or mapping files you should check your mapping configuration for errors.
Should the above command show the mappings described in the xml files?
Thereby allowing me to update the schema in the database?
I used this documenation as reference for the config.yml file: http://symfony.com/doc/current/reference/configuration/doctrine.html#mapping-configuration
I also added this under the config.yml in order to setup the TagListener. Is this correct?
services:
taggable:
class: DoctrineExtensions\Taggable\TagListener
EDIT [#Grimv01k]:
The TagListener requires an argument passed that is an instance of the TagManager Object. I created another service to handle that as follows and passed it to the TagListener:
tag.manager:
class: DoctrineExtensions\Taggable\TagManager
tags:
- { name: doctrine.event_subscriber, connection: default }
arguments:
entity.manager: #doctrine.orm.entity_manager
taggable:
class: DoctrineExtensions\Taggable\TagListener
arguments:
manager: #tag.manager
The TagManager requires an argument of the entityManager however by doing so results in error:
[Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException]
Circular reference detected for service "doctrine.dbal.default_connection", path: "doctrine.dbal.default_connection".
Across the web it's recommended to resolve this error by passing #service_container and in the constructor of the object pull out the entity_manager, however being a vendor I'd prefer not to modify their code. Is there another way?
Just guess: maybe that happens becuse you haven't got tags applied in service, and Doctrine doesn't use this in your complier pass. Try to do it like this:
services:
taggable:
class: DoctrineExtensions\Taggable\TagListener
tags:
- { name: doctrine.event_subscriber, connection: default }
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: ~
I turn on caching the component by editing cache.yml file in my module
_startpage:
enabled: true
contextual: true
lifetime: 60
There was no time difference after that but I think the component is cached because there is a symfony cache box over it (With cache info). I realized that the number of queries to DB is the same as without caching - component for sure execute some queries so why the number of queries did not changed?
Solved.
factories.yml
view_cache:
class: sfFileCache
param:
automatic_cleaning_factor: 0
cache_dir: %SF_TEMPLATE_CACHE_DIR%
lifetime: 86400
prefix: %SF_APP_DIR%/template