1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """Dictionary with item expiration."""
19
20 __revision__="$Id: expdict.py 600 2005-08-19 10:45:03Z jajcus $"
21 __docformat__="restructuredtext en"
22
23 import time
24 import threading
25
27 """An extension to standard Python dictionary objects which implements item
28 expiration.
29
30 Each item in ExpiringDictionary has its expiration time assigned, after
31 which the item is removed from the mapping.
32
33 :Ivariables:
34 - `_timeouts`: a dictionary with timeout values and timeout callback for
35 stored objects.
36 - `_default_timeout`: the default timeout value (in seconds from now).
37 - `_lock`: access synchronization lock.
38 :Types:
39 - `_timeouts`: `dict`
40 - `_default_timeout`: `int`
41 - `_lock`: `threading.RLock`"""
42
43 __slots__=['_timeouts','_default_timeout','_lock']
44
46 """Initialize an `ExpiringDictionary` object.
47
48 :Parameters:
49 - `default_timeout`: default timeout value for stored objects.
50 :Types:
51 - `default_timeout`: `int`"""
52 dict.__init__(self)
53 self._timeouts={}
54 self._default_timeout=default_timeout
55 self._lock=threading.RLock()
56
64
72
75
76 - def set_item(self,key,value,timeout=None,timeout_callback=None):
77 """Set item of the dictionary.
78
79 :Parameters:
80 - `key`: the key.
81 - `value`: the object to store.
82 - `timeout`: timeout value for the object (in seconds from now).
83 - `timeout_callback`: function to be called when the item expires.
84 The callback should accept none, one (the key) or two (the key
85 and the value) arguments.
86 :Types:
87 - `key`: any hashable value
88 - `value`: any python object
89 - `timeout`: `int`
90 - `timeout_callback`: callable"""
91 self._lock.acquire()
92 try:
93 if not timeout:
94 timeout=self._default_timeout
95 self._timeouts[key]=(time.time()+timeout,timeout_callback)
96 return dict.__setitem__(self,key,value)
97 finally:
98 self._lock.release()
99
101 """Do the expiration of dictionary items.
102
103 Remove items that expired by now from the dictionary."""
104 self._lock.acquire()
105 try:
106 for k in self._timeouts.keys():
107 self._expire_item(k)
108 finally:
109 self._lock.release()
110
112 """Do the expiration of a dictionary item.
113
114 Remove the item if it has expired by now.
115
116 :Parameters:
117 - `key`: key to the object.
118 :Types:
119 - `key`: any hashable value"""
120 (timeout,callback)=self._timeouts[key]
121 if timeout<=time.time():
122 if callback:
123 try:
124 callback(key,dict.__getitem__(self,key))
125 except TypeError:
126 try:
127 callback(key)
128 except TypeError:
129 callback()
130 del self[key]
131
132
133