[Answer]-Django – Looking for a good way to cache a dictionary

1👍

Reading a file would work, but it will become slow as the number of requests per second start to increase.

Consider using Redis. It is a supercharged dictionary and cache that you can use to store key-value pairs and more (set, lists, strings, hashes). It runs primarily in memory but can also be stored to disk and reloaded.

For your case, every time a user searches for a city, you check Redis for key (city name as string or ID, however you wish), if it exists, use whatever value (co-ordinates?) it has for that city key, otherwise fall-back to geocoder and add a new key to Redis with its the result from geocoder.

There is also a django app for Redis to make it easier: https://niwibe.github.io/django-redis/

👤Kedar

Leave a comment