Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use-case: caching module's configuration #392

Merged
merged 1 commit into from
Apr 7, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion docs/cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,43 @@ In this case you have to specify a custom factory in your `service_manager` conf
),
```

Please be aware that you can't use *Closures* inside yout module configuration if you want to cache it. To avoid this problem, you have to use a Factory:
```php
// module.config.php
'service_manager' => array(
'factories' => array(
'my_memcached_alias' => __NAMESPACE__ . '\Cache\MemcachedFactory'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Config files usually do not have namespaces. Can you replace __NAMESPACE__ with YourModule?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah nvm, I did not saw that it was already merged :D

)
),
```

```php
// MemcachedFactory.php
namespace YourModule\Cache;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class MemcachedFactory implements FactoryInterface {

/**
* @param ServiceLocatorInterface $serviceLocator
*
* @return \Memcached
*/
public function createService(ServiceLocatorInterface $serviceLocator) {

$memcached = new \Memcached();
$memcached->addServer('localhost', 11211);
return $memcached;
}

}
```


Other supported that need a custom factory are:

* `memcache`: require you to return a `Memcache` instance (use the `my_memcache_alias` as the key in the
service manager).
* `redis`: require you to return a `Redis` instance (use the `my_redis_alias` as the key in the service manager).
* `redis`: require you to return a `Redis` instance (use the `my_redis_alias` as the key in the service manager).