temporal-cache

Time-based cache invalidation

https://github.com/timkpaine/temporal-cache/workflows/Build%20Status/badge.svg?branch=mainBuild Status https://codecov.io/gh/timkpaine/temporal-cache/branch/main/graph/badge.svg?token=ag2j2TV2wECoverage https://img.shields.io/github/license/timkpaine/temporal-cache.svgLicense https://img.shields.io/pypi/v/temporal-cache.svgPyPI https://img.shields.io/readthedocs/temporal-cache.svgDocs

Install

From pip

pip install temporal-cache

Or from source

python setup.py install

Why?

I needed something that would automagically refresh at 4:00pm when markets close.


    @expire(hour=16)
    def fetchFinancialData():
    

Interval Cache

The interval cache expires every time interval since its first use


    @interval(seconds=5, minutes=2)
    def myfoo():
        '''myfoo's lru_cache will expire 2 minutes, 5 seconds after last use'''

Expire Cache

The expire cache expires on the time given, in scheduler/cron style.


    @expire(second=5, minute=2)
    def myfoo():
        '''myfoo's lru_cache will expire on the second minute, fifth second of every hour, every day, etc'''

Caveats

Python hashing symantics persist. Dicts will be frozen, lists will be converted to tuples. Users are advised to pre-freeze to avoid issues.

Development

See CONTRIBUTING.md for guidelines.

License

This software is licensed under the Apache 2.0 license. See the LICENSE and AUTHORS files for details.

API Documentation

temporalcache.expire.daily(on=0, tz=None, maxsize=128, persistent='', custom=None, **kwargs)[source]
temporalcache.expire.expire(second=None, minute=None, hour=None, day=None, day_of_week=None, week=None, month=None, tz=None, maxsize=128, persistent='', custom=None, **kwargs)[source]

Expires all entries in the cache @ whole number time

for example, @expire(0, 30, 16) will expire the cache at 4:30pm every day

temporalcache.expire.hourly(on=0, tz=None, maxsize=128, persistent='', custom=None, **kwargs)[source]
temporalcache.expire.minutely(on=0, tz=None, maxsize=128, persistent='', custom=None, **kwargs)[source]
temporalcache.expire.monthly(on=0, tz=None, maxsize=128, persistent='', custom=None, **kwargs)[source]
temporalcache.interval.daily(maxsize=128, persistent='', custom=None, **kwargs)[source]
temporalcache.interval.hourly(maxsize=128, persistent='', custom=None, **kwargs)[source]
temporalcache.interval.interval(seconds=0, minutes=0, hours=0, days=0, weeks=0, months=0, years=0, maxsize=128, persistent='', custom=None, **kwargs)[source]

Expires all entries in the cache every interval

temporalcache.interval.minutely(maxsize=128, persistent='', custom=None, **kwargs)[source]
temporalcache.interval.monthly(maxsize=128, persistent='', custom=None, **kwargs)[source]
class temporalcache.utils.StorageBase[source]

Bases: object

cache_clear()[source]
exception temporalcache.utils.TCException[source]

Bases: Exception

temporalcache.utils.calc[source]
temporalcache.utils.disable()[source]
temporalcache.utils.enable()[source]
temporalcache.utils.should_expire(last, now, secondly=None, minutely=None, hourly=None, daily=None, day_of_week=None, weekly=None, monthly=None)[source]

should the cache expire? last - datetime now - datetime

if yearly:
necessary_distance = calc(0, 0, 0, 0, 0, 0, yearly)