Esempio n. 1
0
 /**
  * Flush all cached resources
  */
 public function flushAll()
 {
     $this->cache->flush();
 }
/**
 * You must run `composer install` in order to generate autoloader for this example
 */
require __DIR__ . '/../vendor/autoload.php';
use Memcached;
use Memcached\Wrapper;
try {
    // Server pool
    $servers = array(array('127.0.0.1', 11211));
    // Set TTL for new keys
    $ttl = time() + Wrapper::EXTENDED_TTL * 2;
    // Set client
    $client = isset($_GET['client']) && $_GET['client'] == 'wrapper' ? 'wrapper' : 'memcached';
    if ($client == 'wrapper') {
        // Initialize our Memcached wrapper and simulate caching of a large SQL query
        $wrapper = new Wrapper('wrapper', $servers);
        $wrapper->toggleStorage();
        $result = null;
        if (!$wrapper->get('wrapper', $result)) {
            error_log('Wrapper database hit: ' . date('Y-m-d H:i:s'));
            // Some query that takes 5 seconds
            sleep(5);
            $wrapper->set('wrapper', 'value', $ttl);
        }
    } else {
        // Initialize regular Memcached instance and simulate caching of a large SQL query
        $memcached = new Memcached('memcached');
        $memcached->addServers($servers);
        if (!$memcached->get('memcached')) {
            error_log('Memcached database hit: ' . date('Y-m-d H:i:s'));
            // Some query that takes 5 seconds
 * @author Aleksey Korzun <*****@*****.**>
 * @link https://github.com/AlekseyKorzun/memcached-wrapper-php
 * @link http://www.alekseykorzun.com
 */
/**
 * You must run `composer install` in order to generate autoloader for this example
 */
require __DIR__ . '/../vendor/autoload.php';
use stdClass;
use Memcached\Wrapper as Cache;
// Key to use for integrity tests
define('KEY', 'key');
// List of pools
$servers = array(array('127.0.0.1', 11211, 10), array('127.0.0.1', 11211, 20));
try {
    $cache = new Cache('pool', $servers);
    // Attempt to retrieve previously cached result from pool (run this twice)
    if (!$cache->get(KEY, $resource)) {
        print "Key was not found in our cache pool!\n";
        // Create test resource
        $resource = new stdClass();
        $resource->name = 'Test';
        // If nothing was found during our cache look up, save resource to cache pool
        if ($cache->set(KEY, $resource)) {
            print "Stored resource in cache pool!\n";
        } else {
            print "Failed to store resource in cache pool!\n";
        }
    } else {
        print "Key was found in our cache pool!\n";
        // Let's get fancy
 * @license MIT
 * @author Aleksey Korzun <*****@*****.**>
 * @link https://github.com/AlekseyKorzun/memcached-wrapper-php
 * @link http://www.alekseykorzun.com
 */
/**
 * You must run `composer install` in order to generate autoloader for this example
 */
require __DIR__ . '/../vendor/autoload.php';
use stdClass;
use Memcached;
use Memcached\Wrapper;
try {
    // Server pool
    $servers = array(array('127.0.0.1', 11211));
    $wrapper = new Wrapper('wrapper', $servers);
    // Turn off local storage, compression and race condition protection because
    // append/prepend and increment/decrement operations do not support it
    $wrapper->toggleAll();
    // Append operation
    $wrapper->set('key', '1');
    $wrapper->append('key', '00');
    $result = null;
    $wrapper->get('key', $result);
    // Result is now 100
    print "Should be 100: {$result} \n";
    // Increment operation
    $wrapper->increment('key', 50);
    $result = null;
    $wrapper->get('key', $result);
    // Result is now 150