skip to navigation
skip to content

Not Logged In

cache_it 0.0.1

Decorators for caching

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'}


```
 
File Type Py Version Uploaded on Size
cache_it-0.0.1-py2.7.egg (md5) Python Egg 2.7 2014-06-17 7KB
cache_it-0.0.1.macosx-10.9-intel.tar.gz (md5)
built for Darwin-13.2.0
"dumb" binary any 2014-06-17 6KB
cache_it-0.0.1.tar.gz (md5) Source 2014-06-17 3KB
  • Downloads (All Versions):
  • 0 downloads in the last day
  • 31 downloads in the last week
  • 252 downloads in the last month
  • Author: Shoji Ihara
  • Home Page: http://github.com/shoz/cache_it
  • Keywords: memcached,test,testing,decorators
  • License:
    switchcache
    The MIT License
    Copyright (c) 2014 Shoji Ihara
    
    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files (the
    "Software"), to deal in the Software without restriction, including
    without limitation the rights to use, copy, modify, merge, publish,
    distribute, sublicense, and/or sell copies of the Software, and to
    permit persons to whom the Software is furnished to do so, subject to
    the following conditions:
    
    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.
    
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  • Categories
  • Package Index Owner: ihara
  • DOAP record: cache_it-0.0.1.xml