views:

95

answers:

1

I'm developing a Django project where I need to serve temporary images, which are generated online. The sessions should be anonymous; anyone should be able to use the service. The images should be destroyed when the session expires or closes.

I don't know, however, what's the best approach. For instance, I could use file-based sessions and just set the images to be generated at the session folder, and they would (or at least should) be destroyed with the session. I suppose I could do something similar with database sessions, maybe saving the images in the database or just removing them when the sessions ends, however, the file-based solution sounds more reliable to me.

Is it a good solution, or are there more solid alternatives?

A: 

I'd name the temporary images based on a hash of the session key and then create a management command that:

  1. makes a list containing potential temp filename hashes for all the current sessions.
  2. grabs a list of all the current filenames in your temporary directory
  3. deletes filenames which don't have a matching entry in the hash list

Since there's no failsafe way to know if a session has "closed", you should use the cleanup management command first - either before this one, or you could make it run implicitly as part of this new command by using call_command() function.

SmileyChris