Skip to content

Latest commit

 

History

History
104 lines (71 loc) · 3.19 KB

cache.md

File metadata and controls

104 lines (71 loc) · 3.19 KB

Cache

Table of Contents

  1. Introduction
  2. Array Bridge
  3. File Bridge
  4. Memcached Bridge
  5. Redis Bridge

Introduction

Cache is in-memory storage for frequently-accessed data. Data in cache is commonly short-lived, but it can be persisted for long periods of time. There are many caching libraries out there, such as Redis and Memcached, which offer tons of great features. However, sometimes all your application needs is basic caching functions like:

  1. decrement($key, $by = 1)
  2. delete($key)
  3. flush()
  4. get($key)
  5. has($key)
  6. increment($key, $by = 1)
  7. set($key, $value, $lifetime)

In this case, you can use a cache bridge to provide a simple wrapper around your favorite cache libraries. Opulence supplies the Opulence\Cache\ICacheBridge interface with the above methods as well as some bridges to the most popular cache libraries.

Array Bridge

Opulence\Cache\ArrayBridge provides a simple cache bridge most useful for running tests.

use Opulence\Cache\ArrayBridge;

$arrayBridge = new ArrayBridge();

File Bridge

Opulence\Cache\FileBridge allows you to easily cache data to plaintext files on your server.

use Opulence\Cache\FileBridge;

$fileBridge = new FileBridge('/path/to/my/cache/files');

Memcached Bridge

Opulence\Cache\MemcachedBridge acts as a simple wrapper around Memcached.

use Memcached as Client;
use Opulence\Cache\MemcachedBridge;
use Opulence\Memcached\Memcached;

$client = new Client();
$client->addServer('localhost', 11211);
$memcached = new Memcached($client);
$memcachedBridge = new MemcachedBridge($memcached);

If you would like to use a Memcached client besides the "default" one, specify it:

$memcachedBridge = new MemcachedBridge($memcached, 'some-other-server');

You can add a prefix to all your keys to prevent naming collisions with other applications using your Memcached server:

$memcachedBridge = new MemcachedBridge($memcached, 'default', 'myapp:');

If you need the underlying Memcached instance to do anything beyond what the bridge does, you may call getMemcached().

Note: Read more information about Opulence's Memcached extension.

Redis Bridge

Opulence\Cache\RedisBridge is a simple bridge to Redis.

use Opulence\Cache\RedisBridge;
use Opulence\Redis\Redis;
use Redis as Client;

$client = new Client();
$client->connect('localhost', 6379);
$redis = new Redis($client);
$redisBridge = new RedisBridge($redis);

If you would like to use a Redis client besides the "default" one, specify it:

$redisBridge = new RedisBridge($redis, 'some-other-server');

You can add a prefix to all your keys to prevent naming collisions with other applications using your Redis server:

$redisBridge = new RedisBridge($redis, 'default', 'myapp:');

If you need the underlying Redis instance to do anything beyond what the bridge does, you may call getRedis().

Note: Read more information about Opulence's Redis extension.