douzen3516 2016-11-02 13:28
浏览 191
已采纳

Prestashop缓存数据

We have created some custom pages, where we are forced to run a lot of sql statements. The data can be cached for a couple of days or untill we clear the cache.

My prolem is it don't store the cache. Everytime the funciton Cache::isStored returns false.

$cache_id = "test";
if (Cache::isStored($cache_id)) {
   /// Query data
   Cache::store($cache_id, $data);
}

$data = Cache::retrieve($cache_id);
echo json_encode($data);

I think i'm using the cache function wrong. But I can't seem to find any documentation for it.

Can anybody help either with documentation or some example code

  • 写回答

1条回答 默认 最新

  • dryl34156 2016-11-02 19:06
    关注

    Cache::store() and Cache::retrieve() will store data 'locally' in a class static variable which means data is lost next time you refresh a page.

    Caching data this way is useful if you have multiple modules requesting same data in one request but you need data to refresh everytime you refresh a page.

    One example of this is the price calculation of a product. Lets say you have on home page a module which displays few products from each category, a module for new products, a module for best sellers and each shop has a cart block module.

    For each product shown the modules will call price calculation method so it's displayed to customers and because some products can be the same in multiple modules, this is a way to cache prices 'locally' or for one request.

    To cache data beyond one request you should use

    $cache = Cache::getInstance();
    $cache->set($key, $data, $ttl);
    $cache->get($key);
    $cache->exists($key);
    

    Set $ttl to time in seconds for how long you want the data to persist or by default its 0 so it stays in cache until you manually clear it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数