From a4509089e1f74c1df6beb0c662e0d977001740c8 Mon Sep 17 00:00:00 2001 From: neko13 Date: Sun, 18 Jun 2017 20:33:29 -0400 Subject: [PATCH] add evict_expired method --- py3cache/PyInMem.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/py3cache/PyInMem.py b/py3cache/PyInMem.py index f73dec2..5777d5b 100644 --- a/py3cache/PyInMem.py +++ b/py3cache/PyInMem.py @@ -4,6 +4,7 @@ Python Mem Cache with cache evict callback """ import configparser +import sys import os from time import time @@ -67,6 +68,16 @@ class PyInMem: if self.callback is not None: self.callback('evict', region, key) + def evict_expired(self, region): + if sys.version < '3': + items = self.cachetime[region].items() + else: + items = self.cachetime[region].copy().items() + + for (key, ctime) in items: + if (ctime.expire > 0) and (time() - ctime.begin > ctime.expire): + self.evict(region, key) + def _clear(self, region): self.regions[region].clear() self.cachetime[region].clear() -- Gitee