GithubHelp home page GithubHelp logo

microcache's Introduction

microcache

Latest Version Build Status

A Really! Small! Cache! for python 2 and 3.

Why?

Working on a totally separate project, I found myself wanting to use funcy.memoize...a lot. There was a major problem, though: I couldn't test with it. There was no (obvious) way to turn it off. It was making me crazy, so I spent a few hours writing a small module (even smaller than this, originally) to suit the memoization needs, with a hook to turn it off for testing. When I got to the end of my feature sprint for that project, I decided it made more sense to spin it out into its own project, so here we are!

Current features

  • Simple upsert and get workflow
  • TTLs for expiring cache items
  • Decorator for "memoization"
  • Enabling and disabling functionality, including a context manager
  • Work directly on the imported module... no additional instantiation (unless you want to)

Installation

pip install microcache

Examples

Basic usage

>>> import microcache
>>> microcache.has('key')
False
>>> microcache.get('key')
CACHE_MISS
>>> microcache.get('key', default='default')
'default'
>>> microcache.upsert('key', 'value')
>>> microcache.get('key')
'value'
>>> microcache.disable()
>>> microcache.get('key')
CACHE_DISABLED

Decorator and context manager

import microcache
import time

@microcache.this
def somefunc():
    time.sleep(5)
    return "this will be cached, and you won't have to wait a second time!"

def test_somefunc():
    somefunc()
    somefunc()
    with microcache.temporarily_disabled():
        # now we'll have to wait again
        somefunc()

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.