1 """cubicweb server sources support |
1 """cubicweb server sources support |
2 |
2 |
3 :organization: Logilab |
3 :organization: Logilab |
4 :copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
4 :copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 """ |
6 """ |
7 __docformat__ = "restructuredtext en" |
7 __docformat__ = "restructuredtext en" |
8 |
8 |
9 from logging import getLogger |
9 from logging import getLogger |
10 |
10 |
|
11 from mx.DateTime import now, DateTimeDelta |
|
12 |
11 from cubicweb import set_log_methods |
13 from cubicweb import set_log_methods |
|
14 |
|
15 class TimedCache(dict): |
|
16 def __init__(self, ttlm, ttls=0): |
|
17 # time to live in minutes |
|
18 self.ttl = DateTimeDelta(0, 0, ttlm, ttls) |
|
19 |
|
20 def __setitem__(self, key, value): |
|
21 dict.__setitem__(self, key, (now(), value)) |
|
22 |
|
23 def __getitem__(self, key): |
|
24 return dict.__getitem__(self, key)[1] |
|
25 |
|
26 def clear_expired(self): |
|
27 now_ = now() |
|
28 ttl = self.ttl |
|
29 for key, (timestamp, value) in self.items(): |
|
30 if now_ - timestamp > ttl: |
|
31 del self[key] |
12 |
32 |
13 |
33 |
14 class AbstractSource(object): |
34 class AbstractSource(object): |
15 """an abstract class for sources""" |
35 """an abstract class for sources""" |
16 |
36 |