Giter VIP home page Giter VIP logo

memoizewrapper's Introduction

memoizewrapper

A python module provides decorators to store wrapped function's return values into memory. They are used on relatively expensive functions, which allows a certain delay. For example, they can be used when load meta data from database, which is assumed won't change frequently.

WARNING: It is tested in Python 3.5.1. keygenerator module uses inspect, which keeps changing (the interface was changed after 3.4.4 even). In test part, nonlocal is used also, which makes Python 2.x unhappy. Making it compatible with Python 2.6.x to 3.5.x is on my list.

Features

  • Expiring Memoize decorator which allows user to set a time-to-live on cache. (Of course, 0 means forever)
  • LRU Memoize decorator which provides a cache storage with a given capacity.
  • Flush the cache explicitly
  • Key template - user can customize how their key look like.
  • Skip cache - user can control what return values they want to cache.
  • Deep copy - if the stored value is deep copied.

Usage

Expiring cache decorator

import memoizewrapper

# cache will be expired in 10 seconds
# the stored/returned value will be deep-copied. Feel free to change them
# cache is based on parameter `param1`
@memoizewrapper.expiring_memoize(('param1',), 10, deepcopy=True)
def my_func(param1):
    return param1

# flush the cache
my_func.flush()

Lru cache decorator

import memoizewrapper

# it maintains the cache by LRU algorithm, and the capacity is 3
# the stored/returned value is not deep-copied. It is better if the return values are immutable,
# (or, you remember not to modify them)
# cache is based on parameter `param1` and `param2`. Even if the returned values depend on
# `param3`, `param3` wont be a part of the key, see example below
@memoizewrapper.lru_memoize(('param1', 'param2'), 3)
def my_func(param1, param2, param3):
    return ', '.join([param1, param2, param3])

my_func('Good', 'morning', 'Beijing')  # 'Good morning Beijing'
my_func('Good', 'morning', 'Shanghai')  # 'Good morning Beijing'

skip cache

import memoizewrapper

# Sometimes, you don't want to cache the return value. Let say we are querying a card database,
# we don't want to cache a card if it does not exist.
@memoizewrapper.lru_memoize(('card_id',), 1000, escape_cache_if=lambda x: x is None)
def query_card_name(card_id, db_connection):
    cursor = db_connection.cursor()
    cursor.execute('''SELECT `card_name` FROM `card` WHERE `card_id`=%(card_id)s''',
                   {'card_id': card_id})
    row = cursor.fetchone()
    return row['card_name'] if row else None

customize key generator and storage

memoizewrapper can be extended to provide more features. Samples are provided below.

file cache

UNDER CONSTRUCTION

actively clean expired cache

UNDER CONSTRUCTION

memoizewrapper's People

Contributors

theliuy avatar

Watchers

James Cloos 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.