--- gwibber/gwui.py.cache 2009-07-13 22:51:45.000000000 -0400 +++ gwibber/gwui.py 2010-01-03 13:32:07.451519002 -0500 @@ -7,7 +7,7 @@ SegPhault (Ryan Paul) - 05/26/2007 from . import gintegration, resources, config import webkit, gtk, copy -import urllib2, hashlib, os, simplejson +import urllib2, hashlib, os, simplejson, operator from mako.template import Template from mako.lookup import TemplateLookup import Image @@ -139,6 +139,7 @@ def image_cache(url, cache_dir = IMG_CAC img_path = os.path.join(cache_dir, encoded_url + '.' + fmt).replace("\n", "") if not os.path.exists(img_path): + cleanup_cache(cache_dir) output = open(img_path, "w+") try: image_data = urllib2.urlopen(url).read() @@ -171,3 +172,31 @@ def image_cache(url, cache_dir = IMG_CAC output.close() return img_path + +def cleanup_cache(cache_dir = IMG_CACHE_DIR): + + files = os.listdir(cache_dir) + cache_files = [] + max_cache_size = 2000000 # XXX Get this as a configuration option + target_cache_size = max_cache_size - (max_cache_size * .1) + cache_size = 0 + + for i in files: + the_filename = os.path.join(cache_dir, i) + file_size = os.path.getsize(the_filename) + # Even if something mounted noatime, we get mtime here + file_atime = os.path.getatime(the_filename) + cache_files.append((the_filename, file_size, file_atime)) + cache_size = cache_size + file_size + + if cache_size > max_cache_size: + + # Start removing the oldest files first + cache_files.sort(key=operator.itemgetter(2)) + for i in cache_files: + os.unlink(i[0]) + cache_size = cache_size - i[1] + if cache_size <= target_cache_size: + break + + return