Ejemplo n.º 1
0
 /**
  * Load a file, functions like include() or require()
  *
  * @param string|string[] $filename path or uri
  * @param bool $isRequired if file is required but could not be accessed exception will be thrown
  */
 public function load($filename, $isRequired = false)
 {
     $keys = array();
     // Convert regular strings into an array
     $filenames = (array) $filename;
     foreach ($filenames as $filename) {
         $keys[md5($filename)] = $filename;
     }
     // Initialize resource array
     $resources = array();
     // Retrieve resources from cache
     if (!$this->cache->get(array_keys($keys), $resources)) {
         $resources = $this->save($keys, $isRequired);
     }
     // If did not get all of the resources back from cache
     // cache resources that we were missing
     if (count($resources) != count($keys)) {
         $saved = $this->save(array_diff_key($keys, $resources), $isRequired);
         $resources = array_merge($resources, $saved);
     }
     if ($resources) {
         foreach ($resources as $resource) {
             eval('?>' . $resource . '<?');
         }
     }
 }
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
            sleep(5);
            $memcached->set('memcached', 'value', $ttl);
        }
 * @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
        $server = $cache->getServerByKey(KEY);
        if ($server) {
Ejemplo n.º 4
0
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;
    $wrapper->get('key', $result);
    $result = get_class($result);
    // Result is now instance of new stdClass