pcelta/doctrine-cache-factory

A Doctrine Cache Factory

0.1.0 2016-06-28 14:00 UTC

This package is not auto-updated.

Last update: 2024-11-06 17:07:44 UTC


README

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version Total Downloads License

doctrine-cache-factory

Doctrine Cache Factory it's better way to uncouple your application of the cache adapters. Changing just the configuration the factory will load a different client.

Instalation

{
    "require": {
        "pcelta/doctrine-cache-factory": "dev-master"
    }
}

Adapters Availables

  • Custom
  • Array
  • Memcache
  • Memcached
  • Redis

Write your own adapter

Use the adapter namespace to specify the location of your adapter.

use Pcelta\Doctrine\Cache\Factory;

$factory = new Factory();
$cacheSettings [
    'adapter'           => 'Memcache',
    'adapter_namespace' => '\Doctrine\Common\Cache\%sCache',
    'host'              => '127.0.0.1',
    'port'              => 11211,
];

$cacheProvider = $factory->create($cacheSettings);

How to use Array

use Pcelta\Doctrine\Cache\Factory;

$factory = new Factory();
$cacheSettings [
    'adapter'       => 'Array',
];

$cacheProvider = $factory->create($cacheSettings);

How to use Memcache

Install php5-memcache

sudo apt-get install php5-memcache

use Pcelta\Doctrine\Cache\Factory;

$factory = new Factory();
$cacheSettings [
    'adapter_name' => 'Memcache',
    'host'         => '127.0.0.1',
    'port'         => 11211,
];

$cacheProvider = $factory->create($cacheSettings);

How to use Memcached

Install php5-memcached

sudo apt-get install php5-memcached

use Pcelta\Doctrine\Cache\Factory;

$factory = new Factory();
$cacheSettings [
    'adapter_name' => 'Memcached',
    'host'         => '127.0.0.1',
    'port'         => 11211,
];

$cacheProvider = $factory->create($cacheSettings);

How to use Redis

install PHPRedis

git clone git@github.com:phpredis/phpredis.git
cd phpredis
phpize
./configure
make && make install
use Pcelta\Doctrine\Cache\Factory;

$factory = new Factory();
$cacheSettings [
    'adapter_name' => 'Redis',
    'host'         => '127.0.0.1',
    'port'         => 11211,
];

$cacheProvider = $factory->create($cacheSettings);

General Usage

use Pcelta\Doctrine\Cache\Factory;

$factory = new Factory();
$cacheSettings [
    'adapter_name' => 'Memcache',
    'host'         => '127.0.0.1',
    'port'         => 11211,
];

$cacheProvider = $factory->create($cacheSettings);

$cacheProvider->save('your-key', 'your-data');
$data = $cacheProvider->fetch('your-key');

echo $data; // your-data

Comparing Doctrine Cache Factory with Doctrine Cache Pure

Doctrine Cache

$memcache = new Memcache(); // it's bad to uncouple
$memcache->connect('memcache_host', 11211);

$cacheDriver = new \Doctrine\Common\Cache\MemcacheCache();
$cacheDriver->setMemcache($memcache);
$cacheDriver->save('cache_id', 'my_data');

Doctrine Cache Factory

$factory = new \Pcelta\Doctrine\Cache\Factory();
$cacheSettings [
    'adapter_name' => 'Memcache', // it's much better
    'host'         => '127.0.0.1',
    'port'         => 11211,
];

$cacheProvider = $factory->create($cacheSettings); 
$cacheProvider->save('cache_id', 'your-data');

Road Map

  • Couchbase

License

MIT License