Giter Site home page Giter Site logo

cache_deco's Introduction

cache_deco Build Status Coverage Status

Implements high level function caching to any backend with a decorator

Install

pip install redis_cache_decorator

Usage

Setup

from cache_deco import Cache
from backends.redis.redis_backend import RedisBackend
# Create your cache backend
redis = RedisBackend('localhost', 6379)
# Use this backend as your cache
c = Cache(redis)

Cache

@c.cache()
def my_method(a, b, c):
  return a ** b ** c

Options

expiration: Number of seconds to keep the result in the cache. Defaults to 60 seconds when not specified.

e.g.

@c.cache(expiration=100)
def my_method():
  ...

signature_generator: Callable function that generates the signature to cache on. The default signature generator will be used if not specified.

e.g.

def sig_gen(*args, **kwargs):
  return "?".join(args)
  
@c.cache(signature_generator=sig_gen)
def my_method():
  ...

invalidator: Boolean to determine whether or not to return a cache invalidating function

e.g.

@c.cache(invalidator=True)
def my_method():
    ...

Now when you call my_method, it will return two values. The first value is the cached return if a cache hit occurs, otherwise it's the return value from executing the function. The second value is a callable function to invalidate the cache.

return_value, invalidator = my_method()

To invalidate the cached return, just call the invalidator:

invalidator()

Custom Backends

You can use any backend for the cache by implementing the base class

Contributing

Check for any open issues, or open one yourself! All contributions are appreciated.

Tests

nosetests

cache_deco's People

Contributors

c-w avatar

Watchers

James Cloos avatar

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.