File: narr.rst

package info (click to toggle)
python-repoze.lru 0.7-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 220 kB
  • sloc: python: 966; makefile: 127
file content (95 lines) | stat: -rw-r--r-- 2,551 bytes parent folder | download | duplicates (5)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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()

Each LRU cache tracks some basic statistics via attributes:

  cache.lookups     # number of calls to the get method
  cache.hits        # number of times a call to get found an object
  cache.misses      # number of times a call to get did not find an object
  cahce.evictions   # number of times a object was evicted from cache


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.

Cleaning cache of decorated function
------------------------------------

:mod:`repoze.lru` provides a :class:`~repoze.lru.CacheMaker`, which generates 
decorators. This way, you can later clear your cache if needed.

.. doctest::

   >>> from repoze.lru import CacheMaker
   >>> cache_maker=CacheMaker()
   >>> @cache_maker.lrucache(maxsize=300, name="adder")
   ... def yet_another_exepensive_function(*arg):#*
   ...     pass

   >>> @cache_maker.expiring_lrucache(maxsize=300,timeout=30)
   ... def another_exepensive_function(*arg):#*
   ...     pass

This way, when you need it you can choose to either clear all cache: 

.. doctest::
   
   >>> cache_maker.clear()

or clear a specific cache

.. doctest::
   
   >>> cache_maker.clear("adder")