Decorators for caching
Project description
cache_it
====
__cache_it__ is a decorator which wraps caching(read/write) routine.
Decorated functions automatically access your memcache and get/set values depending on the situation.
All you have to do is decorate your function by __@cache_it__.
## Basic Usage
```python
>>> from cache_it import init_cache, cache_it
>>> init_cache(['127.0.0.1:11211'])
>>> @cache_it(prefix='PREFIX:')
>>> def cached_function(key):
>>> return 'value'
>>> cached_function('key')
'value' # cached => key:'PREFIX:key' value:'value'
>>> cached_function('key')
'value' # getting from a cache
```
## As a method
You can decorate not only functions but also methods in the same way.
```python
class User(Document)
@cache_it(prefix='PREFIX')
def __getitem__(self, key):
return user[key]
```
## ignore_exception (False by default)
If you want to ignore exceptions occured when accessing memcache, you have to set "ignore_exception" into True.
```python
@cache_it(prefix='PREFIX', ignore_exception=True)
def cached_function(key):
return 'value'
```
## ttl (24*60*60 sec by default)
```python
@cache_it(prefix='PREFIX', ttl=60) # 60sec
def cached_function(key):
return 'value'
```
## encoder/decoder
Encoders are called before setting a value.
Decoders, on the other hand, are called after getting a value.
```python
@cache_it(prefix='PREFIX',
encoder=lambda x: x['foo'],
decoder=lambda x: {'foo': x})
def cached_function(key):
return {'foo': 'bar'}
```
====
__cache_it__ is a decorator which wraps caching(read/write) routine.
Decorated functions automatically access your memcache and get/set values depending on the situation.
All you have to do is decorate your function by __@cache_it__.
## Basic Usage
```python
>>> from cache_it import init_cache, cache_it
>>> init_cache(['127.0.0.1:11211'])
>>> @cache_it(prefix='PREFIX:')
>>> def cached_function(key):
>>> return 'value'
>>> cached_function('key')
'value' # cached => key:'PREFIX:key' value:'value'
>>> cached_function('key')
'value' # getting from a cache
```
## As a method
You can decorate not only functions but also methods in the same way.
```python
class User(Document)
@cache_it(prefix='PREFIX')
def __getitem__(self, key):
return user[key]
```
## ignore_exception (False by default)
If you want to ignore exceptions occured when accessing memcache, you have to set "ignore_exception" into True.
```python
@cache_it(prefix='PREFIX', ignore_exception=True)
def cached_function(key):
return 'value'
```
## ttl (24*60*60 sec by default)
```python
@cache_it(prefix='PREFIX', ttl=60) # 60sec
def cached_function(key):
return 'value'
```
## encoder/decoder
Encoders are called before setting a value.
Decoders, on the other hand, are called after getting a value.
```python
@cache_it(prefix='PREFIX',
encoder=lambda x: x['foo'],
decoder=lambda x: {'foo': x})
def cached_function(key):
return {'foo': 'bar'}
```
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
cache_it-0.0.1.tar.gz
(4.0 kB
view details)
Built Distributions
cache_it-0.0.1-py2.7.egg
(8.1 kB
view details)
File details
Details for the file cache_it-0.0.1.tar.gz
.
File metadata
- Download URL: cache_it-0.0.1.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 599f32774aa8cf102016a77e09c4f5bbb81edf6c276b631fe8594e9fc48a7a5c |
|
MD5 | 9c82bb92085b87b61748166a2b9ce44c |
|
BLAKE2b-256 | 15be67d8872a21c9ded3d0753639ad02833a75f4544be358229c7e03ddcd254d |
File details
Details for the file cache_it-0.0.1.macosx-10.9-intel.tar.gz
.
File metadata
- Download URL: cache_it-0.0.1.macosx-10.9-intel.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d26266ac7b04b935e9ad6e4ee693d21f2145cd99ee18ee296791e25aa0bdd6ab |
|
MD5 | 71dbb69c9eda30752e2ee0c8f9b08787 |
|
BLAKE2b-256 | 46483859ec0960e0327f485177483b2d994e14036e23a0482a7b5cd586664d53 |
File details
Details for the file cache_it-0.0.1-py2.7.egg
.
File metadata
- Download URL: cache_it-0.0.1-py2.7.egg
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 39ae8a6df9b7ae7a7f8afd79ab4d7f58fae5732cd26dbe2485eda0f1af176b26 |
|
MD5 | 0da67966a186583c2650c23ee23b1bfb |
|
BLAKE2b-256 | 35ba14f5a6ca1e58813c22083496a420488d3155c842844baeec815d2d5d486b |