Giter Club home page Giter Club logo

stupydcache's Introduction

This is a simple on-disk compressed cache for Python objects mimicking dict interface.

Sample usage:

Cache

If you have a program that takes some input data (let's say from data files), makes computationally or I/O intense operations on them and then continues:

r = read_and_process_data(datafile)
#... do stuff with r

and the data changes infrequently, you can use the stupydcache module to cache the result:

from stupydcache import Cache
cache = Cache()
if datafile in cache:
    r = cache[datafile]
else:
    r = read_and_process_data(datafile)
    cache[datafile] = r

cache is persistent on disk, the next invocation of the program will use the cached values.

memoize

The module provides one decorator, memoize. You can use it to decorate a function, each call to the function will go through the cache, the key will be constructed from the arguments and the function name. Each subsequent call with the same arguments will use the cached value. Since each cache entry is a separate file, do not use it when you call the functions with many (as in, many thousands) different combinations of their arguments.

You can provide a cache directory and/or a cache name:

from stupydcache import memoize

@memoize(cachedir='/tmp/cachedir', cachename='mycache')
def fun(*args):
    ...
    return result

Or as a shortcut, use it without arguments (as a decorator, @memoize is equivalent to @memoize()):

from stupydcache import memoize

@memoize
def fun(*args):
    ...
    return result

Caching into RAM

If you use a dictionary as a cachedir, the caching will be done in RAM, using that dictionary, using function arguments as the dictionary keys. However, you run into issues - if the function arguments are unhashable, the decorator will raise an exception. Use with care.

Do not reuse the same dictionary for different functions - only the arguments are used as the keys, not the function name.

Example: from stupydcache import memoize

cache_dict = {}
@memoize(cache_dict)
def fun(*args):
    ...
    return result

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.