18 WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
18 WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
19 WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
19 WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
20 FOR A PARTICULAR PURPOSE. |
20 FOR A PARTICULAR PURPOSE. |
21 """ |
21 """ |
22 |
22 |
23 from mx.DateTime import now, DateTimeDelta |
23 from datetime import datetime, timedelta |
24 |
24 |
25 from logilab.common.textutils import get_csv |
25 from logilab.common.textutils import get_csv |
26 from rql.nodes import Relation, VariableRef, Constant, Function |
26 from rql.nodes import Relation, VariableRef, Constant, Function |
27 |
27 |
28 import ldap |
28 import ldap |
51 } |
51 } |
52 |
52 |
53 class TimedCache(dict): |
53 class TimedCache(dict): |
54 def __init__(self, ttlm, ttls=0): |
54 def __init__(self, ttlm, ttls=0): |
55 # time to live in minutes |
55 # time to live in minutes |
56 self.ttl = DateTimeDelta(0, 0, ttlm, ttls) |
56 self.ttl = timedelta(0, ttlm*60 + ttls, 0) |
57 |
57 |
58 def __setitem__(self, key, value): |
58 def __setitem__(self, key, value): |
59 dict.__setitem__(self, key, (now(), value)) |
59 dict.__setitem__(self, key, (datetime.now(), value)) |
60 |
60 |
61 def __getitem__(self, key): |
61 def __getitem__(self, key): |
62 return dict.__getitem__(self, key)[1] |
62 return dict.__getitem__(self, key)[1] |
63 |
63 |
64 def clear_expired(self): |
64 def clear_expired(self): |
65 now_ = now() |
65 now_ = datetime.now() |
66 ttl = self.ttl |
66 ttl = self.ttl |
67 for key, (timestamp, value) in self.items(): |
67 for key, (timestamp, value) in self.items(): |
68 if now_ - timestamp > ttl: |
68 if now_ - timestamp > ttl: |
69 del self[key] |
69 del self[key] |
70 |
70 |