Skip to content

Instantly share code, notes, and snippets.

@thephoenics
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thephoenics/ee7de9f95bfdf5f6c24f to your computer and use it in GitHub Desktop.
Save thephoenics/ee7de9f95bfdf5f6c24f to your computer and use it in GitHub Desktop.
PhpFastCacheProvider for using with Doctrine.
<?php
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Cache\DefaultCacheFactory;
use Doctrine\ORM\Cache\Region\DefaultRegion;
use Doctrine\ORM\Cache\RegionsConfiguration;
use Doctrine\ORM\Cache\Logging\StatisticsCacheLogger;
use Doctrine\ORM\Cache\DefaultCache;
require_once __DIR__."/vendor/autoload.php";
require_once __DIR__."/src/CacheProvider/PhpFastCacheProvider.php";
$cache = new PhpFastCacheProvider();
$isDevMode = true;
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src/models"), $isDevMode);
$regions_config = new RegionsConfiguration();
$factory = new DefaultCacheFactory($regions_config, $cache);
$config->setSecondLevelCacheEnabled(true);
$config->getSecondLevelCacheConfiguration()
->setCacheFactory($factory);
$conn = array(
'driver' => 'pdo_mysql',
'host' => 'localhost',
'dbname' => 'silex_test',
'user' => 'root',
'password' => 'root',
'charset' => 'utf8',
);
$entityManager = EntityManager::create($conn, $config);
$cache = new DefaultCache($entityManager);
To get the best use for the cache, install the latest version of doctrine dbal and orm.
composer require doctrine/dbal:2.5.*@dev
composer require doctrine/orm:2.5.*@dev
This helps you to use second level cache.
You need to enable the secondary cache using something similar to the code below.
<?php
use Doctrine\Common\Cache\CacheProvider;
require_once __DIR__."/../autoload.php";
require_once __DIR__.'/../phpfastcache/phpfastcache/phpfastcache_v2.1_release/phpfastcache/phpfastcache.php';
class PhpFastCacheProvider extends CacheProvider
{
public function __construct()
{
phpFastCache::setup("storage","auto");
phpFastCache::setup("path", dirname(__FILE__)."/../../cache");
}
/**
* Fetches an entry from the cache.
*
* @param string $id The id of the cache entry to fetch.
*
* @return string|boolean The cached data or FALSE, if no cache entry exists for the given id.
*/
public function doFetch($id)
{
$value = __c()->get($id);
if ($value == null)
{
return false;
}
return $value;
}
/**
* Tests if an entry exists in the cache.
*
* @param string $id The cache id of the entry to check for.
*
* @return boolean TRUE if a cache entry exists for the given cache id, FALSE otherwise.
*/
public function doContains($id)
{
return __c()->isExisting($id);
}
/**
* Puts data into the cache.
*
* @param string $id The cache id.
* @param string $data The cache entry/data.
* @param int $lifeTime The lifetime. If != 0, sets a specific lifetime for this
* cache entry (0 => infinite lifeTime).
*
* @return boolean TRUE if the entry was successfully stored in the cache, FALSE otherwise.
*/
public function doSave($id, $data, $lifeTime = 0)
{
__c()->set($id, $data, ($lifeTime == 0) ? 24*3600 : $lifeTime);
}
/**
* Deletes a cache entry.
*
* @param string $id The cache id.
*
* @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise.
*/
public function doDelete($id)
{
__c()->delete($id);
}
/**
* Flushes all cache entries.
*
* @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise.
*/
public function doFlush()
{
__c()->clean();
}
/**
* Retrieves cached information from the data store.
*
* @since 2.2
*
* @return array|null An associative array with server's statistics if available, NULL otherwise.
*/
public function doGetStats()
{
return __c()->stats();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment