Example #1
0
/**
 * 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)]);
}
Example #2
0
File: O.php Project: mage2pro/core
 /**
  * @param string $propertyName
  * @param string $cacheKey
  * @return void
  */
 private function cacheLoadProperty($propertyName, $cacheKey)
 {
     $cacheKey = $cacheKey . $propertyName;
     /** @var string|bool $propertyValueSerialized */
     $propertyValueSerialized = df_cache_load($cacheKey);
     if ($propertyValueSerialized) {
         /** @var mixed $propertyValue */
         /**
          * Обратите внимание,
          * что @see json_decode() в случае невозможности деколирования возвращает NULL,
          * а @see unserialize в случае невозможности деколирования возвращает FALSE.
          */
         $propertyValue = isset($this->_cachedPropertiesSimpleMap[$propertyName]) ? df_unserialize_simple($propertyValueSerialized) : df_ftn(df_unserialize($propertyValueSerialized));
         if (!is_null($propertyValue)) {
             $this->_cachedPropertiesLoaded[$propertyName] = true;
             $this->{$propertyName} = $propertyValue;
         }
     }
 }