Skip to content

Instantly share code, notes, and snippets.

@mneuhaus
Created December 3, 2012 11:47
Show Gist options
  • Save mneuhaus/4194460 to your computer and use it in GitHub Desktop.
Save mneuhaus/4194460 to your computer and use it in GitHub Desktop.
Caching

The Caching API currently is quite low-level. Tagging is useful to flush specific parts, but the tagging and flushing itself must still completly be implemented manually.

I think there are a few usecases that could be simplefied through some api love:

  • cache-entry for specific conditional arguments
  • cache-entry conditionally based on current user roles
  • cache-entry based on one or all entities of a specific type, maybe even a specific property
  • cache-entry based on directory/path changes

I could imagine to do this with a 'cacheEntry' API layer above the current Caching API:

$cache = $this->cacheManager->getCache('My_Cache');
$cacheEntry = $cache->getEntry();
$cacheEntry->setArguments(array('some specifiying', 'arguments'));
$cacheEntry->setEntitiesCondition('My\Package\Domain\Model\Address');
$cacheEntry->setEntityCondition($addressIdentifier);
$cacheEntry->setRoleCondition(TRUE);
$cacheEntry->setUserCondition(TRUE);
$cacheEntry->setDirectoryCondition('resource://My.Package/...');
$cacheEntry->setConfigurationCondition('Settings');

if (!$cacheEntry->exists()) {
    $cacheEntry->set($cacheData);
}
return $cacheEntry->get();

The different conditions would be collected and sha'ed into a cache-identifier. The set function would push that cacheData into the cache and tags it with the set conditions.

Then there could be some signals and automatic flushes to handle such cases really easily.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment