Metadata-Version: 2.5
Name: filecache-functionsource
Version: 1.0.0
Summary: Persistent caching decorator
Author-email: ubershmekel <ubershmekel@gmail.com>
Maintainer-email: Lassi Haasio <ilikeavocadoes@devmail.cc>
License-Expression: BSD-2-Clause
License-File: LICENSE
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: BSD License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Utilities
Requires-Python: >=3.11
Description-Content-Type: text/markdown

# filecache-functionsource

> **This is a maintained fork of [filecache](https://github.com/ubershmekel/filecache) by ubershmekel.**
> The original package is no longer maintained. This fork is published on PyPI as `filecache-functionsource`.

`filecache` is a decorator which saves the return value of functions even after the interpreter dies. For example this is useful on functions that download
and parse webpages. You can specify how long the return values should be cached (use seconds, like time.sleep) or just cache forever. The cache is in a file right next to the calling `.py` file.

## Function source-aware caching

Unlike the original `filecache`, this fork includes the **source code** of the decorated function in the cache key. This means that if you change the logic of a cached function, the cache is automatically invalidated and the function will be re-executed. This is especially useful during development, where stale cache results from a previous version of a function can be a source of confusing bugs.

# Install

    pip install filecache-functionsource

    # or for developing and debugging after cloning this package

    pip install -e .

# Usage

    from filecache import filecache

    @filecache
    def time_consuming_function(args):
        # cache results forever

    @filecache(filecache.YEAR)
    def another_function(args):
        # etc

    @filecache(24 * 60 * 60)
    def yet_another_function(args):
        # etc

# How it works

Each time a decorated function is called, filecache checks in a `shelve` [1] to see if the arguments and function name have a recorded return value. If the recorded return value is still valid, it's returned, otherwise the original function is called and its return value is recorded with a timestamp.


[1] A shelve is a dictionary in a file with string keys and `pickle` values. The shelve file is named according to the module name that called filecache.

# Caveats

* All arguments of the decorated function and the return value need to be
    picklable for this to work.

* The cache isn't automatically cleaned, it is only overwritten. If your
    function can receive many different arguments that rarely repeat, your
    cache may forever grow. One day I might add a feature that once in every
    100 calls scans the db for outdated stuff and erases.

* This is less useful on methods of a class because the instance (self)
    is cached, and if the instance isn't the same, the cache isn't used. This
    makes sense because class methods are affected by changes in whatever
    is attached to self.

* Tested on python 3.11+

* License: BSD. See [LICENSE](LICENSE) for details.


Here's a trick to invalidate a single value:

    @filecache.filecache
    def somefunc(x, y, z):
        return x * y * z

    del somefunc._db[filecache._args_key(somefunc, (1,2,3), {})]
    # or just iterate through `somefunc._db` (it's a shelve, which is like a dict) to find the right key.
