Code Monkey home page Code Monkey logo

wrapcache's Introduction

wrapcache

A python Function / Method OUTPUT cache system base on function Decorators. Supports to cache into redis or LRU memory. 中文README.md

Build Status codecov Status PyPi Status Python Versions PyPi Downloads

Homepage | Usage & Doc | Source Code | Issue & Bug

What's wrapcache?

wrapcache is a decorator that enables caching of return values for functions.

The cache keys are dependent completely on the arguments passed to the function. very simple to use.

Also has some API to Programmatic get cache or remove cache. Support python 2.6 ~ python3.5.

Here's an example of how you might use wrapcache:

import wrapcache

from time import sleep
import random

@wrapcache.wrapcache(timeout = 3)
def need_cache_function(input, t = 2, o = 3):
    sleep(2)
    return random.randint(1, 100)

if __name__ == "__main__":
	for i in range(10):
		sleep(1)
		print(need_cache_function(1, t = 2, o = 3))
	
	#get cache Programmatic
	key_func = wrapcache.keyof(need_cache_function, 1, o = 3, t = 2)
	print(wrapcache.get(key_func))
	#remove cache Programmatic
	print(wrapcache.remove(wrapcache.keyof(need_cache_function, 1, o = 3, t = 2)))

Some config:

@wrapcache.wrapcache(timeout = timeout, adapter = adapter)
  • timeout: how much seconds the cache exist. Default is -1, not cached.
  • adapter: cache where, now can be RedisAdapter and MemoryAdapter. Default is MemoryAdapter.

When use RedisAdapter, you need to set redis instance before use. e.g.

REDIS_POOL = redis.ConnectionPool(host = '10.246.13.1', port = 6379, password = 'redis_pwd', db = 1)
REDIS_INST = redis.Redis(connection_pool = REDIS_POOL, charset = 'utf8')
RedisAdapter.db = REDIS_INST

When use MemoryAdapter, you can set Memory Storage Policy before use, that is choose where to store. Default is store in {}, also it provide LUR(Supported by jlhutch/pylru, add dynamic size.) algorithm. e.g.

from wrapcache.database import LruCacheDB
lruDB = MemoryAdapter.db = LruCacheDB(size = 100)
RedisAdapter.db = lruDB

How to Install and Use?

Install

Three ways to install wrapcache:

1. Use tool install

  • pip install wrapcache

2. Download to install

3. Manual installation

  • Manual installation: Put wrapcache folder into current directory or site-packages, then import wrapcache to use.

Usage

Decorators

import wrapcache
@wrapcache.wrapcache(timeout = 3)
def need_cache_function():
	return 'hello wrapcache'

API

  1. wrapcache.keyof(func, *args, **kws): get the key of function output cache.
  2. wrapcache.get(key, adapter = MemoryAdapter): get the value of cache.
  3. wrapcache.set(key, value, timeout = -1, adapter = MemoryAdapter): set cache use code.
  4. wrapcache.remove(key, adapter = MemoryAdapter): remove a cache.
  5. wrapcache.flush(adapter = MemoryAdapter): clear all the cache.

The API 2~5, need to input a adapter to set which db to operate.

TODO

  • usage wiki.

wrapcache's People

Contributors

hustcc avatar ralph-wang avatar

Watchers

 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.