File cache for images

G

Guest

I'm looking for a way to cache some modified images as files (in a python
program ofcourse). The scenario would look like this:

getmodifiedimage(filename):
is it in cache?
is the cache up to date (not older than file)
if not, modify filename and store in cache (keyed by filename +
modification)
return modified image


I'm looking for prior-art before inventing it myself :)
 
F

Fredrik Lundh

I'm looking for a way to cache some modified images as files (in a python
program ofcourse). The scenario would look like this:

getmodifiedimage(filename):
is it in cache?
is the cache up to date (not older than file)
if not, modify filename and store in cache
(keyed by filename + modification)
return modified image

I'm looking for prior-art before inventing it myself :)

I'm not sure what "modify filename" means, but a straightforward
solution is not that much longer than your pseudocode:

def getmodifiedimage(filename, cache={}):
try:
image, mtime = cache[filename]
if mtime < os.path.getmtime(filename):
raise KeyError
except KeyError:
image = open(filename, "rb").read() # or something
cache[filename] = image, os.path.getmtime(filename)
return image

</F>
 
G

Guest

I'm looking for a way to cache some modified images as files (in a python
program ofcourse). The scenario would look like this:

getmodifiedimage(filename):
is it in cache?
is the cache up to date (not older than file)
if not, modify filename and store in cache (keyed by filename +
modification)
return modified image


I'm looking for prior-art before inventing it myself :)

I've read Fredrik Lundh answer on Google Groups, since it didn't appear on
my news server.

I dont want to cache file in memory (a dict in your ex.), access to the file
is cheap. I want to modify the image (say create a thumbnail, or watermark
it) and cache the thumbnail in a file.

.... but anyway it should be easy to implement really... It's just I've had a
fealing I miss something... well maybe I miss the implementation of
removing least recently used files in the cache, when the cache becomes
full (as configured).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top