From 12f7446f05450f73d6af065f5fe7fc96cb715e99 Mon Sep 17 00:00:00 2001 From: howie6879 Date: Tue, 6 Mar 2018 10:06:30 +0800 Subject: [PATCH] Update README.md --- .gitignore | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 74 ++++++++++++++++++++++++++++++++++++- 2 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b5d580f --- /dev/null +++ b/.gitignore @@ -0,0 +1,105 @@ +.idea/ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class +.html/ +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +env27/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# dotenv +.env + +# virtualenv +.venv +venv/ +ENV/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +/tests/data.sqlite +/examples/toapi-pic/data.sqlite diff --git a/README.md b/README.md index b7619c0..7b9d9b8 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,76 @@ ![PyPI](https://img.shields.io/pypi/v/expire.svg) -Expire aims to make using cache as convenient as possible. \ No newline at end of file +## 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 expire + +pip 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, MemoryCache + +memory_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, CacheSetting + +class 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' +``` \ No newline at end of file