Skip to main content

LRU Cache Implementation

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

python-lru

Least Recently Used (LRU) Cache implementation

Usage

Instantiate a cache collection object specifying storage parameters. The cache object itself is thread safe. However, depending on the storage backend, it may not be safe to open a cache store multiple times.

from lru import LRUCache, ItemNotCached

# Open cache store (all arguments are optional)
cache = LRUCache(
    storage = MemoryStorage() or ShelvedStorage(path=''),
    max_size = 1000000,
    sizeof = lambda o: len(str(o)),
    max_age = timedelta(minutes=15))

# Add items with keys
cache['name'] = "Bob"
cache['age'] = 12

# Check for items in cache
try:
    age = cache['age']
except ItemNotCached:
    print("No age")

Cache Objects

Cached data can be any variable, and must be cached using a string key. It's up to you to ensure that you don't mutate objects returned from the cache, as storage won't automatically update from changes.

Cache Parameters

The LRUCache container parameters are:

  • storage: Where to store cached data. See Storages.
  • sizeof: Callable to estimate the size of an object being cached.
  • max_size: Maximum size before starting to forget cached items.
  • max_age: All cached items will expire after this amount of time.
  • storage: Object to use to store cached data

Storages

There are a few storage classes provided. All are inherited from CacheStorage

  • MemoryStorage: Caches data in memory
  • ShelvedStorage: Caches data in shelve. Really only useful if you're caching large objects.
  • Sqlite3Storage: Slowest storage engine, but possibly accessible from multiple processes?

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

python-lru-0.2.1.tar.gz (10.7 kB view details)

Uploaded Source

Built Distributions

python_lru-0.2.1-py3.8.egg (35.2 kB view details)

Uploaded Egg

python_lru-0.2.1-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file python-lru-0.2.1.tar.gz.

File metadata

  • Download URL: python-lru-0.2.1.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.1

File hashes

Hashes for python-lru-0.2.1.tar.gz
Algorithm Hash digest
SHA256 36be3ce8f5533cd1452379d72fb3c4fbe58add3b323ebccf41a3689799349952
MD5 a98a17cfefc7871225ae4070bb593d79
BLAKE2b-256 fa148410589088c81b558b495508c962a7b157dbf0cd2ba1c25f99edb7f9b40f

See more details on using hashes here.

File details

Details for the file python_lru-0.2.1-py3.8.egg.

File metadata

  • Download URL: python_lru-0.2.1-py3.8.egg
  • Upload date:
  • Size: 35.2 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.1

File hashes

Hashes for python_lru-0.2.1-py3.8.egg
Algorithm Hash digest
SHA256 7f1bbfe1a190b1b2eadaa35b27dc34a71c0e7e5baeb17435cf00e71eb6a82989
MD5 3aaec53fa75b5ec2cc6bb3f36715dbb2
BLAKE2b-256 c19cf380b4f08bcb0f75a1b23bd3fe40621daca3b5805db14b734bfa584f0fe7

See more details on using hashes here.

File details

Details for the file python_lru-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: python_lru-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.1

File hashes

Hashes for python_lru-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b1a3ab3943644547ffe9e674732ebaf0675e10fdd4459324a9d3e97528ea2964
MD5 88e5df93cf0dde553311a79e9f51bf23
BLAKE2b-256 de2d292be3212b1b02cf6402f863e080d6c529cd31c0b62f272fea087586a9ea

See more details on using hashes here.

Supported by

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