Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

A collection of cache helpers

Project Description
<a href="https://app.codesponsor.io/link/MY7qFCdB7bDgiBqdjtV9ASYi/suzaku/cachelper" rel="nofollow"><img src="https://app.codesponsor.io/embed/MY7qFCdB7bDgiBqdjtV9ASYi/suzaku/cachelper.svg" style="width: 888px; height: 68px;" alt="Sponsor" /></a>

cachelper
==========

[![Build Status](https://travis-ci.org/suzaku/cachelper.svg?branch=master)](https://travis-ci.org/suzaku/cachelper)

Useful cache helpers in one package!

## Install

```bash
pip install cachelper
```

## Helpers

### In memory cache

#### memoize

Caching function return values in memory.


```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

#### cache decorator

Add cache by decorating a function or method.

```python
from redis import StrictRedis
import cachelper

rds = StrictRedis()
cache = cachelper.RedisCache(rds)

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

The `RedisCache` used in the example above is a subclass of the [werkzeug one](http://werkzeug.pocoo.org/docs/0.12/contrib/cache/#werkzeug.contrib.cache.RedisCache).
It's just a mixin of the werkzeug implementation and `cachelper.HelperMixin`.


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

You may use this mixin to create cache class of your own, as long as the following methods are provided:

- `get(key)`
- `set(key, value, timeout)`

#### 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:


```python
from redis import StrictRedis
import cachelper

rds = StrictRedis()
cache = cachelper.RedisCache(rds)

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.

```python
from redis import StrictRedis
import cachelper

rds = StrictRedis()
cache = cachelper.RedisCache(rds)

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)
```
Release History

Release History

History Node

0.3.0

History Node

0.2.0

History Node

0.1.5

This version
History Node

0.1.4

History Node

0.1.3

History Node

0.1.2

History Node

0.1.1

History Node

0.1.0

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
cachelper-0.1.4.tar.gz (3.5 kB) Copy SHA256 Checksum SHA256 Source Aug 30, 2017

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting