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
Related
I am setting up a Symfony 5.2 app, using doctrine/doctrine-bundle 2.4 and doctrine/orm 2.9. My doctrine.yaml config file looks like this:
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
# IMPORTANT: You MUST configure your server version,
# either here or in the DATABASE_URL env var (see .env file)
#server_version: '13'
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
The problem is that when I run doctrine:fixtures:load, I get the following output:
In AbstractPostgreSQLDriver.php line 102:
An exception occurred in driver: could not find driver
In Exception.php line 18:
could not find driver
In PDOConnection.php line 39:
could not find driver
It looks like Doctrine is trying to use the Postgre driver, though I have specified MySQL. Is there another place where I need to configure this? If so, how?
===
EDIT: It looks like DATABASE_URL is not available inside my Docker container, which is probably the source of the problem. I've verified that DATABASE_URL is defined in my .env file, and that my .env file is in the same folder as my docker-compose.yml file. Also, I have a setting like this for the environment variable in that docker-compose file:
environment:
- DATABASE_URL
So I'm not quite sure what's wrong here.
I think you have a wrong DATABASE_URL in your .env file. Please check if it starts with mysql://.
I believe symfony uses dotenv package to load the values from .env to the S_ENV superglobal. You need to debug whether your dotenv (and there is a .env in your project and it is not .env.local or .env.dist) is loaded properly.
It has happened to me sometimes when I was starting, typically when cloning a Github project which has a default .env file created by Doctrine when it's installed through composer that by default Doctrine uses PostgreSQL URL.
See how many .env files have you document and I'm sure that someone overrides your URL connection.
See this StackOverflow post to know the difference between the .env.* files What is the difference between .env.local and .env.development.local
I have this configuration for docker:
# docker-compose.yml
environment:
REDIS_SERVER: redis
REDIS_PORT: 6379
And then in symfony I use this to configure parameters:
# parameters.yml
redis_port: %env(REDIS_PORT)%
And then I use the parameter to configure Picmore's cache:
```yaml
# pimcore.yml
cache:
pools:
redis:
enabled: true
connection:
port: '%redis_port%'
But I get the error
Invalid type for path "pimcore.cache.pools.redis.connection.port". Expected int, but got string.
The error is clear but I can't solve it, I tried different options:
redis_port: '%env(REDIS_PORT)%'
redis_port: '%env(int:REDIS_PORT)%'
But I get the same error. How can I fix this?
You are using Symfony 3.3, but the int environment variable preprocessor was not added until Symfony 3.4, according to this.
I don't think you'll be able to use env vars to configure this value, since environment variables are strings by default.
Your options are to either use different parameters files for different environment, as things used to be done back then (which I understand can be inconvenient when using containers); or upgrade to a more recent version of Symfony.
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.
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