PHPackages                             urodoz/cachemanager - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. urodoz/cachemanager

ActiveSymfony-bundle[Caching](/categories/caching)

urodoz/cachemanager
===================

Bundle for Symfony 2 to handle cache management

2.3.x-dev(12y ago)1410MITPHPPHP &gt;=5.3.2

Since Nov 5Pushed 11y ago1 watchersCompare

[ Source](https://github.com/urodoz/cachemanager)[ Packagist](https://packagist.org/packages/urodoz/cachemanager)[ RSS](/packages/urodoz-cachemanager/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (2)Versions (2)Used By (0)

Urodoz CacheManager
===================

[](#urodoz-cachemanager)

Bundle to handle cache management for Symfony 2

- [Installation](../../wiki/Installation)

Configuration
-------------

[](#configuration)

Update your config.yml file to configure memcache connections and/or redis connection (temporary in development for multiple redis servers)

```
urodoz_cache:
    memcache:
        servers: ["127.0.0.1:11211"]
    redis:
        servers: ["192.168.1.120:6379"]
```

Events
------

[](#events)

**Cache hit**

On cache hit the event *urodoz.events.cachehit* is dispatched on the Symfony 2 EventDispatcher. The event is from class : *Urodoz\\Bundle\\CacheBundle\\Event\\CacheHitEvent*. Contains information about the key used, the content stored on the cache implementation and implementation used.

**Missed cache hit**

On cache hit the event *urodoz.events.missed\_cachehit* is dispatched on the Symfony 2 EventDispatcher. The event is from class : *Urodoz\\Bundle\\CacheBundle\\Event\\MissedCacheHitEvent*. Contains information about the key used and the implementation.

Usage
-----

[](#usage)

To store and retrieve data from memcache servers pool (as service)

```
//Retrieve the service from the ContainerInterface
$cacheManager = $container->get("urodoz_cachemanager");
//Store value
$cacheManager->implementation("memcache")->set($key, $value, 3600);
//Retrieve value
$cacheManager->implementation("memcache")->get($key);
```

When calling to method implementation, it sets the active implementation on the CacheManager service. You can avoid to set again the implementation on the next calls

```
//Storing on memcache
$cacheManager = $container->get("urodoz_cachemanager");
$cacheManager->implementation("memcache")->set($key1, $value1);
$cacheManager->set($key2, $value2);
$cacheManager->set($key3, $value3);
$cacheManager->set($key4, $value4);
//Retrieving from memcache
$data["one"] = $cacheManager->get($key2);
$data["two"] = $cacheManager->get($key3);
$data["three"] = $cacheManager->get($key4);
```

To store and retrieve data from the redis server (as service), you only need to change the implementation requested to cache manager service

```
//Retrieve the service from the ContainerInterface
$cacheManager = $container->get("urodoz_cachemanager");
//Store value
$cacheManager->implementation("redis")->set($key, $value, 3600);
//Retrieve value
$cacheManager->implementation("redis")->get($key);
```

Prefix generation
-----------------

[](#prefix-generation)

The event throwed to update the cache key is *urodoz.events.update\_cachekey* , this is a sample configuration

```
urodoz_cache:
    memcache:
        servers: ["127.0.0.1:11211"]
    redis:
        servers: ["192.168.1.120:6379"]
```

Service configuration attached as listener:

```
    sample.cachePrefixGenerator:
        class: Sample\Bundle\CoreBundle\Service\CachePrefixGenerator
        calls:
            - ["setContainer", [@service_container]]
        tags:
            - { name: kernel.event_listener, event: urodoz.events.update_cachekey, method: onCacheKeyUpdate }
```

Service class:

```
