Metaphore\Cache::cache PHP Method

cache() public method

As a third argument - instead of integer - you can pass Ttt object to define grace tll and lock ttl (both optional).
public cache ( $key, callable $cachedCallable, $ttl, callable $onNoStaleCacheCallable = null )
$cachedCallable callable
$onNoStaleCacheCallable callable
    public function cache($key, callable $cachedCallable, $ttl, callable $onNoStaleCacheCallable = null)
    {
        $value = $this->getValue($key);
        if ($value->hasResult() && !$value->isStale()) {
            return $value->getResult();
        }
        if (!$ttl instanceof Ttl) {
            $ttl = new Ttl($ttl);
        }
        $lock_acquired = $this->lockManager->acquire($key, $ttl->getLockTtl());
        if (!$lock_acquired) {
            if ($value->hasResult()) {
                // serve stale if present
                return $value->getResult();
            }
            if (!$onNoStaleCacheCallable) {
                $onNoStaleCacheCallable = $this->onNoStaleCacheCallable;
            }
            if ($onNoStaleCacheCallable !== null) {
                $event = new NoStaleCacheEvent($this, $key, $cachedCallable, $ttl);
                call_user_func($onNoStaleCacheCallable, $event);
                if ($event->hasResult()) {
                    return $event->getResult();
                }
            }
        }
        $result = call_user_func($cachedCallable);
        $this->setResult($key, $result, $ttl);
        if ($lock_acquired) {
            $this->lockManager->release($key);
        }
        return $result;
    }

Usage Example

Exemplo n.º 1
0
 public function testCallableCanReturnDifferentValueIfNotStaleContentAvailableToServe()
 {
     $cache = new Cache(new MockStore());
     $customValue = 'custom_value';
     $cache->onNoStaleCache(function (NoStaleCacheEvent $event) use($customValue) {
         $event->setResult($customValue);
     });
     $key = 'lamela';
     $value = 'Tottenham Rabona';
     $ttl = 30;
     // simulate lock (other process generating content)
     $cache->getLockManager()->acquire($key, 30);
     $result = $cache->cache($key, $this->createFunc($value), $ttl);
     $this->assertSame($customValue, $result);
 }
All Usage Examples Of Metaphore\Cache::cache