Debian Codegraph @

third_party/python/pylru/README.txt

raw
1
2
3 PyLRU
4 =====
5
6 A least recently used (LRU) cache for Python.
7
8 Introduction
9 ============
10
11 Pylru implements a true LRU cache along with several support classes. The cache is efficient and written in pure Python. It works with Python 2.6+ including the 3.x series. Basic operations (lookup, insert, delete) all run in a constant amount of time. Pylru provides a cache class with a simple dict interface. It also provides classes to wrap any object that has a dict interface with a cache. Both write-through and write-back semantics are supported. Pylru also provides classes to wrap functions in a similar way, including a function decorator.
12
13 You can install pylru or you can just copy the source file pylru.py and use it directly in your own project. The rest of this file explains what the pylru module provides and how to use it. If you want to know more examine pylru.py. The code is straightforward and well commented.
14
15 Usage
16 =====
17
18 lrucache
19 --------
20
21 An lrucache object has a dictionary like interface and can be used in the same way::
22
23 import pylru
24
25 size = 100 # Size of the cache. The maximum number of key/value
26 # pairs you want the cache to hold.
27
28 cache = pylru.lrucache(size)
29 # Create a cache object.
30
31 value = cache[key] # Lookup a value given its key.
32 cache[key] = value # Insert a key/value pair.
33 del cache[key] # Delete a value given its key.
34 #
35 # These three operations affect the order of the cache.
36 # Lookup and insert both move the key/value to the most
37 # recently used position. Delete (obviously) removes a
38 # key/value from whatever position it was in.
39
40 key in cache # Test for membership. Does not affect the cache order.
41
42 value = cache.peek(key)
43 # Lookup a value given its key. Does not affect the
44 # cache order.
45
46 cache.keys() # Return an iterator over the keys in the cache
47 cache.values() # Return an iterator over the values in the cache
48 cache.items() # Return an iterator over the (key, value) pairs in the
49 # cache.
50 #
51 # These calls have no effect on the cache order.
52 # lrucache is scan resistant when these calls are used.
53 # The iterators iterate over their respective elements
54 # in the order of most recently used to least recently
55 # used.
56 #
57 # WARNING - While these iterators do not affect the
58 # cache order the lookup, insert, and delete operations
59 # do. The result of changing the cache's order
60 # during iteration is undefined. If you really need to
61 # do something of the sort use list(cache.keys()), then
62 # loop over the list elements.
63
64 for key in cache: # Caches support __iter__ so you can use them directly
65 pass # in a for loop to loop over the keys just like
66 # cache.keys()
67
68 cache.size() # Returns the size of the cache
69 cache.size(x) # Changes the size of the cache. x MUST be greater than
70 # zero. Returns the new size x.
71
72 x = len(cache) # Returns the number of items stored in the cache.
73 # x will be less than or equal to cache.size()
74
75 cache.clear() # Remove all items from the cache.
76
77
78 Lrucache takes an optional callback function as a second argument. Since the cache has a fixed size, some operations (such as an insertion) may cause the least recently used key/value pair to be ejected. If the optional callback function is given it will be called when this occurs. For example::
79
80 import pylru
81
82 def callback(key, value):
83 print (key, value) # A dumb callback that just prints the key/value
84
85 size = 100
86 cache = pylru.lrucache(size, callback)
87
88 # Use the cache... When it gets full some pairs may be ejected due to
89 # the fixed cache size. But, not before the callback is called to let you
90 # know.
91
92 WriteThroughCacheManager
93 ------------------------
94
95 Often a cache is used to speed up access to some other high latency object. For example, imagine you have a backend storage object that reads/writes from/to a remote server. Let us call this object *store*. If store has a dictionary interface a cache manager class can be used to compose the store object and an lrucache. The manager object exposes a dictionary interface. The programmer can then interact with the manager object as if it were the store. The manager object takes care of communicating with the store and caching key/value pairs in the lrucache object.
96
97 Two different semantics are supported, write-through (WriteThroughCacheManager class) and write-back (WriteBackCacheManager class). With write-through, lookups from the store are cached for future lookups. Insertions and deletions are updated in the cache and written through to the store immediately. Write-back works the same way, but insertions are updated only in the cache. These "dirty" key/value pair will only be updated to the underlying store when they are ejected from the cache or when a sync is performed. The WriteBackCacheManager class is discussed more below.
98
99 The WriteThroughCacheManager class takes as arguments the store object you want to compose and the cache size. It then creates an LRU cache and automatically manages it::
100
101 import pylru
102
103 size = 100
104 cached = pylru.WriteThroughCacheManager(store, size)
105 # Or
106 cached = pylru.lruwrap(store, size)
107 # This is a factory function that does the same thing.
108
109 # Now the object *cached* can be used just like store, except caching is
110 # automatically handled.
111
112 value = cached[key] # Lookup a value given its key.
113 cached[key] = value # Insert a key/value pair.
114 del cached[key] # Delete a value given its key.
115
116 key in cache # Test for membership. Does not affect the cache order.
117
118 cached.keys() # Returns store.keys()
119 cached.values() # Returns store.values()
120 cached.items() # Returns store.items()
121 #
122 # These calls have no effect on the cache order.
123 # The iterators iterate over their respective elements
124 # in the order dictated by store.
125
126 for key in cached: # Same as store.keys()
127
128 cached.size() # Returns the size of the cache
129 cached.size(x) # Changes the size of the cache. x MUST be greater than
130 # zero. Returns the new size x.
131
132 x = len(cached) # Returns the number of items stored in the store.
133
134 cached.clear() # Remove all items from the store and cache.
135
136
137 WriteBackCacheManager
138 ---------------------
139
140 Similar to the WriteThroughCacheManager class except write-back semantics are used to manage the cache. The programmer is responsible for one more thing as well. They MUST call sync() when they are finished. This ensures that the last of the "dirty" entries in the cache are written back. This is not too bad as WriteBackCacheManager objects can be used in with statements. More about that below::
141
142
143 import pylru
144
145 size = 100
146 cached = pylru.WriteBackCacheManager(store, size)
147 # Or
148 cached = pylru.lruwrap(store, size, True)
149 # This is a factory function that does the same thing.
150
151 value = cached[key] # Lookup a value given its key.
152 cached[key] = value # Insert a key/value pair.
153 del cached[key] # Delete a value given its key.
154
155 key in cache # Test for membership. Does not affect the cache order.
156
157
158 cached.keys() # Return an iterator over the keys in the cache/store
159 cached.values() # Return an iterator over the values in the cache/store
160 cached.items() # Return an iterator over the (key, value) pairs in the
161 # cache/store.
162 #
163 # The iterators iterate over a consistent view of the
164 # respective elements. That is, except for the order,
165 # the elements are the same as those returned if you
166 # first called sync() then called
167 # store.keys()[ or values() or items()]
168 #
169 # These calls have no effect on the cache order.
170 # The iterators iterate over their respective elements
171 # in arbitrary order.
172 #
173 # WARNING - While these iterators do not effect the
174 # cache order the lookup, insert, and delete operations
175 # do. The results of changing the cache's order
176 # during iteration is undefined. If you really need to
177 # do something of the sort use list(cached.keys()),
178 # then loop over the list elements.
179
180 for key in cached: # Same as cached.keys()
181
182 cached.size() # Returns the size of the cache
183 cached.size(x) # Changes the size of the cache. x MUST be greater than
184 # zero. Returns the new size x.
185
186 cached.clear() # Remove all items from the store and cache.
187
188 cached.sync() # Make the store and cache consistent. Write all
189 # cached changes to the store that have not been
190 # yet.
191
192 cached.flush() # Calls sync() then clears the cache.
193
194
195 To help the programmer ensure that the final sync() is called, WriteBackCacheManager objects can be used in a with statement::
196
197 with pylru.WriteBackCacheManager(store, size) as cached:
198 # Use cached just like you would store. sync() is called automatically
199 # for you when leaving the with statement block.
200
201
202 FunctionCacheManager
203 ---------------------
204
205 FunctionCacheManager allows you to compose a function with an lrucache. The resulting object can be called just like the original function, but the results are cached to speed up future calls. The fuction must have arguments that are hashable::
206
207 import pylru
208
209 def square(x):
210 return x * x
211
212 size = 100
213 cached = pylru.FunctionCacheManager(square, size)
214
215 y = cached(7)
216
217 # The results of cached are the same as square, but automatically cached
218 # to speed up future calls.
219
220 cached.size() # Returns the size of the cache
221 cached.size(x) # Changes the size of the cache. x MUST be greater than
222 # zero. Returns the new size x.
223
224 cached.clear() # Remove all items from the cache.
225
226
227
228 lrudecorator
229 ------------
230
231 PyLRU also provides a function decorator. This is basically the same functionality as FunctionCacheManager, but in the form of a decorator::
232
233 from pylru import lrudecorator
234
235 @lrudecorator(100)
236 def square(x):
237 return x * x
238
239 # The results of the square function are cached to speed up future calls.
240
241 square.size() # Returns the size of the cache
242 square.size(x) # Changes the size of the cache. x MUST be greater than
243 # zero. Returns the new size x.
244
245 square.clear() # Remove all items from the cache.