简体   繁体   中英

Php Cached File Delete Request

My project has a lot of cached file in a folder called '_sCache'.

And, When I delete a data from database, I want to delete a cached file of this data. But there are a lot of cached file.

How can I find the right cached file to delete?

Btw instead of storing database values in files I'd store it in Redis.

You would store database entities in cached files like this:

  • _sCache_product_1
  • _sCache_product_2
  • _sCache_product_200

so _sCache_producct_1 stores json encoded values of product entity with id 1. So when product with ID 1 is deleted I'll know that I'll have to delete only _sCache_product_1 file and nothing else. Your getProduct function would be something like:

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.
}

This approach is elegantly done using ORM like Doctrine where you can create event listeners, but that's another topic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM