summaryrefslogtreecommitdiff
path: root/docs/narr.rst
blob: 7343a71be3b0d9668eeca04bab5ccd92e8787d68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
Using :mod:`repoze.lru`
======================

``repoze.lru`` is a LRU (least recently used) cache implementation.  Keys and
values that are not used frequently will be evicted from the cache faster
than keys and values that are used frequently.  It works under Python 2.5,
Python 2.6, Python 2.7, and Python 3.2.

Using the API programmatically
------------------------------

Creating an LRUCache object:

.. doctest::

   >>> from repoze.lru import LRUCache
   >>> cache = LRUCache(100) # 100 max length

Retrieving from an LRUCache object:

.. doctest::

   >>> cache.get('nonexisting', 'foo') # return 'foo'
   'foo'
   >>> cache.get('nonexisting') is None
   True

Adding to an LRUCache object:

.. doctest::

   >>> cache.put('existing', 'value') # add the key 'key' with the value 'value'
   >>> cache.get('existing') # return the value for existing
   'value'

Clearing an LRUCache:

.. doctest::

   >>> cache.clear()


Decorating an "expensive" function call
---------------------------------------

:mod:`repoze.lru` provides a class :class:`~repoze.lru.lru_cache`, which
wrapps another callable, caching the results.  All values passed to the
decorated function must be hashable.  It does not support keyword arguments:

.. doctest::

   >>> from repoze.lru import lru_cache
   >>> @lru_cache(500)
   ... def expensive_function(*arg): #*
   ...     pass

Each function decorated with the lru_cache decorator uses its own
cache related to that function.