https://github.com/howie6879/expire
Expire aims to make using cache as convenient as possible.
https://github.com/howie6879/expire
cache memcached-cache memory-cache redis-cache
Last synced: 5 months ago
JSON representation
Expire aims to make using cache as convenient as possible.
- Host: GitHub
- URL: https://github.com/howie6879/expire
- Owner: howie6879
- Created: 2018-03-06T01:25:55.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-07-17T15:24:55.000Z (almost 7 years ago)
- Last Synced: 2024-10-29T08:30:36.447Z (6 months ago)
- Topics: cache, memcached-cache, memory-cache, redis-cache
- Language: Python
- Size: 9.77 KB
- Stars: 4
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Expire
[](https://travis-ci.org/howie6879/expire) 
## What is Expire
When you are writing a service, maybe you need to be able to save a piece of JSON data to your system's memory.
Expire aims to make using cache as convenient as possible.
There are three ways to create a cache, which are MemoryCache, RedisCache or MemcachedCache.
## How to Use?
### Installation
**Run:**
``` shell
pip install expirepip install git+https://github.com/howie6879/expire.git
```### Usage
**MemoryCache:** easy to configure, but it automatically destroys when the server is stopped.
``` python
from expire import CacheSetting, MemoryCachememory_cache = MemoryCache()
memory_cache.set('name', 'expire')
cache_ins = CacheSetting()
print(cache_ins.get('name'))# Output: expire
```**RedisCache:** stable but you have to install Redis.
``` python
from expire import Settings
from expire import RedisCache, cached, CacheSettingclass MySettings(Settings):
"""
Create custom configuration
"""
cache = {
# RedisCache, MemoryCache or MemcachedCache
'cache_class': RedisCache,
'cache_config': {
'host': '127.0.0.1',
'port': 6379,
'db': 0,
'password': None
},
# JsonSerializer, PickleSerializer or StrSerializer
'serializer': None
}@cached(**MySettings.cache, ttl=1000)
def parse(url, params=None, **kwargs):
return "{0}: {1}".format(url, 'hello')def cached_by_redis(key):
cache_ins = CacheSetting(MySettings)
return cache_ins.get(key)key = 'expire'
result = parse(url=key, dynamic_key=key)
print(result)
# Output 'expire: hello'
print(cached_by_redis(key))
# Output 'expire: hello'
```## Thanks
- [Toapi](https://github.com/gaojiuli/toapi)
- [aiocache](https://github.com/argaen/aiocache)