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

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):
    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
    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")))

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


And you can ignore parameters for the purpose of caching:

def myComplexExpensiveFunction(cost, uglyState, moreUglyState=None):
    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')


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 hashes)

Uploaded source

Built Distribution

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

Uploaded py2 py3

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