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:
decrement($key, $by = 1)
delete($key)
flush()
get($key)
has($key)
increment($key, $by = 1)
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.
Opulence\Cache\ArrayBridge
provides a simple cache bridge most useful for running tests.
use Opulence\Cache\ArrayBridge;
$arrayBridge = new ArrayBridge();
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');
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.
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.