does version of ZF2 matter in enabling memcache? - php

actually , i already enabled the memcache on a projet zf2 version 2.2.0 and when trying to do the same on a new project zf2 with version 2.5.1; i can't.
Here is what i tried ( module.config.php ) :
'service_manager' => array(
'abstract_factories' => array(
'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
'Zend\Log\LoggerAbstractServiceFactory',
),
'factories' => array(
'doctrine.cache.mycache' => function ($sm) {
$cache = new \Doctrine\Common\Cache\MemcacheCache();
$memcache = new \Memcache();
$memcache->connect('localhost', 11211);
$cache->setMemcache($memcache);
return $cache;
},
),
),
// Doctrine config
'doctrine' => array(
'driver' => array(
__NAMESPACE__ . '_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
),
),
),
/***** enabling the memcache ****/
'configuration' => array(
'orm_default' => array(
'metadata_cache' => 'memcache',
'query_cache' => 'memcache',
'result_cache' => 'memcache',
)
),
/**** end ****/
'cache' => array(
'memcache' => array(
'instance' => 'doctrine.cache.mycache',
),
),
),
What i missed up ?
Thanks.

have you activate memcache as session container in web-server settings?
For apache:
session.save_handler = memcached
session.save_path = "localhost:11211"
EDIT
Are you sure, you are using memcache and no memcached?

Related

How to use the cache in doctrine 2 and zend framework 2?

plz i need some help here , i've goolged a lot but without result :/
how can i exploit the query and their result stored in the memcache , i'm working with zend framework 2 and doctrine 2 ? and here is my configuration in module.config.php :
// Doctrine config
'doctrine' => array(
'driver' => array(
__NAMESPACE__ . '_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
),
)
),
/***** enabling the memcache ****/
'configuration' => array(
'orm_default' => array(
'metadata_cache' => 'mycache',
'query_cache' => 'mycache',
'result_cache' => 'mycache',
)
/**** end ****/
)
),
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
'doctrine.cache.mycache' => function ($sm) {
$cache = new \Doctrine\Common\Cache\MemcacheCache();
$memcache = new \Memcache();
$memcache->connect('localhost', 11211);
$cache->setMemcache($memcache);
return $cache;
},
),
),
any idea or link is appeciated , thanks.
Regards.
I suppose You are using DoctrineModule, right?
Change your configuration to this:
// Doctrine config
'doctrine' => array(
'driver' => array(
__NAMESPACE__ . '_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
),
),
),
/***** enabling the memcache ****/
'configuration' => array(
'orm_default' => array(
'metadata_cache' => 'memcache',
'query_cache' => 'memcache',
'result_cache' => 'memcache',
)
),
/**** end ****/
'cache' => array(
'memcache' => array(
'instance' => 'doctrine.cache.mycache',
),
),
),
'service_manager' => array(
'factories' => array(
'doctrine.cache.mycache' => function ($sm) {
$cache = new \Doctrine\Common\Cache\MemcacheCache();
$memcache = new \Memcache();
$memcache->connect('localhost', 11211);
$cache->setMemcache($memcache);
return $cache;
},
),
),
How does this work?
In module configuration are predefined configurations for every supported cache adapter, including memcache. With this configuration, you are saying "use memcache for caching":
'configuration' => array(
'orm_default' => array(
'metadata_cache' => 'memcache',
'query_cache' => 'memcache',
'result_cache' => 'memcache',
)
),
This cache needs configured Memcache instance and this config saying "Memcache instance is available in ServiceManager with key 'doctrine.cache.mycache'"
'cache' => array(
'memcache' => array(
'instance' => 'doctrine.cache.mycache',
),
),
Update:
How to use result cache (documentation):
$cache = $entityManager->getConfiguration()->getResultCacheImpl();
$cacheItemKey = 'my-item';
// test if item exists in the cache
if ($cache->contains($cacheItemKey)) {
$item = $cache->fetch($cacheItemKey); // retrieve item from cache
} else {
$item = $repository->find($id); // retrieve item from repository
$cache->save($cacheItemKey, $item); // save item to cache
}

