Skip to main content

An Dict like LRU container.

Project description

https://travis-ci.com/amitdev/lru-dict.svg?branch=master https://github.com/amitdev/lru-dict/actions/workflows/tests.yml/badge.svg https://github.com/amitdev/lru-dict/actions/workflows/build-and-deploy.yml/badge.svg

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.

Usage

This can be used to build a LRU cache. Usage is almost like a dict.

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 against 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lru-dict-1.1.8.tar.gz (10.9 kB view hashes)

Uploaded source

Built Distributions

lru_dict-1.1.8-cp310-cp310-win_amd64.whl (12.3 kB view hashes)

Uploaded cp310

lru_dict-1.1.8-cp310-cp310-win32.whl (11.3 kB view hashes)

Uploaded cp310

lru_dict-1.1.8-cp39-cp39-win_amd64.whl (12.3 kB view hashes)

Uploaded cp39

lru_dict-1.1.8-cp39-cp39-win32.whl (11.3 kB view hashes)

Uploaded cp39

lru_dict-1.1.8-cp38-cp38-win_amd64.whl (12.3 kB view hashes)

Uploaded cp38

lru_dict-1.1.8-cp38-cp38-win32.whl (11.3 kB view hashes)

Uploaded cp38

lru_dict-1.1.8-cp37-cp37m-win_amd64.whl (12.3 kB view hashes)

Uploaded cp37

lru_dict-1.1.8-cp37-cp37m-win32.whl (11.3 kB view hashes)

Uploaded cp37

lru_dict-1.1.8-cp36-cp36m-win_amd64.whl (12.8 kB view hashes)

Uploaded cp36

lru_dict-1.1.8-cp36-cp36m-win32.whl (11.5 kB view hashes)

Uploaded cp36

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page