Skip to content
Python cache interface with clean API and built-in memcache & redis + asyncio support.
Branch: master
Clone or download



Join the chat at

Let's concentrate on code, not on storages.

Ring shows a way to control cache in point of view of code - not about storages. Ring's decorator is convenient but also keeps fluency for general scenarios.

asyncio support!

Take advantage of perfectly explicit and fully automated cache interface. Ring decorators convert your functions to cached version of them, with extra control methods.


Full documentation with examples and references:

  • Function/method support.
  • asyncio support.
  • Django support.
  • Bulk access support.

Function cache

import ring
import memcache
import requests

mc = memcache.Client([''])

# working for mc, expire in 60sec
@ring.memcache(mc, time=60)
def get_url(url):
    return requests.get(url).content

# normal way - it is cached
data = get_url('')

It is a normal smart cache flow.

But ring is different when you want to explicitly control it.

# delete the cache
# get cached data or None
data_or_none = get_url.get('')

# get internal cache key
key = get_url.key('')
# and access directly to the backend
direct_data = mc.get(key)

Method cache

import ring
import redis

rc = redis.StrictRedis()

class User(dict):
    def __ring_key__(self):
        return self['id']

    # working for rc, no expiration
    # using json coder for non-bytes cache data
    @ring.redis(rc, coder='json')
    def data(self):
        return self.copy()

    # parameters are also ok!
    @ring.redis(rc, coder='json')
    def child(self, child_id):
        return {'user_id': self['id'], 'child_id': child_id}

user = User(id=42, name='Ring')

# create and get cache
user_data =  # cached
user['name'] = 'Ding'
# still cached
cached_data =
assert user_data == cached_data
# refresh
updated_data =
assert user_data != updated_data

# id is the cache key so...
user2 = User(id=42)
# still hitting the same cache
assert updated_data ==


PyPI is the recommended way.

$ pip install ring
To browse versions and tarballs, visit:

To use memcached or redis, don't forget to install related libraries. For example: python-memcached, python3-memcached, pylibmc, redis-py, Django etc

It may require to install and run related services on your system too. Look for memcached and redis for your system.


See contributors list on:
You can’t perform that action at this time.