コード例 #1
0
ファイル: Operation.php プロジェクト: drcts/closure-cache
 /**
  * @param  null|array $arguments Must be serializable.
  * @return void
  */
 public function warm($arguments = null)
 {
     $callable = $this->callable;
     if (is_array($arguments)) {
         $value = call_user_func_array($callable, $arguments);
     } else {
         $value = $callable();
     }
     $cacheKey = Cache::makeCacheKey($this->name, $arguments);
     $this->storage->addItem($cacheKey, serialize($value));
 }
コード例 #2
0
ファイル: CacheTest.php プロジェクト: drcts/closure-cache
 public function testExpireWithCallableArgumentsOnlyExpiresCacheWithSpecificArguments()
 {
     $executedWithArgumentsCount = array();
     $Cache = new Cache(array('adapter' => 'memory'));
     $Cache->define('slowDynamicOperation', function ($arg = null) use(&$executedWithArgumentsCount) {
         $idx = serialize(array($arg));
         if (!isset($executedWithArgumentsCount[$idx])) {
             $executedWithArgumentsCount[$idx] = 0;
         }
         $executedWithArgumentsCount[$idx]++;
         return "dskljhelwih";
     });
     $Cache->warm('slowDynamicOperation', array(null));
     $Cache->warm('slowDynamicOperation', array('a'));
     $Cache->warm('slowDynamicOperation', array(1));
     $result = $Cache->expire('slowDynamicOperation', array('a'));
     $this->assertTrue($result);
     $Cache->warm('slowDynamicOperation', array(null));
     // This should come from cache.
     $Cache->warm('slowDynamicOperation', array('a'));
     // This should run again.
     $Cache->warm('slowDynamicOperation', array(1));
     // This should come from cache.
     $this->assertEquals(1, $executedWithArgumentsCount[serialize(array(null))]);
     $this->assertEquals(2, $executedWithArgumentsCount[serialize(array('a'))]);
     $this->assertEquals(1, $executedWithArgumentsCount[serialize(array(1))]);
 }