Neos\Cache\Backend\PdoBackend::set PHP 메소드

set() 공개 메소드

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 liftime.
리턴 void
    public function set($entryIdentifier, $data, array $tags = [], $lifetime = null)
    {
        $this->connect();
        if (!$this->cache instanceof FrontendInterface) {
            throw new Exception('No cache frontend has been set yet via setCache().', 1259515600);
        }
        if (!is_string($data)) {
            throw new InvalidDataException('The specified data is of type "' . gettype($data) . '" but a string is expected.', 1259515601);
        }
        $this->remove($entryIdentifier);
        $lifetime = $lifetime === null ? $this->defaultLifetime : $lifetime;
        // Convert binary data into hexadecimal representation,
        // because it is not allowed to store null bytes in PostgreSQL.
        if ($this->pdoDriver === 'pgsql') {
            $data = bin2hex($data);
        }
        $statementHandle = $this->databaseHandle->prepare('INSERT INTO "cache" ("identifier", "context", "cache", "created", "lifetime", "content") VALUES (?, ?, ?, ?, ?, ?)');
        $result = $statementHandle->execute([$entryIdentifier, md5($this->environmentConfiguration->getApplicationIdentifier()), $this->cacheIdentifier, time(), $lifetime, $data]);
        if ($result === false) {
            throw new Exception('The cache entry "' . $entryIdentifier . '" could not be written.', 1259530791);
        }
        $statementHandle = $this->databaseHandle->prepare('INSERT INTO "tags" ("identifier", "context", "cache", "tag") VALUES (?, ?, ?, ?)');
        foreach ($tags as $tag) {
            $result = $statementHandle->execute([$entryIdentifier, md5($this->environmentConfiguration->getApplicationIdentifier()), $this->cacheIdentifier, $tag]);
            if ($result === false) {
                throw new Exception('The tag "' . $tag . ' for cache entry "' . $entryIdentifier . '" could not be written.', 1259530751);
            }
        }
    }

Usage Example

 /**
  * @test
  * @expectedException \Neos\Cache\Exception
  */
 public function setThrowsExceptionIfNoFrontEndHasBeenSet()
 {
     $backend = new PdoBackend(new EnvironmentConfiguration('SomeApplication Testing', '/some/path', PHP_MAXPATHLEN));
     $data = 'Some data';
     $identifier = 'MyIdentifier';
     $backend->set($identifier, $data);
 }