Git Product home page Git Product logo

temporal-cache's Introduction

temporal-cache

Time-based cache invalidation

Build Status Coverage License PyPI Docs

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.

temporal-cache's People

Contributors

timkpaine avatar dependabot[bot] avatar

Stargazers

Steve Simmons avatar Eugene Mayer avatar Scott Ivey avatar

Watchers

James Cloos avatar  avatar  avatar

temporal-cache's Issues

Interval purge cache by key?

We may want to expand this so that the interval cache expires x duration after last call with a specific key. This is a pretty straightforward feature to implement, just replace foo.cache_clear with the appropriate deleter. But since functools.lru_cache does not support this, we would need to switch to e.g. ring or roll our own.

persistent_lru_cache generates a new key for the same arguments between runtimes when there's more than 1 argument

Describe the bug
The cache persistence only seems to work correctly when there is a single argument in the function call. The cached result is not retrieved for multiple arguments or any keyword arguments in subsequent runtimes (although it does work in the same runtime).

To Reproduce
Make a scatch.py:

    from temporalcache.persistent_lru_cache import persistent_lru_cache

    @persistent_lru_cache('test.cache')
    def foo(*args, **kwargs):
        print(f'running {args, kwargs}')
        return f'{args, kwargs}

Run scratch.py twice.

Expected behavior
The first time scratch.py is run the expected behavior is seen:

running (('1',), {})
(('1',), {})
(('1',), {})
running (('1', '2'), {})
(('1', '2'), {})
(('1', '2'), {})
running ((), {'kw': 'k'})
((), {'kw': 'k'})
((), {'kw': 'k'})

The second time, I would expect to see:

(('1',), {})
(('1',), {})
(('1', '2'), {})
(('1', '2'), {})
((), {'kw': 'k'})
((), {'kw': 'k'})

Instead, the second (and future runs) result in:

(('1',), {})
(('1',), {})
running (('1', '2'), {})
(('1', '2'), {})
(('1', '2'), {})
running ((), {'kw': 'k'})
((), {'kw': 'k'})
((), {'kw': 'k'})

Desktop (please complete the following information):

  • OS: Windows 10
  • Version: temporal-cache 0.1.4

Additional context
I believe the issue is that for multiple arguments or keyword arguments (where a kwd_mark object is added as an additional argument) the key is generated from _HashedSeq(key) instead of just key[0].
The hash of _HashedSeq uses hash(tup), but the python built-in hash is only deterministic in the same runtime, not between runtimes.

I think the fix probably requires the use of something like hashlib.sha256(), but then that only takes bytes, and I'm not sure what the implications of that may be. Maybe some combination with json.dumps()?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.