Pimcore\Cache::storeToCache PHP Method

storeToCache() protected static method

Write's an item to the cache // don't use the logger inside here
protected static storeToCache ( $data, $key, array $tags = [], null $lifetime = null, boolean $force = false ) : boolean | void
$data
$key
$tags array
$lifetime null
$force boolean
return boolean | void
    protected static function storeToCache($data, $key, $tags = [], $lifetime = null, $force = false)
    {
        if (!self::$enabled) {
            return;
        }
        // don't put anything into the cache, when cache is cleared
        if (in_array("__CLEAR_ALL__", self::$clearedTagsStack) && !$force) {
            return;
        }
        // do not cache hardlink-wrappers
        if ($data instanceof Document\Hardlink\Wrapper\WrapperInterface) {
            return;
        }
        // get cache instance
        if ($cache = self::getInstance()) {
            //if ($lifetime !== null) {
            //    $cache->setLifetime($lifetime);
            //}
            if ($data instanceof Element\ElementInterface) {
                // check for currupt data
                if ($data->getId() < 1) {
                    return;
                }
                if (isset($data->_fulldump)) {
                    unset($data->_fulldump);
                }
                // get dependencies for this element
                $tags = $data->getCacheTags($tags);
                $type = get_class($data);
                Logger::debug("prepared " . $type . " " . $data->getId() . " for data cache with tags: " . implode(",", $tags));
            }
            // check for cleared tags, only item which are not cleared within the same session are stored to the cache
            if (is_array($tags)) {
                foreach ($tags as $t) {
                    if (in_array($t, self::$clearedTagsStack)) {
                        Logger::debug("Aborted caching for key: " . $key . " because it is in the clear stack");
                        return;
                    }
                }
            } else {
                $tags = [];
            }
            // always add the key as tag
            $tags[] = $key;
            // array_values() because the tags from \Element_Interface and some others are associative eg. array("object_123" => "object_123")
            $tags = array_values($tags);
            if (is_object($data) && isset($data->____pimcore_cache_item__)) {
                unset($data->____pimcore_cache_item__);
            }
            $key = self::$cachePrefix . $key;
            if ($lifetime === null) {
                $lifetime = false;
                // set to false otherwise the lifetime stays at null (\Zend_Cache_Backend::getLifetime())
            }
            $success = $cache->save($data, $key, $tags, $lifetime);
            if ($success !== true) {
                Logger::error("Failed to add entry {$key} to the cache, item-size was " . formatBytes(strlen(serialize($data))));
            }
            Logger::debug("Added " . $key . " to cache");
            return $success;
        }
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Adds a Pimcore Object/Asset/Document to the cache
  *
  * @param $element
  */
 public static function loadElementToCache($element)
 {
     $cacheKey = Element\Service::getElementType($element) . "_" . $element->getId();
     Cache::storeToCache($element, $cacheKey, [], null, null, true);
 }