Skip to content

Commit 2e344cd

Browse files
committed
Add section on insertion order and sort order
1 parent 1506b72 commit 2e344cd

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

docs/tutorial.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,26 @@ Some users may defer all culling to a cron-like process by setting the
294294
Each of these methods is designed to work concurrent to others. None of them
295295
block readers or writers in other threads or processes.
296296

297+
Caches may be iterated by either insertion order or sorted order. The default
298+
ordering uses insertion order. To iterate by sorted order, use :meth:`iterkeys
299+
<.Cache.iterkeys>`. The sort order is determined by the database which makes it
300+
valid only for `str`, `bytes`, `int`, and `float` data types. Other types of
301+
keys will be serialized which is likely to have a meaningless sorted order.
302+
303+
>>> for key in 'cab':
304+
... cache[key] = None
305+
>>> list(cache)
306+
['c', 'a', 'b']
307+
>>> list(cache.iterkeys())
308+
['a', 'b', 'c']
309+
>>> cache.peekitem()
310+
('b', None)
311+
>>> cache.peekitem(last=False)
312+
('c', None)
313+
314+
If only the first or last item in insertion order is desired then
315+
:meth:`peekitem <.Cache.peekitem>` is more efficient than using iteration.
316+
297317
Lastly, three methods support metadata about the cache. The first is
298318
:meth:`volume <diskcache.Cache.volume>` which returns the estimated total size
299319
in bytes of the cache directory on disk.

0 commit comments

Comments
 (0)