예제 #1
0
파일: cache.php 프로젝트: mage2pro/core
/**
 * 2016-07-18
 * 2016-10-28
 * Добавил дополнительный уровень кэширования: в оперативной памяти.
 * Также позволил в качестве $key передавать массив.
 *
 * 2016-11-01
 * При вызове @see df_cache_get_simple синтаксис use для параметра $f использовать безопасно,
 * в отличие от @see dfc() и @see dfcf(), потому что ключ кэширования передаётся параметром $key.
 *
 * @param string|string[] $key
 * @param callable $f
 * @param mixed[] ...$arguments [optional]
 * @return mixed
 */
function df_cache_get_simple($key, callable $f, ...$arguments)
{
    return dfcf(function ($key) use($f, $arguments) {
        /** @var string|bool $resultS */
        $resultS = df_cache_load($key);
        /** @var mixed $result */
        $result = null;
        if (false !== $resultS) {
            /** @var array(string => mixed) $result */
            $result = df_unserialize_simple($resultS);
        }
        /**
         * 2016-10-28
         * json_encode(null) возвращает строку 'null',
         * а json_decode('null') возвращает null.
         * Поэтому если $resultS равно строке 'null',
         * то нам не надо вызывать функцию: она уже вызывалась,
         * и (кэшированным) результатом этого вызова было значение null.
         */
        if (null === $result && 'null' !== $resultS) {
            $result = call_user_func_array($f, $arguments);
            df_cache_save(df_serialize_simple($result), $key);
        }
        return $result;
    }, [!is_array($key) ? $key : dfa_hashm($key)]);
}
예제 #2
0
파일: O.php 프로젝트: mage2pro/core
 /**
  * @param string $propertyName
  * @param string $cacheKey
  * @return void
  */
 private function cacheSaveProperty($propertyName, $cacheKey)
 {
     $cacheKey = $cacheKey . $propertyName;
     /** @var mixed $propertyValue */
     $propertyValue = $this->{$propertyName};
     /** @var string|bool $propertyValueSerialized */
     $propertyValueSerialized = isset($this->_cachedPropertiesSimpleMap[$propertyName]) ? df_serialize_simple($propertyValue) : df_serialize($propertyValue);
     if ($propertyValueSerialized) {
         df_cache_save($data = $propertyValueSerialized, $id = $cacheKey, $tags = df_array($this->cacheTags()), $lifeTime = $this->cacheLifetime());
     }
 }