/** * Save resources in cache * * @throws \Exception * @param string[] $keys * @param bool $isRequired * @return string[] */ protected function save(array $keys, $isRequired) { $resources = array(); if ($keys) { foreach ($keys as $key => $filename) { // If we detect an URI, encode filename if (strpos($filename, 'http') === 0) { $filename = urlencode($filename); } $resource = file_get_contents($filename); if (!$resource && $isRequired) { throw new Exception('We are unable to load required resource: ' . $filename); } // Make sure opening tag is there if (strpos($resource, '<?') === false) { throw new Exception('Requested resource must have opening tags: ' . $filename); } // Append closing tag $position = strrpos($resource, '?>'); if ($position === false || trim(substr($resource, $position + 2, strlen($resource))) != '') { $resource .= "\n?>\n"; } $this->cache->set($key, $resource); $resources[$key] = $resource; } } return $resources; }
// 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 sleep(5); $memcached->set('memcached', 'value', $ttl); } } } catch (Exception $exception) { header('HTTP/1.1 500 Internal Server Error'); print $exception->getMessage();
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 $server = $cache->getServerByKey(KEY); if ($server) { print "Server key is mapped to: " . print_r($server) . "\n"; } // We retrieved resource from cache, let's make sure delete works if ($cache->delete(KEY)) { print "Deleted resource from cache!\n"; } else {
/** * 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 print "Should be 150: {$result} \n"; // Turn on local storage, compression and race condition protection for regular storage $wrapper->toggleAll(); $wrapper->set('key', new stdClass()); $result = null;