How to know if the cache in doctrine 2 + zend framewrok is working?

Here is my configuration for the cache in a doctrine 2 + zend framewrok 2 project :
module.config.php :
// Doctrine config
'doctrine' => array(
'driver' => array(
__NAMESPACE__ . '_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity'),
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
),
)
),
'configuration' => array(
'orm_default' => array(
'metadata_cache' => 'mycache',
'query_cache' => 'mycache',
'result_cache' => 'mycache',
)
)
),
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
// fin la partie ajouté pour le cache doctrine 2
'doctrine.cache.mycache' => function ($sm) {
$cache = new \Doctrine\Common\Cache\MemcacheCache();
$memcache = new \Memcache();
$memcache->connect('localhost', 11211);
$cache->setMemcache($memcache);
return $cache;
},
),
),
I have no error , but i need to know if the cache is working or not ? how this can be done ?
thanks.
If you need to check integration between ZF and Doctrine Cache, you can change cache configuration to use filesystem adapter and check files in data folder:
'configuration' => array(
'orm_default' => array(
'metadata_cache' => 'filesystem',
'query_cache' => 'filesystem',
'result_cache' => 'filesystem',
)
)
And if you need to check if data were written into memcache, you can use telnet API:
Connect:
telnet 127.0.0.1 11211
List all items:
stats items

Could not find driver will be seen on command prompt

I am implementing doctrin2 with zend framework2 .when i am executing a command
./vendor/doctrine/doctrine-module/bin/doctrine-module orm:convert-mapping --namespace="Album\\Entity\\" --force --from-database annotation ./module/Album/src/
then i have seen an error [PDOException] could not find driver .
My code is given below
My doctrine.local.php
<?php
return array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOPgSql\Driver',
'params' => array(
'user' => 'root',
'password' => '',
),
),
)
));
?>
my doctrine.global.php
<?php
return array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'driver' => 'pdo_mysql',
'host' => 'localhost',
'dbname' => 'test',
),
),
)
));
?>
my application.module.config.php
'modules' => array(
'Application',
'DoctrineModule',
'DoctrineORMModule',
'Album',
),
In my album/module.config.php i have also add this code
namespace Album;
'doctrine' => array(
'driver' => array(
__NAMESPACE__ . '_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
)
)
)
)
There is a missing driver key in your params array.
Did you enabled the specific driver extensions such as php_pdo_mysql, php_pdo_pgsql in your PHP config?
open ./vendor/doctrine/doctrine-module/bin/doctrine-module
and change
#!/usr/bin/env php
to
#!/usr/bin/env php5

zend framework 2 + doctrine2 + DoctrineDataFixtureModule no loading fixtures

