Nette\Caching\Storages\FileStorage::write PHP Method

write() public method

Writes item into the cache.
public write ( $key, $data, array $dp ) : void
$dp array
return void
    public function write($key, $data, array $dp)
    {
        $meta = [self::META_TIME => microtime()];
        if (isset($dp[Cache::EXPIRATION])) {
            if (empty($dp[Cache::SLIDING])) {
                $meta[self::META_EXPIRE] = $dp[Cache::EXPIRATION] + time();
                // absolute time
            } else {
                $meta[self::META_DELTA] = (int) $dp[Cache::EXPIRATION];
                // sliding time
            }
        }
        if (isset($dp[Cache::ITEMS])) {
            foreach ((array) $dp[Cache::ITEMS] as $item) {
                $depFile = $this->getCacheFile($item);
                $m = $this->readMetaAndLock($depFile, LOCK_SH);
                $meta[self::META_ITEMS][$depFile] = $m[self::META_TIME];
                // may be NULL
                unset($m);
            }
        }
        if (isset($dp[Cache::CALLBACKS])) {
            $meta[self::META_CALLBACKS] = $dp[Cache::CALLBACKS];
        }
        if (!isset($this->locks[$key])) {
            $this->lock($key);
            if (!isset($this->locks[$key])) {
                return;
            }
        }
        $handle = $this->locks[$key];
        unset($this->locks[$key]);
        $cacheFile = $this->getCacheFile($key);
        if (isset($dp[Cache::TAGS]) || isset($dp[Cache::PRIORITY])) {
            if (!$this->journal) {
                throw new Nette\InvalidStateException('CacheJournal has not been provided.');
            }
            $this->journal->write($cacheFile, $dp);
        }
        ftruncate($handle, 0);
        if (!is_string($data)) {
            $data = serialize($data);
            $meta[self::META_SERIALIZED] = TRUE;
        }
        $head = serialize($meta) . '?>';
        $head = '<?php //netteCache[01]' . str_pad((string) strlen($head), 6, '0', STR_PAD_LEFT) . $head;
        $headLen = strlen($head);
        do {
            if (fwrite($handle, str_repeat("", $headLen)) !== $headLen) {
                break;
            }
            if (fwrite($handle, $data) !== strlen($data)) {
                break;
            }
            fseek($handle, 0);
            if (fwrite($handle, $head) !== $headLen) {
                break;
            }
            flock($handle, LOCK_UN);
            fclose($handle);
            return;
        } while (FALSE);
        $this->delete($cacheFile, $handle);
    }