2016-02-21 105 views
0

我的項目在名爲'_sCache'的文件夾中有很多緩存文件。Php緩存文件刪除請求

而且,當我從數據庫中刪除數據時,我想要刪除此數據的緩存文件。但是有很多緩存文件。

如何找到要刪除的正確緩存文件?

回答

0

順便說一句,而不是將數據庫值存儲在文件中我會將它存儲在Redis中。

你會存儲在緩存文件數據庫實體是這樣的:

  • _sCache_product_1
  • _sCache_product_2
  • _sCache_product_200

所以_sCache_producct_1店JSON編碼產品實體的值id爲1。所以當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. 
} 

這種方法是使用ORM像學說在這裏你可以創建事件偵聽器優雅的完成,但這是另一個話題。