Skip to content

LRU cache for Python. Use Redis as backend. Provides a dictionary-like object as well as a method decorator. pip install redis-lru

License

Notifications You must be signed in to change notification settings

leohowell/redis-lru

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

redis-lru

Installation

pip install redis-lru

Introduction

It's often useful to have an lru redis cache. Of course, it's also desirable not to have the cache grow too large, and cache expiration is often desirable. This module provides such a cache.

redis-lru supports CPython 3.4+

For the most part, you can just use it like this:

import redis
from redis_lru import RedisLRU

client = redis.StrictRedis()
cache = RedisLRU(client)

@cache
def f(x):
    print("Calling f({})".format(x))
    return x


f(3) # This will print "Calling f(3)", will return 3
f(3) # This will not print anything, but will return 3 (unless 15 minutes have passed between the first and second function call).

Additionally a datetime.time object can be provided to clear the cache at a specific time of the day:

@cache(expire_on=datetime.time(hour=8)) # clear at 08:00 o'clock
def b(x):
    print("Calling f({})".format(x))
    return x

About

LRU cache for Python. Use Redis as backend. Provides a dictionary-like object as well as a method decorator. pip install redis-lru

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages