All Projects → aio-libs → Aiocache

aio-libs / Aiocache

Licence: bsd-3-clause
Asyncio cache manager for redis, memcached and memory

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Aiocache

Cache
Cache library
Stars: ✭ 310 (-37.5%)
Mutual labels:  redis, cache, memcached
Senparc.co2net
支持 .NET Framework & .NET Core 的公共基础扩展库
Stars: ✭ 289 (-41.73%)
Mutual labels:  redis, cache, memcached
Easycaching
💥 EasyCaching is an open source caching library that contains basic usages and some advanced usages of caching which can help us to handle caching more easier!
Stars: ✭ 1,047 (+111.09%)
Mutual labels:  redis, cache, memcached
Weixinmpsdk
微信全平台 SDK Senparc.Weixin for C#,支持 .NET Framework 及 .NET Core、.NET 6.0。已支持微信公众号、小程序、小游戏、企业号、企业微信、开放平台、微信支付、JSSDK、微信周边等全平台。 WeChat SDK for C#.
Stars: ✭ 7,098 (+1331.05%)
Mutual labels:  redis, cache, memcached
Ring
Python cache interface with clean API and built-in memcache & redis + asyncio support.
Stars: ✭ 404 (-18.55%)
Mutual labels:  asyncio, redis, cache
Overlord
Overlord是哔哩哔哩基于Go语言编写的memcache和redis&cluster的代理及集群管理功能,致力于提供自动化高可用的缓存服务解决方案。
Stars: ✭ 1,884 (+279.84%)
Mutual labels:  redis, cache, memcached
Sequelize Transparent Cache
Simple to use and universal cache layer for Sequelize
Stars: ✭ 137 (-72.38%)
Mutual labels:  redis, cache, memcached
Cachego
Golang Cache component - Multiple drivers
Stars: ✭ 148 (-70.16%)
Mutual labels:  redis, cache, memcached
Ansible Role Redis
Ansible Role - Redis
Stars: ✭ 176 (-64.52%)
Mutual labels:  redis, cache, memcached
Cachemanager
CacheManager is an open source caching abstraction layer for .NET written in C#. It supports various cache providers and implements many advanced features.
Stars: ✭ 2,049 (+313.1%)
Mutual labels:  redis, cache, memcached
Fastapi Plugins
FastAPI framework plugins
Stars: ✭ 104 (-79.03%)
Mutual labels:  asyncio, redis, memcached
Phpfastcache
A high-performance backend cache system. It is intended for use in speeding up dynamic web applications by alleviating database load. Well implemented, it can drops the database load to almost nothing, yielding faster page load times for users, better resource utilization. It is simple yet powerful.
Stars: ✭ 2,171 (+337.7%)
Mutual labels:  redis, cache, memcached
Scrapbook
PHP cache library, with adapters for e.g. Memcached, Redis, Couchbase, APC(u), SQL and additional capabilities (e.g. transactions, stampede protection) built on top.
Stars: ✭ 279 (-43.75%)
Mutual labels:  redis, cache, memcached
Koa Redis
Redis storage for Koa session middleware/cache with Sentinel and Cluster support
Stars: ✭ 324 (-34.68%)
Mutual labels:  redis, cache
Gokv
Simple key-value store abstraction and implementations for Go (Redis, Consul, etcd, bbolt, BadgerDB, LevelDB, Memcached, DynamoDB, S3, PostgreSQL, MongoDB, CockroachDB and many more)
Stars: ✭ 314 (-36.69%)
Mutual labels:  redis, memcached
Dotnetguide
🦸【C#/.NET/.NET Core学习、工作、面试指南】概述:C#/.NET/.NET Core基础知识,学习资料、文章、书籍,社区组织,工具和常见的面试题总结。以及面试时需要注意的事项和优秀简历编写技巧,希望能和大家一起成长进步👊。【让现在的自己不再迷漫✨】
Stars: ✭ 308 (-37.9%)
Mutual labels:  redis, memcached
Hibernate Redis
hibernate 2nd level cache privder using redis
Stars: ✭ 345 (-30.44%)
Mutual labels:  redis, cache
Cache
Cache library with Redis backend for Golang
Stars: ✭ 337 (-32.06%)
Mutual labels:  redis, cache
Memtier benchmark
NoSQL Redis and Memcache traffic generation and benchmarking tool.
Stars: ✭ 480 (-3.23%)
Mutual labels:  redis, memcached
Ssm booksystem
ssm demo,ssm详细教程,SSM简明教程:简单的十步教你搭建人生第一个SSM框架[ SSM框架整合教程(spring+spring mvc+mybatis+redis+maven+idea+bootstrap) ]
Stars: ✭ 355 (-28.43%)
Mutual labels:  redis, cache

