在 Django 中处理临时图像的最佳方式?
我正在开发一个 Django 项目,我需要在其中提供在线生成的临时图像。 会议应该是匿名的; 任何人都应该能够使用该服务。 当会话过期或关闭时,图像应该被销毁。
但是,我不知道最好的方法是什么。 例如,我可以使用基于文件的会话,只需设置要在会话文件夹中生成的图像,它们将(或至少应该)随会话一起销毁。 我想我可以对数据库会话做类似的事情,也许将图像保存在数据库中,或者在会话结束时删除它们,但是,基于文件的解决方案对我来说听起来更可靠。
这是一个好的解决方案,还是有更可靠的替代方案?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将根据会话密钥的哈希值命名临时图像,然后创建一个管理命令:
由于没有故障安全方法可以知道会话是否已“关闭”,因此您应该使用
cleanup< /code> 第一个管理命令 - 要么在此命令之前,要么您可以使用
call_command()
函数使其作为新命令的一部分隐式运行。I'd name the temporary images based on a hash of the session key and then create a management command that:
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 usingcall_command()
function.