Example #1
0
 public function testDataMemcachedCache()
 {
     $memcache = $this->_prepareMemcached();
     if (!$memcache) {
         return false;
     }
     $memcache->delete('test-data');
     $frontCache = new Phalcon\Cache\Frontend\Data();
     $cache = new Phalcon\Cache\Backend\Memcache($frontCache, array('host' => '127.0.0.1', 'port' => '11211'));
     $data = array(1, 2, 3, 4, 5);
     $cache->save('test-data', $data);
     $cachedContent = $cache->get('test-data');
     $this->assertEquals($cachedContent, $data);
     $cache->save('test-data', "sure, nothing interesting");
     $cachedContent = $cache->get('test-data');
     $this->assertEquals($cachedContent, "sure, nothing interesting");
     $this->assertEquals($cache->queryKeys(), array(0 => 'test-data'));
     //Check if exists
     $this->assertTrue($cache->exists('test-data'));
     //Delete
     $this->assertTrue($cache->delete('test-data'));
 }
Example #2
0
 public function testCacheMemcachedFlush()
 {
     $frontCache = new Phalcon\Cache\Frontend\Data(array('lifetime' => 10));
     // Memcached
     $memcache = $this->_prepareMemcached();
     if (!$memcache) {
         return false;
     }
     $cache = new Phalcon\Cache\Backend\Memcache($frontCache, array('host' => '127.0.0.1', 'port' => '11211'));
     $cache->save('data', "1");
     $cache->save('data2', "2");
     $this->assertTrue($cache->flush());
     $this->assertFalse($cache->exists('data'));
     $this->assertFalse($cache->exists('data2'));
 }
<?php

//Create a None Cache
$frontCache = new Phalcon\Cache\Frontend\None();
// Create the component that will cache "Data" to a "Memcached" backend
// Memcached connection settings
$cache = new Phalcon\Cache\Backend\Memcache($frontCache, array("host" => "localhost", "port" => "11211"));
// This Frontend always return the data as it's returned by the backend
$cacheKey = 'robots_order_id.cache';
$robots = $cache->get($cacheKey);
if ($robots === null) {
    // This cache doesn't perform any expiration checking, so the data is always expired
    // Make the database call and populate the variable
    $robots = Robots::find(array("order" => "id"));
    $cache->save($cacheKey, $robots);
}
// Use $robots :)
foreach ($robots as $robot) {
    echo $robot->name, "\n";
}
<?php

// Cache data for 2 days
$frontCache = new Phalcon\Cache\Frontend\Data(array("lifetime" => 172800));
//Create the Cache setting memcached connection options
$cache = new Phalcon\Cache\Backend\Memcache($frontCache, array('host' => 'localhost', 'port' => 11211, 'persistent' => false));
//Cache arbitrary data
$cache->save('my-data', array(1, 2, 3, 4, 5));
//Get data
$data = $cache->get('my-data');