public set ( string $entryIdentifier, string $data, array $tags = [], integer $lifetime = null ) : void | ||
$entryIdentifier | string | An identifier for this specific cache entry |
$data | string | The data to be stored |
$tags | array | Tags to associate with this cache entry |
$lifetime | integer | Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited liftime. |
return | void |
public function set($entryIdentifier, $data, array $tags = [], $lifetime = null)
{
if (!$this->cache instanceof FrontendInterface) {
throw new Exception('No cache frontend has been set yet via setCache().', 1232986818);
}
if (!is_string($data)) {
throw new InvalidDataException('The specified data is of type "' . gettype($data) . '" but a string is expected.', 1232986825);
}
$tags[] = '%APCBE%' . $this->cacheIdentifier;
$expiration = $lifetime !== null ? $lifetime : $this->defaultLifetime;
$success = apc_store($this->identifierPrefix . 'entry_' . $entryIdentifier, $data, $expiration);
if ($success === true) {
$this->removeIdentifierFromAllTags($entryIdentifier);
$this->addIdentifierToTags($entryIdentifier, $tags);
} else {
throw new Exception('Could not set value.', 1232986877);
}
}
/** * @test */ public function flushRemovesOnlyOwnEntries() { $thisCache = $this->createMock(FrontendInterface::class); $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache')); $thisBackend = new ApcBackend($this->getEnvironmentConfiguration(), []); $thisBackend->setCache($thisCache); $thatCache = $this->createMock(FrontendInterface::class); $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache')); $thatBackend = new ApcBackend($this->getEnvironmentConfiguration(), []); $thatBackend->setCache($thatCache); $thisBackend->set('thisEntry', 'Hello'); $thatBackend->set('thatEntry', 'World!'); $thatBackend->flush(); $this->assertEquals('Hello', $thisBackend->get('thisEntry')); $this->assertFalse($thatBackend->has('thatEntry')); }