Marky avatar

[Python Tips] Expiring Dict

themarkymark

Published: 10 Jun 2018 › Updated: 10 Jun 2018[Python Tips] Expiring Dict

[Python Tips] Expiring Dict

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

Leave [Python Tips] Expiring Dict to:

Written by

Browncoat | Meme Connoisseur | Bitcoin Evangelist | Dev | Gamer | Technical Samurai | AI Nerd | Hive Witness | Black Belt in Dad Jokes

Read more #programming posts


Best Posts From Marky

We have not curated any of themarkymark's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Marky