-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright (c) Amit Dev R | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
Metadata-Version: 2.1 | ||
Name: lru-dict | ||
Version: 1.1.8 | ||
Summary: An Dict like LRU container. | ||
Home-page: https://github.com/amitdev/lru-dict | ||
Author: Amit Dev | ||
License: MIT | ||
Keywords: lru,dict | ||
Classifier: Development Status :: 5 - Production/Stable | ||
Classifier: Intended Audience :: Developers | ||
Classifier: License :: OSI Approved :: MIT License | ||
Classifier: Operating System :: OS Independent | ||
Classifier: Operating System :: POSIX | ||
Classifier: Programming Language :: C | ||
Classifier: Programming Language :: Python :: 2 | ||
Classifier: Programming Language :: Python :: 3 | ||
Classifier: Programming Language :: Python :: 3.6 | ||
Classifier: Programming Language :: Python :: 3.7 | ||
Classifier: Programming Language :: Python :: 3.8 | ||
Classifier: Programming Language :: Python :: 3.9 | ||
Classifier: Programming Language :: Python :: 3.10 | ||
Classifier: Programming Language :: Python :: Implementation :: CPython | ||
Classifier: Topic :: Software Development :: Libraries :: Python Modules | ||
Description-Content-Type: text/x-rst | ||
License-File: LICENSE | ||
Provides-Extra: test | ||
Requires-Dist: pytest ; extra == 'test' | ||
|
||
.. image:: https://travis-ci.com/amitdev/lru-dict.svg?branch=master | ||
:target: https://travis-ci.com/amitdev/lru-dict | ||
|
||
.. image:: https://github.com/amitdev/lru-dict/actions/workflows/tests.yml/badge.svg | ||
:target: https://github.com/amitdev/lru-dict/actions/workflows/tests.yml | ||
|
||
.. image:: https://github.com/amitdev/lru-dict/actions/workflows/build-and-deploy.yml/badge.svg | ||
:target: https://github.com/amitdev/lru-dict/actions/workflows/build-and-deploy.yml | ||
|
||
LRU Dict | ||
======== | ||
|
||
A fixed size dict like container which evicts Least Recently Used (LRU) items | ||
once size limit is exceeded. There are many python implementations available | ||
which does similar things. This is a fast and efficient C implementation. | ||
LRU maximum capacity can be modified at run-time. | ||
If you are looking for pure python version, look `else where <http://www.google.com/search?q=python+lru+dict>`_. | ||
|
||
Usage | ||
===== | ||
|
||
This can be used to build a LRU cache. Usage is almost like a dict. | ||
|
||
.. code:: python | ||
|
||
from lru import LRU | ||
l = LRU(5) # Create an LRU container that can hold 5 items | ||
|
||
print l.peek_first_item(), l.peek_last_item() #return the MRU key and LRU key | ||
# Would print None None | ||
|
||
for i in range(5): | ||
l[i] = str(i) | ||
print l.items() # Prints items in MRU order | ||
# Would print [(4, '4'), (3, '3'), (2, '2'), (1, '1'), (0, '0')] | ||
|
||
print l.peek_first_item(), l.peek_last_item() #return the MRU key and LRU key | ||
# Would print (4, '4') (0, '0') | ||
|
||
l[5] = '5' # Inserting one more item should evict the old item | ||
print l.items() | ||
# Would print [(5, '5'), (4, '4'), (3, '3'), (2, '2'), (1, '1')] | ||
|
||
l[3] # Accessing an item would make it MRU | ||
print l.items() | ||
# Would print [(3, '3'), (5, '5'), (4, '4'), (2, '2'), (1, '1')] | ||
# Now 3 is in front | ||
|
||
l.keys() # Can get keys alone in MRU order | ||
# Would print [3, 5, 4, 2, 1] | ||
|
||
del l[4] # Delete an item | ||
print l.items() | ||
# Would print [(3, '3'), (5, '5'), (2, '2'), (1, '1')] | ||
|
||
print l.get_size() | ||
# Would print 5 | ||
|
||
l.set_size(3) | ||
print l.items() | ||
# Would print [(3, '3'), (5, '5'), (2, '2')] | ||
print l.get_size() | ||
# Would print 3 | ||
print l.has_key(5) | ||
# Would print True | ||
print 2 in l | ||
# Would print True | ||
|
||
l.get_stats() | ||
# Would print (1, 0) | ||
|
||
|
||
l.update(5='0') # Update an item | ||
print l.items() | ||
# Would print [(5, '0'), (3, '3'), (2, '2')] | ||
|
||
l.clear() | ||
print l.items() | ||
# Would print [] | ||
|
||
def evicted(key, value): | ||
print "removing: %s, %s" % (key, value) | ||
|
||
l = LRU(1, callback=evicted) | ||
|
||
l[1] = '1' | ||
l[2] = '2' | ||
# callback would print removing: 1, 1 | ||
|
||
l[2] = '3' | ||
# doesn't call the evicted callback | ||
|
||
print l.items() | ||
# would print [(2, '3')] | ||
|
||
del l[2] | ||
# doesn't call the evicted callback | ||
|
||
print l.items() | ||
# would print [] | ||
|
||
Install | ||
======= | ||
|
||
:: | ||
|
||
pip install lru-dict | ||
|
||
or | ||
|
||
:: | ||
|
||
easy_install lru_dict | ||
|
||
|
||
When to use this | ||
================ | ||
|
||
Like mentioned above there are many python implementations of an LRU. Use this | ||
if you need a faster and memory efficient alternative. It is implemented with a | ||
dict and associated linked list to keep track of LRU order. See code for a more | ||
detailed explanation. To see an indicative comparison with a pure python module, | ||
consider a `benchmark <https://gist.github.com/amitdev/5773979>`_ against | ||
`pylru <https://pypi.python.org/pypi/pylru/>`_ (just chosen at random, it should | ||
be similar with other python implementations as well). | ||
|
||
:: | ||
|
||
$ python bench.py pylru.lrucache | ||
Time : 3.31 s, Memory : 453672 Kb | ||
$ python bench.py lru.LRU | ||
Time : 0.23 s, Memory : 124328 Kb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
lru.cpython-310-darwin.so,sha256=M2koUSvR5Wsb4S78bfmbkFw3zkkgT29t75wBUUzXysg,38536 | ||
lru_dict-1.1.8.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4 | ||
lru_dict-1.1.8.dist-info/LICENSE,sha256=GWo0-7TdPNM1QhB86XvRyQO72zzzB_fJTnGqesuyuYg,1057 | ||
lru_dict-1.1.8.dist-info/METADATA,sha256=LGMPE-HwRCCDXYLe8fnOYqTFQS8ZwzFnYlQpYpeT6ng,4638 | ||
lru_dict-1.1.8.dist-info/RECORD,, | ||
lru_dict-1.1.8.dist-info/WHEEL,sha256=wHXPjcF1PbvralrZAZffaBRfMW8u_7d-L-q3slIS7Go,111 | ||
lru_dict-1.1.8.dist-info/top_level.txt,sha256=OIPSj70MdT9Mc7ZePoSS7eeje55Qu-04zgpp-Ws75s4,4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Wheel-Version: 1.0 | ||
Generator: bdist_wheel (0.37.1) | ||
Root-Is-Purelib: false | ||
Tag: cp310-cp310-macosx_10_9_x86_64 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
lru |