Php缓存文件删除请求

时间:2016-02-21 14:00:01

标签: php caching

我的项目在名为' _sCache'的文件夹中有很多缓存文件。

并且,当我从数据库中删除数据时,我想删除此数据的缓存文件。但是有很多缓存文件。

如何找到要删除的正确缓存文件?

1 个答案:

答案 0 :(得分:0)

顺便说一句,而不是将数据库值存储在文件中,我将它存储在Redis中。

您可以将数据库实体存储在缓存的文件中,如下所示:

  • _sCache_product_1
  • _sCache_product_2
  • _sCache_product_200

所以_sCache_producct_1存储id为1的产品实体的json编码值。因此,当删除ID为1的产品时,我知道我必须只删除_sCache_product_1文件,而不是其他任何内容。你的getProduct函数类似于:

public function getProduct($id)
{
   // First check the caching system if you have product with id
   if ($product = $this->cache->readCache($id)) {
      return json_decode($product, true);
   }

   // Product doesn't exist in cache fetch fetch it from the database
   // then store it in the caching system.
}

使用像Doctrine这样的ORM可以优雅地完成这种方法,您可以在其中创建事件监听器,但这是另一个主题。