hi have some problems with zend framework 2 + doctrine2 and DoctrineDataFixtureModule (https://github.com/Hounddog/DoctrineDataFixtureModule)
the module is not loading my fixtures
this is my config
'doctrine' => array(
'eventmanager' => array(
'orm_default' => array(
'subscribers' => array(
'Gedmo\Tree\TreeListener',
'Gedmo\Timestampable\TimestampableListener',
'Gedmo\Sluggable\SluggableListener',
'Gedmo\Loggable\LoggableListener',
'Gedmo\Sortable\SortableListener'
),
),
),
'driver' => array(
__NAMESPACE__.'_driver' => array(
'class' =>'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(
__DIR__ . '/../src/'.__NAMESPACE__.'/Entity',
)
'data-fixture' => array(
__NAMESPACE__.'_fixture' => __DIR__ . '/../src/'.__NAMESPACE__.'/Fixtures',
),
),
'translatable_metadata_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(
'vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/Entity',
),
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__.'\Entity' => __NAMESPACE__.'_driver',
// 'Gedmo\Translatable\Entity' => 'translatable_metadata_driver',
)
),
)
),
the problem is i dont know how set the fixtures path or what exactly key in the array i have to set the fixtures path
in the documentation the developer says:
To register drivers with Doctrine module simply add the drivers to the
doctrine.driver key in your configuration.
return array(
'data-fixture' => array(
'ModuleName_fixture' => __DIR__ . '/../src/ModuleName/Fixture',
),
);
You actually need to add the data-fixture array to the root of the configuration array, not in the doctrine array. Like so:
return array(
...
'doctrine' => array(
'eventmanager' => array(
'orm_default' => array(
'subscribers' => array(
'Gedmo\Tree\TreeListener',
'Gedmo\Timestampable\TimestampableListener',
'Gedmo\Sluggable\SluggableListener',
'Gedmo\Loggable\LoggableListener',
'Gedmo\Sortable\SortableListener'
),
),
),
'driver' => array(
__NAMESPACE__.'_driver' => array(
'class' =>'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(
__DIR__ . '/../src/'.__NAMESPACE__.'/Entity',
)
),
'translatable_metadata_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(
'vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/Entity',
),
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__.'\Entity' => __NAMESPACE__.'_driver',
// 'Gedmo\Translatable\Entity' => 'translatable_metadata_driver',
)
),
)
),
'data-fixture' => array(
__NAMESPACE__.'_fixture' => __DIR__ . '/../src/'.__NAMESPACE__.'/Fixtures',
),
...
);
I'll admit the wording in the documentation is a bit confusing.

mongo-odm configuration (module / global) in zf2

I was following this tutorial
http://www.bigwisu.com/2012/10/03/zend-framework-2-doctrine-odm
when a get this error:
The class 'Application\Document\User' was not found in the chain configured namespaces
This is my module.doctrine-mongo-odm.local.php after a little bit of testing:
<?php
return array(
'doctrine' => array(
'connection' => array(
'odm_default' => array(
'server' => 'localhost',
'port' => '27017',
'user' => '',
'password' => '',
'dbname' => 'test',
'options' => array()
),
),
'configuration' => array(
'odm_default' => array(
'metadata_cache' => 'array',
'driver' => 'odm_default',
'generate_proxies' => true,
'proxy_dir' => 'data/DoctrineMongoODMModule/Proxy',
'proxy_namespace' => 'DoctrineMongoODMModule\Proxy',
'generate_hydrators' => true,
'hydrator_dir' => 'data/DoctrineMongoODMModule/Hydrator',
'hydrator_namespace' => 'DoctrineMongoODMModule\Hydrator',
'default_db' => 'test',
'filters' => array(), // array('filterName' => 'BSON\Filter\Class'),
'logger' => null // 'DoctrineMongoODMModule\Logging\DebugStack'
)
),
'odm_default' => array(
'drivers' => array(
'Application\Document' => 'odm_driver'
)
),
'odm_driver' => array(
'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(
'module/Application/src/Application/Document'
),
),
'documentmanager' => array(
'odm_default' => array(
'connection' => 'odm_default',
'configuration' => 'odm_default',
'eventmanager' => 'odm_default'
)
),
'eventmanager' => array(
'odm_default' => array(
'subscribers' => array()
)
),
),
);
I could fix the error by adding this information to the the Application/config/module.config.php and remove it from the global conf:
<?php
namespace Application;
return array(
// routes, etc
'doctrine' => array(
'driver' => array(
'odm_driver' => array(
'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Document')
),
'odm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Document' => 'odm_driver'
)
)
)
)
Can you explain me, why this is working? And what is the best way to go, since i need the odm in different modules? Define it in every module.config.php where its needed?
Played a little with the configs I managed to setup Global configruation.
'odm_driver' => array(
'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Document')
),
'odm_default' => array(
'drivers' => array(
Change is here --> 'Admin\Document' => 'odm_driver'
)
)
As you can see, I've changed NAMESPACE to strict value, this made the trick. Really don't understand till the end new Namespaces in the ZF2.

Categories