Skip to main content

A simple, lightweight caching library for disk-based or object-based caching

Project description

light-cache

A Python package for using disk-based or object-bashed caching, perfect for simple scripts or Jupyter Notebooks.

Features

Flexible Storage Options

  • Multiple cache storage modes (Memory, Disk, or Hybrid)
  • Support for multiple independent cache stores
  • Configurable cache directory location

Performance

  • In-memory caching for fast access
  • Disk persistence for data durability
  • Hybrid mode for optimal performance and reliability

⚙️ Advanced Cache Control

  • Automatic cache expiration
  • Manual cache item removal
  • Configurable default values for cache misses
  • Non-expiring cache items support

🛠️ Developer Friendly

  • Simple, intuitive API
  • Perfect for scripts and Jupyter notebooks
  • No external dependencies
  • Lightweight alternative to Redis for simple use cases

Install

Use pip to install:

pip install light-cache

Usage

To get started, you need to set up a cache store:

from light_cache import CacheStore

cache = CacheStore(persist_cache=True, keep_cache_in_memory=True)

There are three main modes for this cache:

  1. Disk cache: Setting persist_cache to True and keep_cache_in_memory to False, the cache will read and write to a local cache file on each get/put operation. If you have many instances of a script running and need to keep the cache in sync, this is likely the right option.
  2. Memory cache: Setting persist_cache to False and keep_cache_in_memory to True, keeps the cache in memory during the execution of the script or while the notebook is running. The cache will not persist between sessions. If you do not need the cache to exist after the session ends and have many computationally intensive functions during the execution, this is likely the right option.
  3. Hybrid (default and recommended): Setting both to True will allow the cache to stay in memory during execution but save changes to a file to persist between sessions. For most use-cases of working in a notebook or creating small scripts that do not require full Redis or similar cache, this is likely the right option.

Using multiple stores

You can set up different cache stores for different information. For example, if you need one store for just movies and another for podcasts, you could:

from light_cache import CacheStore

movie_cache = CacheStore(store='movies')
podcast_cache = CacheStore(store='podcasts')

Setting and retrieving values

The main methods are get(), put(), has(), forget(), and clear().

from light_cache import CacheStore

cache = CacheStore()

cache.put('some-key', 'some-value')
my_value = cache.get('some-key')

# `get()` returns None if cache doesn't exist. This can be changed using the `default` parameter
my_value = cache.get('some-nonexistent-key', default=0)

if cache.has('some-key'):
    # do something

# `pull()` will retrieve a cached item and delete it from the cache if it exists
my_value = cache.pull('some-key')

# Expiration is in seconds (defaults to 5 minutes)
cache.put('new-key', 'new-value', expires=1200)

# Set to None for items that should not expire
cache.put('never-expire-key', 'never-expire-value', expires=None)

# Items that do not expire will remain in cache until the item is removed using `forget()`
cache.forget('never-expire-key')

# `clear()` removes all items from the store at once
cache.clear()

Change location of the cache directory

When using disk-based cache, the files are stored in .cache/ but this can be changed:

from light_cache import CacheStore

cache = CacheStore(cache_directory='src/app/cache')

Contributing

Community made feature requests, patches, bug reports, and contributions are always welcome.

Please review our contributing guidelines if you decide to make a contribution.

Setting up development environment

This project uses Poetry for managing dependencies and environments.

Tests

This project uses pytest for its tests.

To run tests locally, use:

poetry run pytest

Formatter and linter

This project uses ruff for formatting and linting. To run the formatter, use:

poetry run ruff format .

To run the linter, use:

poetry run ruff check .

License

This project is licensed under the MIT License. See LICENSE for more details.

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

light_cache-1.0.0.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

light_cache-1.0.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file light_cache-1.0.0.tar.gz.

File metadata

  • Download URL: light_cache-1.0.0.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for light_cache-1.0.0.tar.gz
Algorithm Hash digest
SHA256 269b300f6aca110eebebb9966433e8f61df2d66c0f85a1a37c41899d813031a1
MD5 5f114f18093264f91e21faca55faa483
BLAKE2b-256 736466ee1debdb5ae9a1129f520fa68e02f4cc77c02d392c50aecc86c939d4e1

See more details on using hashes here.

File details

Details for the file light_cache-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: light_cache-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for light_cache-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b3f1e492900f12d9e5eabafc4d5e797b5da885d968de07bf7876eaeda02feb21
MD5 fffd1f3580daf0b31d90220e6e74e5dc
BLAKE2b-256 f3e74614a9e2fc78a01a7365918f485c2133d6bef20fdd91e7d2a40145a21702

See more details on using hashes here.

Supported by

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