Code Monkey home page Code Monkey logo

django-function-cache's Introduction

Table of Contents generated with autotoc

Django Function Cache

django_function_cache allows you to wrap functions so they cache their output if they are repeatedly called with the same arguments. Assumes that functions return values that can be pickled.

Build Status

Requirements

  • Python 2.6+ (untested with Python 3)
  • Django 1.4+

Installing

$ pip install django_function_cache

Usage

Simply wrap cached around your expensive function:

from urllib2 import urlopen
import json

from django_function_cache import cached


def get_external_ip():
    response = urlopen("http://httpbin.org/ip").read()
    ip = json.loads(response)['origin']
    return ip


def print_my_ip():
    print cached(get_external_ip)()

You can also specify a timeout:

def print_my_ip():
    print cached(get_external_ip, minutes=5)()

You can specify any time units supported by timedelta.

If the wrapped function raises an exception, nothing is cached and the exception propagates as if you'd call the function directly.

Alternatives

If you're just caching database calls, you're probably better off using Johnny Cache.

Changelog

v1.2

Added Python 2.6 support. Added Travis tests to ensure we work with the range of Python versions and Django versions that we claim we do.

v1.1

Changed the top level package name. Previously you would write:

from cached import cached

Now:

from django_function_cache import cached

v1.0

Initial release.

Developing

Check out the code, and install it in a virtual environment that includes Django:

$ source path/to/your/virtualenv
$ pip install -e .

Hack away, then send a pull request when you're happy.

Releasing a new version

$ python setup.py sdist upload

django-function-cache's People

Contributors

wilfred avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.