@@ -168,18 +168,29 @@ computed when the item is inserted into the cache.
168
168
value of `timer() `.
169
169
170
170
.. testcode ::
171
-
172
- from datetime import datetime, timedelta
173
-
171
+
174
172
def my_ttu(_key, value, now):
175
- # assume value.ttl contains the item's time-to-live in hours
176
- return now + timedelta(hours=value.ttl)
173
+ # assume value.ttu contains the item's time-to-use in seconds
174
+ # note that the _key argument is ignored in this example
175
+ return now + value.ttu
177
176
178
- cache = TLRUCache(maxsize=10, ttu=my_ttu, timer=datetime.now )
177
+ cache = TLRUCache(maxsize=10, ttu=my_ttu)
179
178
180
179
The expression `ttu(key, value, timer()) ` defines the expiration
181
180
time of a cache item, and must be comparable against later results
182
- of `timer() `.
181
+ of `timer() `. As with :class: `TTLCache `, a custom `timer ` function
182
+ can be supplied, which does not have to return a numeric value.
183
+
184
+ .. testcode ::
185
+
186
+ from datetime import datetime, timedelta
187
+
188
+ def datetime_ttu(_key, value, now):
189
+ # assume now to be of type datetime.datetime, and
190
+ # value.hours to contain the item's time-to-use in hours
191
+ return now + timedelta(hours=value.hours)
192
+
193
+ cache = TLRUCache(maxsize=10, ttu=datetime_ttu, timer=datetime.now)
183
194
184
195
Items that expire because they have exceeded their time-to-use will
185
196
be no longer accessible, and will be removed eventually. If no
0 commit comments