mosbth / cimage

Server-side image resizing and cropping on the fly with caching of generated image-files using PHP.
http://cimage.se/
MIT License
267 stars 43 forks source link

Cache dir - autodelete? #129

Open szunyi opened 8 years ago

szunyi commented 8 years ago

Hello,

I really like your script, very nice job! But i think the cache autodelete/autopurge feature would be cool! (Example: delete unused images after X days)

Thanks bye

szunyi commented 8 years ago

this can be good for this? http://php.net/manual/en/function.fileatime.php

mosbth commented 8 years ago

Yes, the function fileatime can be used to check when a cached image was last accessed.

I had some thoughts on implementing such a cleanup facility within img.php, but I choose to leave it for now. If I would implement it in img.php there might be concerns on when to do autodelete and if it could be initiated from an url, perhaps protected by a password.

For now, there is a script bin bin/cache.bash, showing how you can deal with the cache and old items. One brutal way is to execute a command like this, to remove all cached files, not accessed within the last 30 days.

find cache/ -type f -atime +30 -exec rm {} \;

You could put such a command in the crontab to make it run nightly. This implies you have the access to the server and its a unix-like server. To make it work on a hosted webserver, where you dont have access to the shell, you most likely would need a PHP-solution through the url. So there might be a usecase for this.

mosbth commented 8 years ago

Running the command bin/cache.bash could present the following output.

$ bin/cache.bash cache/                                              
# Size                                                                                                                       
Total size:       115M                                                                                                       
Number of files:  1305                                                                                                       
Number of dirs:   2                                                                                                          

# Top-5 largest files/dirs:                                                                                                  
928     /home/mos/htdocs/dbwebb.se/cimage/cache//mikael-roos_me-2.jpg_900_580_q60_co-1                                       
896     /home/mos/htdocs/dbwebb.se/cimage/cache//mikael-roos_me-2.jpg_900_580_q60_co-1.png                                   
724     /home/mos/htdocs/dbwebb.se/cimage/cache//_._bth-sommar-hav.jpg_630_630_q60_co-1                                      
692     /home/mos/htdocs/dbwebb.se/cimage/cache//_._bth-sommar-hav.jpg_630_630_q60_co-1.png                                  
668     /home/mos/htdocs/dbwebb.se/cimage/cache//snapshot_wonderlan-2013-alpacaliceposter7-1.jpg_613_867_q60_co-1            

# Last-5 created files:                                                                                                      
2015-11-30 15:28 /home/mos/htdocs/dbwebb.se/cimage/cache/_._bobba_webprog2.jpg_800_356_q60_co-1                              
2015-11-29 18:49 /home/mos/htdocs/dbwebb.se/cimage/cache/snapht15_webgl-cube.png_630_613_q60_co-1.png                        
2015-11-29 18:49 /home/mos/htdocs/dbwebb.se/cimage/cache/snapht15_webgl-cube.png_630_613_q60_co-1                            
2015-11-29 14:49 /home/mos/htdocs/dbwebb.se/cimage/cache/snapshot_workbench-disable-secure-auth.jpg_128_128_cf_q60_co-1      
2015-11-29 14:49 /home/mos/htdocs/dbwebb.se/cimage/cache/snapshot_wonderlan-2013-alpacaliceposter7.jpg_128_128_cf_q60_co-1   

# Last-5 accessed files:                                                                                                     
2015-12-02 11:14 snapshot_seo-ansokningssiffror-hosten.png_600_202_q60_co-1                                                  
2015-12-02 11:03 javascript-programmeringslab_01.jpg_630_426_q60_co-1                                                        
2015-12-02 10:42 snapht15_ajax-marvin.png_630_522_q60_co-1                                                                   
2015-12-02 10:42 snapht15_ajax-marvin-devtools.png_630_543_q60_co-1                                                          
2015-12-02 10:42 snapht15_ajax-af-total.png_630_355_q60_co-1                                                                 

# 5 Oldest files:                                                                                                            
2015-10-18 15:22 README.me                                                                                                   
2015-10-18 15:23 dummy_100_100                                                                                               
2015-10-18 15:24 dummy_100_100_bgcff00ff                                                                                     
2015-10-18 15:25 vimmel_hostbild2-smal-dbwebbisar.jpg_600_121_q60_co-1                                                       
2015-10-18 15:25 vimmel_hostbild2-smal-dbwebbisar.jpg_600_121_q60_co-1.png                                                   

# Files not accessed within the last 30 days                                                                                 
Number of files: 475                                                                                                         
Total file size: 42968                                                                                                       

Seems like I have some old and not used items in my cache, not to much though.

mosbth commented 8 years ago

I'll close this for now. Lets reopen it if it turns out there is a great need for a cache-cleaning solution using a PHP-script, instead of bash/crontab.

szunyi commented 8 years ago

Hello, thanks for your solution, but it seems this method is not suit for me, here is the output :)

Size

Total size: 37G Number of files: 458545 Number of dirs: 2

Top-5 largest files/dirs:

./cache.sh: line 29: /usr/bin/du: Argument list too long

Last-5 created files:

2015-12-21 09:35 cache/remote/ffb2574bf08f473e87c18b68209dc137.json 2015-12-21 09:35 cache/remote/fd3a4adffabbf3ec5bd9878c04a6b1eb.json 2015-12-21 09:35 cache/remote/f09f7e2f8b2c8fb02bf387ce01a0c126.json 2015-12-21 09:35 cache/remote/ef8071acb273b24c176f84a6cc82e24c.json 2015-12-21 09:35 cache/remote/ebd578999600d0b760cd683ba6b6d298.json

Last-5 accessed files:

2015-12-21 08:05 fc9c08a98d0bacc8f06c204a24f320fe.json 2015-12-21 08:05 _._fc9c08a98d0bacc8f06c204a24f320fe_400_450_cf_q90co-1 2015-12-21 08:05 fc9c08a98d0bacc8f06c204a24f320fe 2015-12-21 08:05 f925c8d3c6f084d37ce44540c5651c88.json 2015-12-21 08:05 ._f925c8d3c6f084d37ce44540c5651c88_400_450_cf_q90_co-1

5 Oldest files:

2015-12-10 11:56 007ea81677530c8964fae5f74e2f88db 2015-12-10 11:56 _._007ea81677530c8964fae5f74e2f88db_300_230_cf_q90co-1 2015-12-10 11:56 007ea81677530c8964fae5f74e2f88db.json 2015-12-10 11:56 0365f54ae8cb785912b92d1c5b7a0674 2015-12-10 11:56 ._0365f54ae8cb785912b92d1c5b7a0674_100_90_cf_q90_co-1

Files not accessed within the last 30 days

Number of files: 0 Total file size:

szunyi commented 8 years ago

... and the "find ./cache/ -type f -atime +14 -exec rm {} \;" is not working for me :'(

or this can happen if...

find ./cache/* -type f -atime +14 -exec rm {} \; bash: /bin/find: Argument list too long

mosbth commented 8 years ago

And your operatingsystem/version and version of bash is?

And if you run the individual commands in the bash-file, what is the error message output?

szunyi commented 8 years ago

hello,

OS: Centos 6.7, Linux version 2.6.32-573.8.1.el6.x86_64 Bash: GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)

Only this error: line 29: /usr/bin/du: Argument list too long

I think only the files number in that director is the problem :) [Number of files: 461592]

Thanks

UPDATE 2; I changed your code in line29, remove the * so the final line is; echo "$( du -s $1/ | sort -nr | head -5 )"

UPDATE; This is works for me: find ./cache/ -mtime +14 -print -exec rm {} \;