All Projects → suzaku → Cachelper

suzaku / Cachelper

Licence: mit

Programming Languages

python
139335 projects - #7 most used programming language

Labels

Projects that are alternatives of or similar to Cachelper

Q Municate Services Ios
Easy-to-use services for Quickblox SDK, for speeding up development of iOS chat applications
Stars: ✭ 30 (-37.5%)
Mutual labels:  cache
Use Axios Request
Data fetching is easy with React Hooks for axios!
Stars: ✭ 38 (-20.83%)
Mutual labels:  cache
Wheel
关于net nio os cache db rpc json web http udp tcp mq 等多个小工具的自定义实现
Stars: ✭ 45 (-6.25%)
Mutual labels:  cache
Laravel Couchbase
Couchbase providers for Laravel
Stars: ✭ 31 (-35.42%)
Mutual labels:  cache
Sst Elements
SST Architectural Simulation Components and Libraries
Stars: ✭ 36 (-25%)
Mutual labels:  cache
Python Diskcache
Python disk-backed cache (Django-compatible). Faster than Redis and Memcached. Pure-Python.
Stars: ✭ 992 (+1966.67%)
Mutual labels:  cache
Ecache
👏👏 Integrate cache(redis) [flask etc.] with SQLAlchemy.
Stars: ✭ 28 (-41.67%)
Mutual labels:  cache
Pomodoro
A simple WordPress translation cache
Stars: ✭ 47 (-2.08%)
Mutual labels:  cache
Exchange Rates
💱 Querying a rate-limited currency exchange API using Redis as a cache
Stars: ✭ 37 (-22.92%)
Mutual labels:  cache
Extended image
A powerful official extension library of image, which support placeholder(loading)/ failed state, cache network, zoom pan image, photo view, slide out page, editor(crop,rotate,flip), paint custom etc.
Stars: ✭ 1,021 (+2027.08%)
Mutual labels:  cache
Easyandroid
一个完整基于kotlin的安卓开发框架,采用了mvvm设计模式。涵盖了: 1、基于retrofit2封装的通过kotlin协程实现的网络框架 2、基于阿里开源router修改的api-router实现项目模块化 3、基于glide的图片加载缓存框架 4、基于room实现的往来数据缓存加载 5、基于step实现的数据异步提交 6、基于PreferenceHolder实现的本地数据快速存储 7、基于mlist实现的简单复杂列表的快速开发扩展 8、定制的toolbar可以自适应异形屏,挖孔屏,水滴屏等等。。 本框架几乎涵盖了开发所需的所有模块组件。简单fork之后就可以基于框架快速开发。
Stars: ✭ 33 (-31.25%)
Mutual labels:  cache
Htmlcache
Laravel middleware to cache the rendered html
Stars: ✭ 35 (-27.08%)
Mutual labels:  cache
Synchrotron
Caching layer load balancer.
Stars: ✭ 42 (-12.5%)
Mutual labels:  cache
Apicache
Simple API-caching middleware for Express/Node.
Stars: ✭ 957 (+1893.75%)
Mutual labels:  cache
Lfscache
LFS Cache is a caching Git LFS proxy.
Stars: ✭ 45 (-6.25%)
Mutual labels:  cache
Zache
Zero-footprint Ruby In-Memory Thread-Safe Cache
Stars: ✭ 30 (-37.5%)
Mutual labels:  cache
Spring Boot
spring-boot 项目实践总结
Stars: ✭ 989 (+1960.42%)
Mutual labels:  cache
Redis Tag Cache
Cache and invalidate records in Redis with tags
Stars: ✭ 48 (+0%)
Mutual labels:  cache
Cache
A promise-aware caching API for Amp.
Stars: ✭ 46 (-4.17%)
Mutual labels:  cache
Rxnetwork
A swift network library based on Moya/RxSwift.
Stars: ✭ 43 (-10.42%)
Mutual labels:  cache

cachelper ##########

.. image:: https://travis-ci.org/suzaku/cachelper.svg?branch=master :target: https://travis-ci.org/suzaku/cachelper .. image:: https://img.shields.io/pypi/v/cachelper.svg :target: https://pypi.python.org/pypi/cachelper .. image:: https://bettercodehub.com/edge/badge/suzaku/cachelper?branch=master

Useful cache helpers in one package!

Install


.. code-block:: bash

pip install cachelper

Helpers


In memory cache

memoize

Caching function return values in memory.

.. code-block:: python

import cachelper

@cachelper.memoize()
def fibo(n):
    if n in (0, 1):
        return 1
    return fibo(n - 1) + fibo(n - 2)

fibo(10)

Cache with Redis/Memcached

Using the HelperMixin

HelperMixin can be used with any cache client classes that implement the following methods:

  • def get(self, key)
  • def set(self, key, value, timeout=None)
  • def delete(self, key)
  • def get_many(self, keys)
  • def set_many(self, mapping, timeout=None)

For example, RedisCache from werkzeug is one such class:

.. code-block:: python

from redis import StrictRedis
from werkzeug.contrib.cache import RedisCache as _RedisCache

from cachelper import HelperMixin

class RedisCache(_RedisCache, HelperMixin):
    '''werkzeug.contrib.cache.RedisCache mixed with HelperMixin'''

    def get_many(self, keys):
        return super().get_many(*keys)

rds = StrictRedis()
cache = RedisCache(rds)

This mixin defines these methods: call, map, __call__. If your class already defines methods of the same name, the mixin methods may not work correctly.

cache decorator

Add cache by decorating a function or method.

.. code-block:: python

@cache("key-{user_id}", timeout=300)
def get_name(user_id):
    # Fetch user name from database
    ...

The cache key template can also be a function which acts as a key factory:

.. code-block:: python

def name_key(user_id):
    return "key-%s" % user_id

@cache(name_key, timeout=300)
def get_name(user_id):
    # Fetch user name from database
    ...

Just make sure the key factory function accepts the same parameters as the cached function and returns the key.

cached function calls

Sometimes we don't want to cache all calls to a specific function. So the decorator is not suitable, we may cache the call instead the function in this case:

.. code-block:: python

def get_name(user_id):
    # Fetch user name from database
    ...

user_id = 42
key = "key-{user_id}".format(user_id=user_id)
cache.call(lambda: get_name(user_id), key, timeout=300)

cached multiple calls

For most cache backends, it's much faster to get or set caches in bulk.

.. code-block:: python

def get_name(user_id):
    # Fetch user name from database
    ...

user_ids = [1, 2, 42, 1984]
names = cache.map("key-{user_id}", get_name, user_ids, timeout=300)

.. image:: https://app.codesponsor.io/embed/MY7qFCdB7bDgiBqdjtV9ASYi/suzaku/cachelper.svg :width: 888px :height: 68px :alt: Sponsor :target: https://app.codesponsor.io/link/MY7qFCdB7bDgiBqdjtV9ASYi/suzaku/cachelper

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].