Skip to main content

Implementation of a simple persistent value and function cache.

Project description

Copyright 2017 Jeff Ward

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

This is an extremely basic implementation of a value and function cache. There are better tools out there I am sure. It is in initial development so there are likely major bugs. I may update this as I use it more, but you are safest expecting no further updates.

The intent behind this package is to provide something that in one line decorates a function call to, as transparently as possible, cache its results. The use case I built this around was searching hyperparameters. By wrapping your model call in this cache you can quickly implement either random search, or even MCTS, where previously generated results are automatically re-loaded without putting custom caching logic into your code. Additionally, if you are performing grid searches with parameter values from a-c and later realizae you wanted to search from a-m then using this function cache allows you to re-test and automatically re-load existing results while only calculating new results.

A simple example:

import easycache as ec
import time


def myExpensiveFunction(cost, extraCost=0.0):
    time.sleep(cost)
    time.sleep(extraCost)
    print("This was expensive!")
    return cost

def gridSearchExample(paramA, paramB):
    """ Simulates a grid search over parameter values

    :param paramA: list of values
    :param paramB: list of values
    :return:
    """
    cachedEF = ec.cacheFunction(myExpensiveFunction, 'example.pkl', name="myExpensiveFunction")

    bestValue = None
    bestA = None
    bestB = None
    for a in paramA:
        for b in paramB:
            newValue = cachedEF(a+b)
            if bestValue is None or newValue > bestValue:
                bestValue = newValue
                bestA = a
                bestB = b
    return bestValue, bestA, bestB

print("Best Value={} using A={} and B={}".format(*gridSearchExample([.1, .2], [.01, .02])))

#Oh, we REALLY should have searched more! This should only run the 2 new tests
print("Best Value={} using A={} and B={}".format(*gridSearchExample([.1, .2], [.01, .02, .003])))

Of course you can do other things:

def runClass(cache):
    print("Cached Value={}".format(cache.someValue))
    #Assignment will flush the cache.
    #If this is a complex object you will need to manually flush if internal state changes
    cache.someValue = 12
    print("Cached Value={}".format(cache.someValue))
    print("peek={}".format(cache.myExpensiveFunction(.5, mode="cache_peek")))
    cache.myExpensiveFunction(.5)

def classExample():
    cache = ec.EasyCache("exampleClass.pkl")
    cache.clearCache()
    cache.cacheFunction(myExpensiveFunction, name="myExpensiveFunction")
    cache.cacheProperty("someValue", initialValue=42)
    runClass(cache)
    runClass(cache)
    cache.myExpensiveFunction(.5,mode="force_run")
    #need 'mode' for a parameter? change it
    cache.modeArg='mode2'
    cache.myExpensiveFunction(.5,mode2="clear_clear")
    cache.myExpensiveFunction(.5)
    del cache.someValue
    del cache.myExpensiveFunction

classExample()

And you can ignore parameters for the purpose of caching:

def myComplexExpensiveFunction(cost, uglyState, moreUglyState=None):
    time.sleep(cost)
    print("This was expensive and complex!")
    return cost

def ignoreParametersExample():
    cachedCEF = ec.cacheFunction(myComplexExpensiveFunction, 'example.pkl', name="myComplexExpensiveFunction", ignoreArgs=(1,), ignorekwArgs=('moreUglyState'))
    cachedCEF(.5, 'ignored for caching purposes', moreUglyState='also ignored for caching purposes')
    cachedCEF(.5, 'still cached', moreUglyState='still cached')
    cachedCEF(.1, 'this wasn\'t cached', moreUglyState='because the arg changed')

ignoreParametersExample()

Project details


Download files

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

Source Distribution

easycache-0.1.5.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

easycache-0.1.5-py2.py3-none-any.whl (7.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file easycache-0.1.5.tar.gz.

File metadata

  • Download URL: easycache-0.1.5.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for easycache-0.1.5.tar.gz
Algorithm Hash digest
SHA256 832e88f1df96908f6eb89181ba78758bec876e27aa815c0042dc8bc78a0881b9
MD5 0d068c321cf81ab500e4744a4313985b
BLAKE2b-256 f5a5ca0a44b668910c22c6f3bccaa3b442616a5e9f4cb6564a8d22dc6c28e8ec

See more details on using hashes here.

File details

Details for the file easycache-0.1.5-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for easycache-0.1.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2c112d109ba3265c3714d6848168337f9a29e8ea45d5df3e16ed0a6130380a52
MD5 37f8ce03886989520cabdfbf8a343b5a
BLAKE2b-256 6bee88051ba93c7d107d1cb20f3ce69bfec112869344222d58d82538ee1b57ff

See more details on using hashes here.

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