aiocache ########

Asyncio cache supporting multiple backends (memory, redis and memcached).

.. image:: https://travis-ci.org/argaen/aiocache.svg?branch=master :target: https://travis-ci.org/argaen/aiocache

.. image:: https://codecov.io/gh/argaen/aiocache/branch/master/graph/badge.svg :target: https://codecov.io/gh/argaen/aiocache

.. image:: https://badge.fury.io/py/aiocache.svg :target: https://pypi.python.org/pypi/aiocache

.. image:: https://img.shields.io/pypi/pyversions/aiocache.svg :target: https://pypi.python.org/pypi/aiocache

.. image:: https://api.codacy.com/project/badge/Grade/96f772e38e63489ca884dbaf6e9fb7fd :target: https://www.codacy.com/app/argaen/aiocache

.. image:: https://img.shields.io/badge/code%20style-black-000000.svg :target: https://github.com/ambv/black

This library aims for simplicity over specialization. All caches contain the same minimum interface which consists on the following functions:

  • add: Only adds key/value if key does not exist.
  • get: Retrieve value identified by key.
  • set: Sets key/value.
  • multi_get: Retrieves multiple key/values.
  • multi_set: Sets multiple key/values.
  • exists: Returns True if key exists False otherwise.
  • increment: Increment the value stored in the given key.
  • delete: Deletes key and returns number of deleted items.
  • clear: Clears the items stored.
  • raw: Executes the specified command using the underlying client.

.. role:: python(code) :language: python

.. contents::

.. section-numbering:

Installing

  • pip install aiocache
  • pip install aiocache[redis]
  • pip install aiocache[memcached]
  • pip install aiocache[redis,memcached]
  • pip install aiocache[msgpack]

Usage

Using a cache is as simple as

.. code-block:: python

>>> import asyncio
>>> loop = asyncio.get_event_loop()
>>> from aiocache import Cache
>>> cache = Cache(Cache.MEMORY) # Here you can also use Cache.REDIS and Cache.MEMCACHED, default is Cache.MEMORY
>>> loop.run_until_complete(cache.set('key', 'value'))
True
>>> loop.run_until_complete(cache.get('key'))
'value'

Or as a decorator

.. code-block:: python

import asyncio

from collections import namedtuple

from aiocache import cached, Cache
from aiocache.serializers import PickleSerializer
# With this we can store python objects in backends like Redis!

Result = namedtuple('Result', "content, status")


@cached(
    ttl=10, cache=Cache.REDIS, key="key", serializer=PickleSerializer(), port=6379, namespace="main")
async def cached_call():
    print("Sleeping for three seconds zzzz.....")
    await asyncio.sleep(3)
    return Result("content", 200)


def run():
    loop = asyncio.get_event_loop()
    loop.run_until_complete(cached_call())
    loop.run_until_complete(cached_call())
    loop.run_until_complete(cached_call())
    cache = Cache(Cache.REDIS, endpoint="127.0.0.1", port=6379, namespace="main")
    loop.run_until_complete(cache.delete("key"))

if __name__ == "__main__":
    run()

The recommended approach to instantiate a new cache is using the Cache constructor. However you can also instantiate directly using aiocache.RedisCache, aiocache.SimpleMemoryCache or aiocache.MemcachedCache.

