Skip to main content

Python lru_cache but with TTL

Project description

Basic Usage

This module provides memoization with LRU, TTL and various clearing utility functions.

from cachian import Cachian

class PatientDB:

    @Cachian() #LRU caching defaulting to 10,000 items
    def get_by_id(self, id):
        pass

    @Cachian(maxsize=100) #LRU caching with 100 items
    def get_by_id2(self, id):
        pass        

    @Cachian(ttl=60) #TTL caching with TTL at 60 secs
    def get_latest_10_patient(self):
        pass
    
    #Split cache clearing by first args ie. id
    @Cachian(partition_attr=1)
    def get_by_id3(self, id):
        pass

    #TTL, LRU & partition caching, whichever miss first
    @Cachian(maxsize=100,ttl=60,partition_attr=1)
    def get_by_id4(self,id):
        pass


#Can be used with standalone functions
@Cachian()
def count_patient_name_len(patient_name):
    return len(patient_name)

Backends

By default, memory backend is used. Any class that subclasses MutableMapping can be used as a backend store. Refer to the default MemoryStore for a skeletal example.

Using Redis as a backend can be achieved using:

import os
os.environ["CACHIAN_REDIS_HOST"] = "prod.redis.com"
os.environ["CACHIAN_REDIS_PORT"] = "6380"
os.environ["CACHIAN_REDIS_DB"] = "8"
os.environ["CACHIAN_REDIS_SSL"] = "true"
os.environ["CACHIAN_REDIS_PASSWORD"] = "abc123"

from cachian.redis_store import RedisStore

class PatientDB:

    @Cachian(cache_class=RedisStore) #LRU caching defaulting to 10,000 items with Redis storage backend
    def get_by_id(self, id):
        pass

Utilities

Get stats

Utilities to obtain statistics on cached methods/functions. Code continues from the Basic Usage example.

from cachian import global_cache_info
from pprint import pprint

#Print stats of all cached function/methods
pprint(global_cache_info())

#Print stats of a single cached function/method
pprint(count_patient_name_len.cache_info())

Clearing cache

A few clearing utilities to allow selecting clearing of cached items. Code continues from the Basic Usage example.

from cachian import function_cache_reset_by_class, function_cache_reset, global_cache_reset

#Clear all cached items of all methods in the class
function_cache_reset_by_class(PatientDB)

#Clear cache of a function
function_cache_reset('count_patient_name_len')

#Clear all cached items
global_cache_reset()

Installation

Basic installation

pip install Cachian

Clone the repository to run unittests.

Installing the cachetools package will allow unittest to run benchmarks against cachetools.

pip install cachetools
python -m unittest discover -s . -p "*_test.py"

License

Copyright (c) 2023-2025 Ian Teoh

Licensed under the MIT license

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

cachian-0.6.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

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

cachian-0.6-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file cachian-0.6.tar.gz.

File metadata

  • Download URL: cachian-0.6.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for cachian-0.6.tar.gz
Algorithm Hash digest
SHA256 c78c569711ba5100217eca37c34b0463958d3576af974a4a0f5ba4eda2cc6ed6
MD5 cda87fb71548bd62ccaaa1e0ec9f6284
BLAKE2b-256 eece66d7f7e700bf1cc9f878467e041b9f6f52b6978665b679c72f2677d6a960

See more details on using hashes here.

File details

Details for the file cachian-0.6-py3-none-any.whl.

File metadata

  • Download URL: cachian-0.6-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for cachian-0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 f07f236414456a87533dc978c6dc2935bfa942b34318a571916a65de1dc3cfc6
MD5 ed211a294ecc6053e802fb8a2111cfda
BLAKE2b-256 ecc031dea116d9d0a0de111d233e1b9837263d96f9d716deb1ba2699a8313a92

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