[Python Tips] Expiring Dict

in #programming6 years ago

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

Sort:  

thank you for the education-truly appreciated

que bella publicación

"This data will self-destruct in 60 seconds"

I didn't even know you could do things like that with data. What would be a typical application for that?

There are a lot of useful situations, for example, I have dashboards I create to monitor what's going on. I will cache the data for an hour rather than continuously hit the database.

Whenever you have a lot of access to the data, but the data doesn't need to be up to the second then caching is a good option.

Thanks for sharing great info

Very beautiful photos I really love to take pictures of nature let's be friends. Welcome to Steemit.