An advanced library to extend and handle PECL-Memcached.
Author: David Carlos Manuelda aka StormByte
Email: StormByte@gmail.com
Version: 3.1.0
Release Date: 4/6/2016
Hint
If you find this library useful, and/or it saved you development time/costs, please consider making a donation (by contacting me via email) to support this library's development as well as other libraries I release in GPL format.
Information
This library is created to be an effective wrapper around PECL-Memcached, providing a solid and easy-to-use API with the following benefits:
- Multiple Pools Handling:
Simplifies your code by allowing one or more pools to be handled via the same class. This avoids the need for a separate class for each pool, which can be hard to maintain. Additionally, a "default" pool is defined for cases where only one pool is used, keeping code complexity low.
- Lazy Resource Instantiation:
The Memcached resource is not instantiated until servers are added to a pool. This allows you to disable caching completely without altering your code and avoids PHP errors when PECL-Memcached is not installed but the class is still used (without adding servers to any pool).
To disable caching, simply comment out the lines where servers are added to pools.
- Namespace Support:
Implements namespaces for easy control of keyset expiration. For example, you can bind several user data keys to a namespace, and when the user is deleted, expire the entire namespace to delete all bound keys.
- Selective Use of Exceptions:
Uses object-oriented exceptions only in creating pools, adding servers, and getting data. This ensures that the code for getting/setting items in the cache remains trivial (see the usage example below).
Requirements
- PHP 5:
Exceptions were first implemented in PHP 5, making it the minimum supported version.
- PECL-Memcached:
Required only when you effectively use caching by binding servers to any pool. If no servers are added, the library will not fail but will not store anything in Memcached either.
Class functions
Take a look at StormCache
class documentation
Usage Example
Below is an example of how to use the StormCache
library. This example demonstrates its key features, including multiple pools, lazy resource instantiation, encryption, and namespace handling.
<?php
require_once 'StormCache.php';
$cache= StormCache::GetInstance();
$cache->AddPoolServer("127.0.0.1", 11211);
$userPoolName="User Important Data";
$cache->AddPool($userPoolName);
$cache->AddPoolServer("127.0.0.1", 11233, 0, $userPoolName);
$cache->SetEncryptionPassword("mypasswordforencrypt1");
$data=NULL;
try {
$cache->Get("ServerData", $data, "POOL_NAME");
}
try {
$cache->Get("KEY", $data, "POOL_NAME");
} catch (Exception $ex) {
$data=some_database_call();
$ok=$cache->Set("ServerData", $data, NULL, 24*3600);
if (!$ok) {
some_database_call($data);
}
}
$data=NULL;
$userID=1;
try {
$cache->Get("UserData:$userID", $data);
} catch (Exception $ex) {
$data=some_database_call($userID);
$cache->Set("UserData:$userID", $data, "UserNamespace:$userID", 24*3600);
}
user_delete_function($userID);
$cache->ExpireNamespace("UserNamespace:$userID");
?>
Definition StormCacheInternals.php:551
Definition StormCacheInternals.php:473
Definition StormCacheInternals.php:486
Definition StormCacheInternals.php:499
Definition StormCacheInternals.php:460
Definition StormCacheInternals.php:512