[Python Tips] Expiring Dict

By @themarkymark6/10/2018programming

Expiring Dict

Expiring dict is a very useful library for caching data via ordered dictionaries. It works similar to a regular dictionary but will cache data for a short period of time. It can be used as a simple caching layer where you don't want to get into something like Redis.

Installation

pip install expiringdict

Using expiring dict

Creating expiring dict
from expiringdict import ExpiringDict
cache = ExpiringDict(max_length=50, max_age_seconds=60)

This will create an expiring dict that contains a max of 50 elements and expires in 60 seconds.

Add data

cache["follows"] = "35"

Retrieve data

cache.get("key")

Summary

Expiring dict is an easy to use library that can give you a quick and easy tool for caching data for short periods of time. If you need more complex caching, you will want to look at a full framework like Redis. Expiring dict has support for Redis directly.

My Python Tips Series

178

comments