You can also setup cache aliases so its easy to reuse configurations

.. code-block:: python

import asyncio

from aiocache import caches

You can use either classes or strings for referencing classes

caches.set_config({ 'default': { 'cache': "aiocache.SimpleMemoryCache", 'serializer': { 'class': "aiocache.serializers.StringSerializer" } }, 'redis_alt': { 'cache': "aiocache.RedisCache", 'endpoint': "127.0.0.1", 'port': 6379, 'timeout': 1, 'serializer': { 'class': "aiocache.serializers.PickleSerializer" }, 'plugins': [ {'class': "aiocache.plugins.HitMissRatioPlugin"}, {'class': "aiocache.plugins.TimingPlugin"} ] } })

async def default_cache(): cache = caches.get('default') # This always returns the SAME instance await cache.set("key", "value") assert await cache.get("key") == "value"

async def alt_cache(): cache = caches.create('redis_alt') # This creates a NEW instance on every call await cache.set("key", "value") assert await cache.get("key") == "value"

def test_alias(): loop = asyncio.get_event_loop() loop.run_until_complete(default_cache()) loop.run_until_complete(alt_cache())

  loop.run_until_complete(caches.get('redis_alt').delete("key"))

if name == "main": test_alias()

How does it work

Aiocache provides 3 main entities:

  • backends: Allow you specify which backend you want to use for your cache. Currently supporting: SimpleMemoryCache, RedisCache using aioredis_ and MemCache using aiomcache_.
  • serializers: Serialize and deserialize the data between your code and the backends. This allows you to save any Python object into your cache. Currently supporting: StringSerializer, PickleSerializer, JsonSerializer, and MsgPackSerializer. But you can also build custom ones.
  • plugins: Implement a hooks system that allows to execute extra behavior before and after of each command.

If you are missing an implementation of backend, serializer or plugin you think it could be interesting for the package, do not hesitate to open a new issue.

.. image:: docs/images/architecture.png :align: center

Those 3 entities combine during some of the cache operations to apply the desired command (backend), data transformation (serializer) and pre/post hooks (plugins). To have a better vision of what happens, here you can check how set function works in aiocache:

.. image:: docs/images/set_operation_flow.png :align: center

Amazing examples

In examples folder <https://github.com/argaen/aiocache/tree/master/examples>_ you can check different use cases:

  • Sanic, Aiohttp and Tornado <https://github.com/argaen/aiocache/tree/master/examples/frameworks>_
  • Python object in Redis <https://github.com/argaen/aiocache/blob/master/examples/python_object.py>_
  • Custom serializer for compressing data <https://github.com/argaen/aiocache/blob/master/examples/serializer_class.py>_
  • TimingPlugin and HitMissRatioPlugin demos <https://github.com/argaen/aiocache/blob/master/examples/plugins.py>_
  • Using marshmallow as a serializer <https://github.com/argaen/aiocache/blob/master/examples/marshmallow_serializer_class.py>_
  • Using cached decorator <https://github.com/argaen/aiocache/blob/master/examples/cached_decorator.py>_.
  • Using multi_cached decorator <https://github.com/argaen/aiocache/blob/master/examples/multicached_decorator.py>_.

Documentation

  • Usage <http://aiocache.readthedocs.io/en/latest>_
  • Caches <http://aiocache.readthedocs.io/en/latest/caches.html>_
  • Serializers <http://aiocache.readthedocs.io/en/latest/serializers.html>_
  • Plugins <http://aiocache.readthedocs.io/en/latest/plugins.html>_
  • Configuration <http://aiocache.readthedocs.io/en/latest/configuration.html>_
  • Decorators <http://aiocache.readthedocs.io/en/latest/decorators.html>_
  • Testing <http://aiocache.readthedocs.io/en/latest/testing.html>_
  • Examples <https://github.com/argaen/aiocache/tree/master/examples>_

.. _aioredis: https://github.com/aio-libs/aioredis .. _aiomcache: https://github.com/aio-libs/aiomcache

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