Skip to content

Expire aims to make using cache as convenient as possible.

Notifications You must be signed in to change notification settings

howie6879/expire

Folders and files

NameName
Last commit message
Last commit date

Latest commit

0da5b3d · Jul 17, 2018

History

6 Commits
Mar 7, 2018
Jul 17, 2018
Mar 7, 2018
Mar 6, 2018
Mar 7, 2018
Mar 7, 2018
Mar 6, 2018
Mar 6, 2018

Repository files navigation

Expire

Build Status PyPI

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:

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.

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.

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'

Thanks

About

Expire aims to make using cache as convenient as possible.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages