Skip to main content

cachettl is an elegant LRU TTL Cache decorator with methods cache_info() and cache_clear().

Project description

cachettl v1.0.0

An elegant LRU TTL Cache decorator with methods cache_info() and cache_clear()

More info about LRU (Last Recent Used) cache: https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU)

def cachettl(ttl=60, maxsize=128, typed=False):
    """An elegant TTL Cache decorator with methods cache_info() and cache_clear()
        
    If *maxsize* is set to None, the LRU features are disabled and the cache
    can grow without bound.

    If *typed* is True, arguments of different types will be cached separately.
    For example, f(3.0) and f(3) will be treated as distinct calls with
    distinct results.

    Arguments to the cached function must be hashable.

    View the cache statistics named tuple (hits, misses, maxsize, currsize and remainingttl)
    with f.cache_info().  Clear the cache and statistics with f.cache_clear().

    Access the underlying function with f._wrapped.

    """

Installation

pip install cachettl

Usage example:

from cachettl import cachettl
import datetime as dt, time

@cachettl(ttl=4)
def print_datetime():
    return dt.datetime.now().strftime("%d/%m/%Y %H:%M:%S")
    
if __name__ == '__main__':
    for I in range(15):
        print(f"{'%02d'%(I+1)}. {print_datetime()}")
        print(f"    CacheInfo: {print_datetime.cache_info()} - Only Remaining TTL: {print_datetime.cache_info().remainingttl}")
        time.sleep(0.5)

Output:

01. 19/06/2024 20:54:34
    CacheInfo: CacheInfo(hits=0, misses=1, maxsize=128, currsize=1, remainingttl=3.999934673309326) - Only Remaining TTL: 3.999899387359619
02. 19/06/2024 20:54:34
    CacheInfo: CacheInfo(hits=1, misses=1, maxsize=128, currsize=1, remainingttl=3.4991674423217773) - Only Remaining TTL: 3.499129295349121
03. 19/06/2024 20:54:34
    CacheInfo: CacheInfo(hits=2, misses=1, maxsize=128, currsize=1, remainingttl=2.9986002445220947) - Only Remaining TTL: 2.998547315597534
04. 19/06/2024 20:54:34
    CacheInfo: CacheInfo(hits=3, misses=1, maxsize=128, currsize=1, remainingttl=2.4979164600372314) - Only Remaining TTL: 2.4978702068328857
05. 19/06/2024 20:54:36
    CacheInfo: CacheInfo(hits=3, misses=2, maxsize=128, currsize=2, remainingttl=3.9998769760131836) - Only Remaining TTL: 3.999843120574951
06. 19/06/2024 20:54:36
    CacheInfo: CacheInfo(hits=4, misses=2, maxsize=128, currsize=2, remainingttl=3.4992663860321045) - Only Remaining TTL: 3.499230146408081
07. 19/06/2024 20:54:36
    CacheInfo: CacheInfo(hits=5, misses=2, maxsize=128, currsize=2, remainingttl=2.9985880851745605) - Only Remaining TTL: 2.998532295227051
08. 19/06/2024 20:54:36
    CacheInfo: CacheInfo(hits=6, misses=2, maxsize=128, currsize=2, remainingttl=2.497987985610962) - Only Remaining TTL: 2.4979512691497803
09. 19/06/2024 20:54:36
    CacheInfo: CacheInfo(hits=7, misses=2, maxsize=128, currsize=2, remainingttl=1.9973821640014648) - Only Remaining TTL: 1.997338056564331
10. 19/06/2024 20:54:36
    CacheInfo: CacheInfo(hits=8, misses=2, maxsize=128, currsize=2, remainingttl=1.4968440532684326) - Only Remaining TTL: 1.4968063831329346
11. 19/06/2024 20:54:36
    CacheInfo: CacheInfo(hits=9, misses=2, maxsize=128, currsize=2, remainingttl=0.9962716102600098) - Only Remaining TTL: 0.9962363243103027
12. 19/06/2024 20:54:36
    CacheInfo: CacheInfo(hits=10, misses=2, maxsize=128, currsize=2, remainingttl=0.4957406520843506) - Only Remaining TTL: 0.49570631980895996
13. 19/06/2024 20:54:40
    CacheInfo: CacheInfo(hits=10, misses=3, maxsize=128, currsize=3, remainingttl=3.999873638153076) - Only Remaining TTL: 3.999840497970581
14. 19/06/2024 20:54:40
    CacheInfo: CacheInfo(hits=11, misses=3, maxsize=128, currsize=3, remainingttl=3.499295473098755) - Only Remaining TTL: 3.4992594718933105
15. 19/06/2024 20:54:40
    CacheInfo: CacheInfo(hits=12, misses=3, maxsize=128, currsize=3, remainingttl=2.9987926483154297) - Only Remaining TTL: 2.998769521713257

Sugestions, feedbacks, bugs...

E-mail me: ricardoabuchaim at gmail.com

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cachettl-1.0.0.tar.gz (5.0 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: cachettl-1.0.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.11.9

File hashes

Hashes for cachettl-1.0.0.tar.gz
Algorithm Hash digest
SHA256 3a35c78b56b650fbc53deb6a68369f5c6bc5985920ef483fe30877f082a776b0
MD5 0f2f00da5bb0a3153084d3eadffe0882
BLAKE2b-256 f1b999d5d0ad013f6a7dca7730f7d7d61a0cfb2053c8eac7fff195df1dfca0df

See more details on using hashes here.

Provenance

Supported by

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