Skip to main content

An Dict like LRU container.

Project description

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.3.0.tar.gz (13.1 kB view hashes)

Uploaded Source

Built Distributions

lru_dict-1.3.0-pp310-pypy310_pp73-win_amd64.whl (13.8 kB view hashes)

Uploaded PyPy Windows x86-64

lru_dict-1.3.0-pp310-pypy310_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (13.3 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

lru_dict-1.3.0-pp310-pypy310_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (12.8 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

lru_dict-1.3.0-pp310-pypy310_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (13.1 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

lru_dict-1.3.0-pp310-pypy310_pp73-macosx_10_9_x86_64.whl (10.1 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

lru_dict-1.3.0-pp39-pypy39_pp73-win_amd64.whl (13.8 kB view hashes)

Uploaded PyPy Windows x86-64

lru_dict-1.3.0-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (13.3 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

lru_dict-1.3.0-pp39-pypy39_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (12.8 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

lru_dict-1.3.0-pp39-pypy39_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (13.1 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

lru_dict-1.3.0-pp39-pypy39_pp73-macosx_10_9_x86_64.whl (10.1 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

lru_dict-1.3.0-pp38-pypy38_pp73-win_amd64.whl (13.7 kB view hashes)

Uploaded PyPy Windows x86-64

lru_dict-1.3.0-pp38-pypy38_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (13.3 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

lru_dict-1.3.0-pp38-pypy38_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (12.9 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

lru_dict-1.3.0-pp38-pypy38_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (13.1 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

lru_dict-1.3.0-pp38-pypy38_pp73-macosx_10_9_x86_64.whl (10.1 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

lru_dict-1.3.0-cp312-cp312-win_amd64.whl (13.8 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

lru_dict-1.3.0-cp312-cp312-win32.whl (12.7 kB view hashes)

Uploaded CPython 3.12 Windows x86

lru_dict-1.3.0-cp312-cp312-musllinux_1_1_x86_64.whl (37.1 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ x86-64

lru_dict-1.3.0-cp312-cp312-musllinux_1_1_ppc64le.whl (38.6 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ ppc64le

lru_dict-1.3.0-cp312-cp312-musllinux_1_1_i686.whl (35.1 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ i686

lru_dict-1.3.0-cp312-cp312-musllinux_1_1_aarch64.whl (37.3 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ ARM64

lru_dict-1.3.0-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (33.5 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ppc64le

lru_dict-1.3.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (32.5 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

lru_dict-1.3.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (32.2 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

lru_dict-1.3.0-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (29.9 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

lru_dict-1.3.0-cp312-cp312-macosx_11_0_arm64.whl (11.1 kB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

lru_dict-1.3.0-cp312-cp312-macosx_10_9_x86_64.whl (10.7 kB view hashes)

Uploaded CPython 3.12 macOS 10.9+ x86-64

lru_dict-1.3.0-cp312-cp312-macosx_10_9_universal2.whl (17.2 kB view hashes)

Uploaded CPython 3.12 macOS 10.9+ universal2 (ARM64, x86-64)

lru_dict-1.3.0-cp311-cp311-win_amd64.whl (13.7 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

lru_dict-1.3.0-cp311-cp311-win32.whl (12.5 kB view hashes)

Uploaded CPython 3.11 Windows x86

lru_dict-1.3.0-cp311-cp311-musllinux_1_1_x86_64.whl (36.5 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

lru_dict-1.3.0-cp311-cp311-musllinux_1_1_ppc64le.whl (38.2 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ ppc64le

lru_dict-1.3.0-cp311-cp311-musllinux_1_1_i686.whl (35.1 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ i686

lru_dict-1.3.0-cp311-cp311-musllinux_1_1_aarch64.whl (36.7 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ ARM64

lru_dict-1.3.0-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (33.1 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ppc64le

lru_dict-1.3.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (31.8 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

lru_dict-1.3.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (31.6 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

lru_dict-1.3.0-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (29.8 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

lru_dict-1.3.0-cp311-cp311-macosx_11_0_arm64.whl (11.3 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

lru_dict-1.3.0-cp311-cp311-macosx_10_9_x86_64.whl (11.1 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

lru_dict-1.3.0-cp311-cp311-macosx_10_9_universal2.whl (17.8 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

lru_dict-1.3.0-cp310-cp310-win_amd64.whl (13.7 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

lru_dict-1.3.0-cp310-cp310-win32.whl (12.5 kB view hashes)

Uploaded CPython 3.10 Windows x86

lru_dict-1.3.0-cp310-cp310-musllinux_1_1_x86_64.whl (34.6 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

lru_dict-1.3.0-cp310-cp310-musllinux_1_1_ppc64le.whl (36.3 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ ppc64le

lru_dict-1.3.0-cp310-cp310-musllinux_1_1_i686.whl (33.4 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ i686

lru_dict-1.3.0-cp310-cp310-musllinux_1_1_aarch64.whl (34.8 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ ARM64

lru_dict-1.3.0-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (31.9 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ppc64le

lru_dict-1.3.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (30.6 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

lru_dict-1.3.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (30.3 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

lru_dict-1.3.0-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (28.7 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

lru_dict-1.3.0-cp310-cp310-macosx_11_0_arm64.whl (11.3 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

lru_dict-1.3.0-cp310-cp310-macosx_10_9_x86_64.whl (11.0 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

lru_dict-1.3.0-cp310-cp310-macosx_10_9_universal2.whl (17.7 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64)

lru_dict-1.3.0-cp39-cp39-win_amd64.whl (13.7 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

lru_dict-1.3.0-cp39-cp39-win32.whl (12.6 kB view hashes)

Uploaded CPython 3.9 Windows x86

lru_dict-1.3.0-cp39-cp39-musllinux_1_1_x86_64.whl (34.2 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

lru_dict-1.3.0-cp39-cp39-musllinux_1_1_ppc64le.whl (35.9 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ ppc64le

lru_dict-1.3.0-cp39-cp39-musllinux_1_1_i686.whl (33.0 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ i686

lru_dict-1.3.0-cp39-cp39-musllinux_1_1_aarch64.whl (34.4 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ ARM64

lru_dict-1.3.0-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (31.5 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ppc64le

lru_dict-1.3.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (30.3 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

lru_dict-1.3.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (29.8 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

lru_dict-1.3.0-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (28.3 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

lru_dict-1.3.0-cp39-cp39-macosx_11_0_arm64.whl (11.3 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

lru_dict-1.3.0-cp39-cp39-macosx_10_9_x86_64.whl (11.0 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

lru_dict-1.3.0-cp39-cp39-macosx_10_9_universal2.whl (17.7 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64)

lru_dict-1.3.0-cp38-cp38-win_amd64.whl (13.7 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

lru_dict-1.3.0-cp38-cp38-win32.whl (12.6 kB view hashes)

Uploaded CPython 3.8 Windows x86

lru_dict-1.3.0-cp38-cp38-musllinux_1_1_x86_64.whl (34.9 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

lru_dict-1.3.0-cp38-cp38-musllinux_1_1_ppc64le.whl (36.6 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ ppc64le

lru_dict-1.3.0-cp38-cp38-musllinux_1_1_i686.whl (33.7 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ i686

lru_dict-1.3.0-cp38-cp38-musllinux_1_1_aarch64.whl (35.0 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ ARM64

lru_dict-1.3.0-cp38-cp38-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (32.5 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ppc64le

lru_dict-1.3.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (31.2 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

lru_dict-1.3.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (30.8 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

lru_dict-1.3.0-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (29.3 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

lru_dict-1.3.0-cp38-cp38-macosx_11_0_arm64.whl (11.4 kB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

lru_dict-1.3.0-cp38-cp38-macosx_10_9_x86_64.whl (11.0 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

lru_dict-1.3.0-cp38-cp38-macosx_10_9_universal2.whl (17.7 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ universal2 (ARM64, x86-64)

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page