/**
* Save cache item
*
* @param string $key
* @param mixed $value
* @param int $ttl 1 or more, up to 300 (seconds)
*
* @return bool stating if caching succeeded or not
*/
/**
* Confirms if the cache contains specified cache item.
*
* @param string $key
*
* @return bool True if item exists in the cache, false otherwise
*/
use Framework\SimpleCache\SimpleCache;
$cacheKey = "(CACHE_KEY)";
$hasKey = SimpleCache::init(SimpleCache::MEMCACHED)->has($cacheKey);
/**
* Fetches cache item.
*
* @param string $key
*
* @return mixed The corresponding values found in the cache, returning null if no value is present in the cache.
*/
use Framework\SimpleCache\SimpleCache;
$cacheKey = "(CACHE_KEY)";
$cacheData = SimpleCache::init(SimpleCache::MEMCACHED)->get($cacheKey);
/**
* Finding the corresponding value in the cache,
* setting the value if it is not present in the cache, and returning the value stored in the cache.
*
* @param string $key
* @param callable $callback
* @param int $ttl 1 or more, up to 300 (seconds)
*
* @return mixed The corresponding values found in the cache, returning values from the cache.
* If no value is present in the cache, it returns the result of the lambda function provided as the second parameter.
*/
/**
* Removes cache item.
*
* @param string $key
*
* @return mixed True if the items were successfully removed, false otherwise
*/
use Framework\SimpleCache\SimpleCache;
$cacheKey = "(CACHE_KEY)";
$cacheData = SimpleCache::init(SimpleCache::MEMCACHED)->delete($cacheKey);
/**
* 캐시 내 goods_benefit_config 키에 할당된 값이 있는지 확인합니다.
* set()을 하지 않았거나 ttl(만료 시간)이 지나 캐시가 삭제된 경우에는 has() 값이 false 로 반환됩니다.
*/
$cacheKey = "(CACHE_KEY)";
if (SimpleCache::init(SimpleCache::MEMCACHED)->has($cacheKey)) {
/**
* goods_benefit_config 키에 할당된 값이 있으면
* 캐시에서 값을 가져와 $goodsBenefitConfig 에 할당합니다.
*/
$goodsBenefitConfig = SimpleCache::init(SimpleCache::MEMCACHED)->get($cacheKey);
} else {
/**
* goods_benefit_config 키에 할당된 값이 없으면
* 저장을 원하는 데이터를 가져와 다시 캐시에 set()을 합니다.
*
* set()을 통해 goods_benefit_config 키에는 $goodsBenefitConfig 값이 300초 동안 저장됩니다.
*/
$goodsBenefit = \App::load('\\Component\\Goods\\GoodsBenefit');
$goodsBenefitConfig = $goodsBenefit->getConfig();
$ttl = 300;
SimpleCache::init(SimpleCache::MEMCACHED)->set($cacheKey, $goodsBenefitConfig, 300);
}
/**
* getSet 함수를 활용한 캐시 개발 가이드입니다.
* 1번째 파라미터의 $cacheKey에 대한 데이터가 캐시에 있을 경우,
* 2번째 람다 함수 내 로직이 수행되지 않고 캐시 내 저장된 데이터가 반환됩니다.
*
* 1번째 파라미터의 $cacheKey에 대한 데이터가 캐시에 없을 경우,
* 2번째 람다 함수 내 로직이 수행된 결과가 캐시 내 set 되며, set 된 결과가 반환됩니다.
*/
$cacheKey = "(CACHE_KEY)";
$ttl = 300;
$goodsBenefitConfig = SimpleCache::init(SimpleCache::MEMCACHED)->getSet(
$cacheKey,
function () {
$goodsBenefit = \App::load('\\Component\\Goods\\GoodsBenefit');
$goodsBenefitConfig = $goodsBenefit->getConfig();
return $goodsBenefitConfig;
},
$ttl
);