cool-cache


Namecool-cache JSON
Version 0.3.9 PyPI version JSON
download
home_pagehttps://github.com/jeff-hykin/cool_cache.git
SummaryCache any function to disk
upload_time2023-12-04 20:52:29
maintainer
docs_urlNone
authorJeff Hykin
requires_python>=3.6
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # What is this?

The smart way to cache outputs to cold storage.

- auto rebuilds cache when you edit function source code
- uses mutltiprocessing to keep main thread running fast while saving to disk
- excellent change-tracking of arguments thanks to `super_hash`
- can watch change-tracking of external vars and method attributes
- uses python pickle for saving function outputs, if `dill` is available it will use that instead

# How do I use this?

`pip install cool_cache`

```python
from cool_cache import cache, settings

settings.default_folder = None # disable caching to cold-storage, and instead cache to ram
# this is the default, but you can change it
settings.default_folder = "cache.ignore/"

# 
# 
# simple usage (updates whenever function is edited (excluding comments) or when args change)
# 
# 
@cache()
def things_with_args(a,b,c):
    
    from time import sleep; sleep(1) # <- simulating a long-running process
    
    return a + b + c

things_with_args(1,2,3) # not yet cached
things_with_args(1,2,3) # uses cache
things_with_args(9,9,9) # not yet cached
things_with_args(9,9,9) # uses cache


# 
# 
# external vars
# 
# 
external_counter = 0
@cache(depends_on=lambda:[external_counter])
def things_with_external(a,b,c):
    global external_counter
    
    from time import sleep; sleep(1) # <- simulating a long-running process
    
    return external_counter + a + b + c


# 
# behavior
# 
things_with_external(4,5,6) # not yet cached
things_with_external(4,5,6) # uses cache
external_counter = 1
things_with_external(4,5,6) # not yet cached (because external_counter changed)
things_with_external(4,5,6) # uses cache

# 
# 
# filepath arguments
# 
# 
@cache(watch_filepaths=lambda arg1, arg2, arg3: [ arg1, arg2 ]) # because first two args are filepaths
def things_with_files(filepath1, filepath2, c):
    with open(filepath1, 'r') as in_file1:
        with open(filepath2, 'r') as in_file2:
            return in_file1.readlines() + c + in_file2.readlines()

# 
# behavior
# 
things_with_files("./file1.txt", "./file2.txt", "hello")  # not yet cached
things_with_files("./file1.txt", "./file2.txt", "hello")  # cached
with open("./file2.txt",'w') as f: f.write(str(" world")) # <-- modify the file
things_with_files("./file1.txt", "./file2.txt", "hello")  # not yet cached, because file change is detected
things_with_files("./file1.txt", "./file2.txt", "hello")  # cached

# 
# 
# class methods (e.g. self)
# 
# 
class MyThing:
    def __init__(self, path, other_stuff):
        self.path = path
        self.other_stuff = other_stuff
    
    # for example: self.path changing will affect the cache, but self.other_stuff wont affect the cache
    @cache(watch_attributes=lambda self:[ self.path, ])
    def do_some_stuff(self, arg1):
        from time import sleep; sleep(1)
        return self.path + arg1

# 
# bust=True wipes out all cached values for this function on the next run
# 
@cache(bust=True)
def things(a,b,c):
    return 10

```



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/jeff-hykin/cool_cache.git",
    "name": "cool-cache",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "",
    "author": "Jeff Hykin",
    "author_email": "jeff.hykin@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/91/39/1c0ec6b5209ff78252c6afe202ad305cefbd10562f65683a663843ff59df/cool_cache-0.3.9.tar.gz",
    "platform": null,
    "description": "# What is this?\n\nThe smart way to cache outputs to cold storage.\n\n- auto rebuilds cache when you edit function source code\n- uses mutltiprocessing to keep main thread running fast while saving to disk\n- excellent change-tracking of arguments thanks to `super_hash`\n- can watch change-tracking of external vars and method attributes\n- uses python pickle for saving function outputs, if `dill` is available it will use that instead\n\n# How do I use this?\n\n`pip install cool_cache`\n\n```python\nfrom cool_cache import cache, settings\n\nsettings.default_folder = None # disable caching to cold-storage, and instead cache to ram\n# this is the default, but you can change it\nsettings.default_folder = \"cache.ignore/\"\n\n# \n# \n# simple usage (updates whenever function is edited (excluding comments) or when args change)\n# \n# \n@cache()\ndef things_with_args(a,b,c):\n    \n    from time import sleep; sleep(1) # <- simulating a long-running process\n    \n    return a + b + c\n\nthings_with_args(1,2,3) # not yet cached\nthings_with_args(1,2,3) # uses cache\nthings_with_args(9,9,9) # not yet cached\nthings_with_args(9,9,9) # uses cache\n\n\n# \n# \n# external vars\n# \n# \nexternal_counter = 0\n@cache(depends_on=lambda:[external_counter])\ndef things_with_external(a,b,c):\n    global external_counter\n    \n    from time import sleep; sleep(1) # <- simulating a long-running process\n    \n    return external_counter + a + b + c\n\n\n# \n# behavior\n# \nthings_with_external(4,5,6) # not yet cached\nthings_with_external(4,5,6) # uses cache\nexternal_counter = 1\nthings_with_external(4,5,6) # not yet cached (because external_counter changed)\nthings_with_external(4,5,6) # uses cache\n\n# \n# \n# filepath arguments\n# \n# \n@cache(watch_filepaths=lambda arg1, arg2, arg3: [ arg1, arg2 ]) # because first two args are filepaths\ndef things_with_files(filepath1, filepath2, c):\n    with open(filepath1, 'r') as in_file1:\n        with open(filepath2, 'r') as in_file2:\n            return in_file1.readlines() + c + in_file2.readlines()\n\n# \n# behavior\n# \nthings_with_files(\"./file1.txt\", \"./file2.txt\", \"hello\")  # not yet cached\nthings_with_files(\"./file1.txt\", \"./file2.txt\", \"hello\")  # cached\nwith open(\"./file2.txt\",'w') as f: f.write(str(\" world\")) # <-- modify the file\nthings_with_files(\"./file1.txt\", \"./file2.txt\", \"hello\")  # not yet cached, because file change is detected\nthings_with_files(\"./file1.txt\", \"./file2.txt\", \"hello\")  # cached\n\n# \n# \n# class methods (e.g. self)\n# \n# \nclass MyThing:\n    def __init__(self, path, other_stuff):\n        self.path = path\n        self.other_stuff = other_stuff\n    \n    # for example: self.path changing will affect the cache, but self.other_stuff wont affect the cache\n    @cache(watch_attributes=lambda self:[ self.path, ])\n    def do_some_stuff(self, arg1):\n        from time import sleep; sleep(1)\n        return self.path + arg1\n\n# \n# bust=True wipes out all cached values for this function on the next run\n# \n@cache(bust=True)\ndef things(a,b,c):\n    return 10\n\n```\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Cache any function to disk",
    "version": "0.3.9",
    "project_urls": {
        "Homepage": "https://github.com/jeff-hykin/cool_cache.git"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7316a15ebc2fe5375b91166f56e745cb92126876bf8816006b39ec8960ca0e59",
                "md5": "965ce65c77eb8588ab115808d561dddc",
                "sha256": "feae84488efea8f2b8f0e0bc565b62ca4772265fbac1123eef7986fe3db04735"
            },
            "downloads": -1,
            "filename": "cool_cache-0.3.9-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "965ce65c77eb8588ab115808d561dddc",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 2234189,
            "upload_time": "2023-12-04T20:52:24",
            "upload_time_iso_8601": "2023-12-04T20:52:24.146623Z",
            "url": "https://files.pythonhosted.org/packages/73/16/a15ebc2fe5375b91166f56e745cb92126876bf8816006b39ec8960ca0e59/cool_cache-0.3.9-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "91391c0ec6b5209ff78252c6afe202ad305cefbd10562f65683a663843ff59df",
                "md5": "56116fd597eb51ed493fa32748089ae7",
                "sha256": "d742ebce22d634d3bb1e6aa99bf02acadd9dc24c6cfeb06b1df683c05d64f421"
            },
            "downloads": -1,
            "filename": "cool_cache-0.3.9.tar.gz",
            "has_sig": false,
            "md5_digest": "56116fd597eb51ed493fa32748089ae7",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 2100122,
            "upload_time": "2023-12-04T20:52:29",
            "upload_time_iso_8601": "2023-12-04T20:52:29.148238Z",
            "url": "https://files.pythonhosted.org/packages/91/39/1c0ec6b5209ff78252c6afe202ad305cefbd10562f65683a663843ff59df/cool_cache-0.3.9.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-04 20:52:29",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "jeff-hykin",
    "github_project": "cool_cache",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "cool-cache"
}
        
Elapsed time: 0.25125s