Neos\Cache\Backend\MemcachedBackend::set PHP Method

set() public method

Saves data in the cache.
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 lifetime.
return void
    public function set($entryIdentifier, $data, array $tags = [], $lifetime = null)
    {
        if (strlen($this->identifierPrefix . $entryIdentifier) > 250) {
            throw new \InvalidArgumentException('Could not set value. Key more than 250 characters (' . $this->identifierPrefix . $entryIdentifier . ').', 1232969508);
        }
        if (!$this->cache instanceof FrontendInterface) {
            throw new Exception('No cache frontend has been set yet via setCache().', 1207149215);
        }
        if (!is_string($data)) {
            throw new InvalidDataException('The specified data is of type "' . gettype($data) . '" but a string is expected.', 1207149231);
        }
        $tags[] = '%MEMCACHEBE%' . $this->cacheIdentifier;
        $expiration = $lifetime !== null ? $lifetime : $this->defaultLifetime;
        // Memcache considers values over 2592000 sec (30 days) as UNIX timestamp
        // thus $expiration should be converted from lifetime to UNIX timestamp
        if ($expiration > 2592000) {
            $expiration += time();
        }
        try {
            if (strlen($data) > self::MAX_BUCKET_SIZE) {
                $data = str_split($data, self::MAX_BUCKET_SIZE - 1024);
                $success = true;
                $chunkNumber = 1;
                foreach ($data as $chunk) {
                    $success = $success && $this->setItem($this->identifierPrefix . $entryIdentifier . '_chunk_' . $chunkNumber, $chunk, $expiration);
                    $chunkNumber++;
                }
                $success = $success && $this->setItem($this->identifierPrefix . $entryIdentifier, 'Flow*chunked:' . $chunkNumber, $expiration);
            } else {
                $success = $this->setItem($this->identifierPrefix . $entryIdentifier, $data, $expiration);
            }
            if ($success === true) {
                $this->removeIdentifierFromAllTags($entryIdentifier);
                $this->addIdentifierToTags($entryIdentifier, $tags);
            } else {
                throw new Exception('Could not set value on memcache server.', 1275830266);
            }
        } catch (\Exception $exception) {
            throw new Exception('Could not set value. ' . $exception->getMessage(), 1207208100);
        }
    }

Usage Example

 /**
  * @test
  */
 public function flushRemovesOnlyOwnEntries()
 {
     $backendOptions = ['servers' => ['localhost:11211']];
     $thisCache = $this->getMockBuilder(AbstractFrontend::class)->disableOriginalConstructor()->getMock();
     $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
     $thisBackend = new MemcachedBackend($this->getEnvironmentConfiguration(), $backendOptions);
     $thisBackend->setCache($thisCache);
     $thatCache = $this->getMockBuilder(AbstractFrontend::class)->disableOriginalConstructor()->getMock();
     $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
     $thatBackend = new MemcachedBackend($this->getEnvironmentConfiguration(), $backendOptions);
     $thatBackend->setCache($thatCache);
     $thisBackend->set('thisEntry', 'Hello');
     $thatBackend->set('thatEntry', 'World!');
     $thatBackend->flush();
     $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
     $this->assertFalse($thatBackend->has('thatEntry'));
 }