瀏覽代碼

Added a sessions' cleaning method

Roland Haroutiounian 7 年之前
父節點
當前提交
1b88938825
共有 1 個檔案被更改,包括 18 行新增5 行删除
  1. 18
    5
      lodel/session/lodel_filesystem_session/lodel_filesystem_session_interface.py

+ 18
- 5
lodel/session/lodel_filesystem_session/lodel_filesystem_session_interface.py 查看文件

@@ -5,6 +5,7 @@ from lodel.session.lodel_filesystem_session.lodel_filesystem_session import Lode
5 5
 from contextlib import suppress
6 6
 import binascii
7 7
 
8
+
8 9
 class LodelFileSystemSessionInterface(SessionInterface):
9 10
     
10 11
     __sessions = dict()
@@ -16,9 +17,10 @@ class LodelFileSystemSessionInterface(SessionInterface):
16 17
     def open_session(self, app, request):
17 18
         self.filename_template = app.config['lodel.filesystem_sessions']['filename_template']
18 19
         self.session_tokensize = int(app.config['lodel.sessions']['tokensize'])
20
+        self.clean_sessions()
19 21
         sid = request.cookies.get(app.session_cookie_name) or self.generate_token() #or '{}-{}'.format(uuid1(), os.getpid())
20
-        if LodelFileSystemSessionInterface.__sessions.get(sid, None) is None:
21
-            LodelFileSystemSessionInterface.__sessions[sid] = self.generate_file_path(sid, self.filename_template)
22
+        if self.__class__.__sessions.get(sid, None) is None:
23
+            self.__class__.__sessions[sid] = self.generate_file_path(sid, self.filename_template)
22 24
         return LodelFileSystemSession(self.directory, sid)
23 25
     
24 26
     def save_session(self, app, session, response):
@@ -27,7 +29,7 @@ class LodelFileSystemSessionInterface(SessionInterface):
27 29
         if not session:
28 30
             with suppress(FileNotFoundError):
29 31
                 os.unlink(session.path)
30
-                del(LodelFileSystemSessionInterface.__sessions[session.sid])
32
+                del(self.__class__.__sessions[session.sid])
31 33
             response.delete_cookie(app.session_cookie_name, domain=domain)
32 34
             return
33 35
         
@@ -71,6 +73,17 @@ class LodelFileSystemSessionInterface(SessionInterface):
71 73
     #            bit length, not chars length.
72 74
     def generate_token(self):
73 75
         token = binascii.hexlify(os.urandom(self.session_tokensize))
74
-        if LodelFileSystemSessionInterface.__sessions.get(token, None) is not None:
76
+        if self.__class__.__sessions.get(token, None) is not None:
75 77
             token = self.generate_token()
76
-        return token.decode('utf-8')
78
+        return token.decode('utf-8')
79
+    
80
+    ## @brief Cleans the sessions' store by deleting the expired sessions' files
81
+    # @todo add logging to the cleaning action
82
+    def clean_sessions(self):
83
+        # Unregistered files in the sessions directory
84
+        session_files_directory = os.path.abspath(self.directory)
85
+        for session_file in [file_path for file_path in os.listdir(session_files_directory) if os.path.isfile(os.path.join(session_files_directory, file_path))]:
86
+            session_file_path = os.path.join(session_files_directory, session_file)
87
+            token = self.get_token_from_filepath(session_file_path)
88
+            if token is None or self.__class__.__sessions.get(token, None) is None:
89
+                os.unlink(session_file_path)

Loading…
取消
儲存