仅保留最近 max_events 条事件(内存与文件同步),控制存储增长。
(self, max_events)
| 45 | return items |
| 46 | |
| 47 | def trim(self, max_events): |
| 48 | """仅保留最近 max_events 条事件(内存与文件同步),控制存储增长。""" |
| 49 | with self._lock: |
| 50 | self._buffer = self._buffer[-max_events:] |
| 51 | if self.path: |
| 52 | with open(self.path, "w", encoding="utf-8") as f: |
| 53 | for rec in self._buffer: |
| 54 | f.write(json.dumps(rec, ensure_ascii=False) + "\n") |
| 55 | return len(self._buffer) |
| 56 | |
| 57 | @staticmethod |
| 58 | def read_file(path): |
no outgoing calls