You are here:  » Image caching and deleted products


Image caching and deleted products

Submitted by Retro135 on Sun, 2017-02-05 08:05 in

Hi David,
Is there an automatic way to remove images from the cache when products are deleted from feed?

Submitted by support on Mon, 2017-02-06 12:05

Hi,

Here is a scripts/imageCachePurge.php script to remove image cache files that have not been refreshed in 30 days (adjust at line 6 as required)...

<?php
  set_time_limit
(0);
  
$cacheDir "../cache/";
  
$days 30;
  
$age = ($days 86400);
  
$now time();
  
$count 0;
  if (
$dh opendir($cacheDir))
  {
    while ((
$file readdir($dh)) !== false)
    {
      
$filename $cacheDir.$file;
      if (
filemtime($filename) < ($now-$age))
      {
        
unlink($filename);
        
$count++;
      }
    }
    
closedir($dh);
  }
  exit();
?>

This could be set-up to run, say weekly as a CRON job e.g.

cd /path/to/pricetapestry/scripts/;/usr/bin/php imageCachePurge.php

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by Retro135 on Mon, 2017-02-06 15:40

Perfect, thank you!

Submitted by Retro135 on Tue, 2017-02-07 15:21

I realized not so good as some merchants don't update very frequently, or in the case of a couple small merchants, never. Not sure what to ask for.

Submitted by support on Tue, 2017-02-07 15:49

Hi,

If you're using the default $imageCacheTime of 604800;

  $imageCacheTime = 604800; // cache period in seconds, set to zero for proxy only mode

...which is 7 days, running a purge of cache files not updated in 30 days should keep the cache directly low as the script will only be deleting cached images that have _not_ been requested in the last 30 days. That is potentially a better resource usage scenario that holding on to images just because they are in the database but have not been requested in 30 days; let me know if you are still not sure of course...

Cheers,
David.
--
PriceTapestry.com

Submitted by Retro135 on Tue, 2017-02-07 16:00

Yes, using default time. Doe this mean I don't need to use the script as cache is automatically updated every 7 days?

Submitted by support on Tue, 2017-02-07 16:10

Hi,

It's not so much updated - but images will be _refreshed_ every 7 days, so that behaviour, in conjunction with running the purge script every 30 days will keep the cache/ folder size down to a minimum as once products have been deleted from a merchant feed, they will not be re-visited on the site, the cache file timestamp will not be updated, so imageCachePurge.php will delete the file after 30 days...

Cheers,
David.
--
PriceTapestry.com

Submitted by Retro135 on Wed, 2017-02-08 07:40

Thank you very much for the clarification!