Skip to content

Commit 237ad80

Browse files
committed
Fix #278: Improve TLRUCache docs.
1 parent e960781 commit 237ad80

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

docs/index.rst

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,18 +168,29 @@ computed when the item is inserted into the cache.
168168
value of `timer()`.
169169

170170
.. testcode::
171-
172-
from datetime import datetime, timedelta
173-
171+
174172
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
177176

178-
cache = TLRUCache(maxsize=10, ttu=my_ttu, timer=datetime.now)
177+
cache = TLRUCache(maxsize=10, ttu=my_ttu)
179178

180179
The expression `ttu(key, value, timer())` defines the expiration
181180
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)
183194

184195
Items that expire because they have exceeded their time-to-use will
185196
be no longer accessible, and will be removed eventually. If no

0 commit comments

Comments
 (0)