jadell edited this page Oct 7, 2013 · 3 revisions

In order to decrease requests made to the server, it is possible to tell a neo4jphp client to cache the results of entity look-ups.

Caching Plugins

Three caching plugins come with neo4jphp:

  • Null - no caching is performed
  • Variable - entities are cached for the length of the PHP process/request
  • Memcached - entities are cached using PHP's Memcached extension

It is possible to define your own caching plugin by implementing the Everyman\Neo4j\Cache interface.

The following client methods interact with the cache:

  • Client::deleteNode() and Client::deleteRelationship() - Removes the entity by id from the cache.
  • Client::getNode() and Client::getRelationship() - Looks up the entity by id in the cache and returns it. Multiple calls return the same object.
  • Client::loadNode() and Client::loadRelationship() - Looks up the entity by id in the cache, and loads its properties into the supplied entity.
  • Client::saveNode() and Client::saveRelationship() - Stores the entity by id in the cache.

Setting up the Cache

Caching is enabled by instantiating a cache plugin and then calling Client::setCache(). The following code sets up caching using the local variable cache plugin:

$plugin = new Everyman\Neo4j\Cache\Variable();

This example uses the Memcached plugin:

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

$plugin = new Everyman\Neo4j\Cache\Memcached($memcached);