我将django会话存储在磁盘上。我有一个特殊的情况,我需要遍历每个会话并删除某些键,然后保留其余的键。因此,清除整个缓存不是一种选择。当会话在db中时,我能够迭代各个会话。但是有了会话存储,我就卡住了。我需要做的类似于:
sessions=sessionstore.all()
for session in sessions:
session.pop('key1')
session.pop('key2')
if session lastmodified before some time:
del session发布于 2013-01-03 01:32:45
看看django.contrib.sessions.backends.file中的clear_expired方法是如何完成迭代的:
@classmethod
def clear_expired(cls):
storage_path = cls._get_storage_path()
file_prefix = settings.SESSION_COOKIE_NAME
for session_file in os.listdir(storage_path):
if not session_file.startswith(file_prefix):
continue
session_key = session_file[len(file_prefix):]
session = cls(session_key)
# When an expired session is loaded, its file is removed, and a
# new file is immediately created. Prevent this by disabling
# the create() method.
session.create = lambda: None
session.load()https://stackoverflow.com/questions/14125281
复制相似问题