Skip to main content

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 which is not thread safe. 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
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')]

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
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')]

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.

Metadata

Release files for lru-dict 1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lru-dict 1.0
File Size Uploaded
lru-dict-1.0.tar.gz 5.1 kB Details

Release files / lru-dict-1.0.tar.gz

Download URL lru-dict-1.0.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
d5c27815c121d2e3951c1d9c7f3e00797fbfa270950b240b8e453f43747750cb
BLAKE2b-256 checksum
How to use checksums
108e7fdc5705036a5b010945ce10814187c51254bd6a24e51bbcd5f1a2109e4b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

1.2.0

82 release files

1.1.7

1 release file

1.1.6

1 release file

1.1.5

1 release file

1.1.4

1 release file

1.1.3

1 release file

1.1.2

1 release file

1.1.1

1 release file

1.1

1 release file

This release

1.0 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page