Skip to main content

Simple, in-memory, thread-safe caching system for python programs.

Project description

smoothcache

smoothcache is a simple, in-memory, thread-safe caching system for your python programs.

Installation

pip install smoothcache

Usage

Interacting with the cache is done using a CacheController object:

from smoothcache import CacheController

Cache = CacheController()

Cache.set("cache key", "cache value")

Interacting with the cache object

The cache object exposes 4 methods for interacting with the cache:

  • set
  • get
  • clear
  • remove

Set

Cache.set(key, value, ttl=None)

The set method adds an entry to the cache.

The key parameter is how you can access the cached value.

The ttl parameter is the Time-to-Live value (in seconds) that the cache entry will remain valid.

After ttl seconds has passed, the cache will no longer return the value. The default ttl value is set at 3600 seconds (1 hour) and can be modified in the cache settings.

If a key is passed to set that already exists, the entry gets overridden. This behavior can be changed in the cache settings to instead raise a KeyAlreadyExistsError when a duplicate key is passed.

Get

Cache.get(key, default=None)

The get method retrieves an entry from the cache.

The key parameter is the value of the entries key set with the set method.

The default parameter is the value that will be returned if the entry is no longer valid, or doesn't exist.

get can also be configured in the cache settings to raise an EntryNotFoundError when the specified key doesn't exist or an EntryExpiredError when the specified cache entry has expired.

get returns a CacheResult object with two attributes (key and value) when the entry is found. Otherwise, get returns default.

Clear

Cache.clear()

The clear method removes all entries from the cache

Remove

Cache.remove(key)

The remove method removes the specified key from the cache.

By default, if the key doesn't exist in the cache, the function silently returns and doesn't fail. This behavior can be changed in the cache settings to instead raise an EntryNotFoundError.

Cache Settings

The Cache object's behavior can be modified with it's settings. Settings are accessed within the object's settings property.

# Cache.settings.<setting name> = <setting value>
# For example:
Cache.settings.error_on_dup_key = False

error_on_dup_key:

Default: False

If true, raise a KeyAlreadyExistsError when set is called with a key that is already set in the cache. If false, the cache entry with key is overwritten if the key is already set.

error_on_invalid_key:

Default: False

If true, raise an EntryNotFoundError when get or remove is called with a key that is not set in the cache. If false, the get call will return it's default and the remove call silently returns.

error_on_expired_entry:

Default: False

If true, raise an EntryExpiredError when the entry returned from a get call has gone past it's TTL. If false, the get call will return it's default.

default_ttl:

Default: 3600 (1 hour)

The TTL value (in seconds) set on any entry that does not explicitly have a TTL value.

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

smoothcache-1.1.0.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

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

smoothcache-1.1.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file smoothcache-1.1.0.tar.gz.

File metadata

  • Download URL: smoothcache-1.1.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for smoothcache-1.1.0.tar.gz
Algorithm Hash digest
SHA256 b9cdb6869e8e386b12d29514470147890fb1a2b4c2407d1cd5c69d04604dc7f0
MD5 3fdb7ad8ccf8327b8b1367e5d95ca795
BLAKE2b-256 91c8fdf45ed28841ea8746c3a60045142e590c1c69e33c585c88244fb8a28b6e

See more details on using hashes here.

File details

Details for the file smoothcache-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: smoothcache-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for smoothcache-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 448207a4f559b267f9b59324e0eb8bdb12e26ccaf1fd9b18dbf76825e1e0cf24
MD5 2690e02c3fc27b150579ff78be70d8f5
BLAKE2b-256 ebd7ff9b69a90f3f7a3604567bc062d0140f97c70e3285eb82d504f097643f75

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