0

My idea is to create a hash of a queryset result. For example, product inventory.

Each update of this stock would generate a hash.

This use would be intended to only request this queryset in the API, when there is a change (example: a new product in invetory).

Example for this use:

  • no change, same hash - no request to get queryset
  • there was change, different hash. Then a request will be made.

This would be a feature designed for those who are consuming the data and not for the Django that is serving.

Does this make any sense? I saw that in python there is a way to generate a hash from a tuple, in my case it would be to use the frozenset and generate the hash. I don't know if it's a good idea.

yzakius
  • 25
  • 3
  • I don't understand entirely. Can you please write the flow of requests without mentioning "hash"? Such as 1. GET /products 2. POST (or PUT) /products/1 3. GET /products. The hash you mentioned, is it stored client-side? – Tom Wojcik Jul 12 '21 at 19:43

1 Answers1

0

I would comment, but I'm waiting on the 50 rep to be able to do that. It sounds like you're trying to cache results so you aren't querying on data that hasn't been changed. If you're not familiar with caching, the idea is to save hard-to-compute answers in memory for frequently queried endpoints/functions.

For example, if I had a program that calculated the first n digits of pi, I may choose to save a map of [digit count -> value] so that if 10 people asked me for the first thousand, I would only calculate it once. Redis is a popular option for caching, and I believe it exists for Django. It allows you to cache some information, set a time before expiration on it, and then wipe specific parts of that information (to force it to recalculate) every time something specific changes (like a new product in inventory).

Everybody should try writing their own cache at least once, like what you're describing, but the de facto professional option is to use a caching library. Your idea is good, it will definitely work, and you will probably want a dict of [hash->result] for each hash, where result is the information you would send back over your API. If you plan to save data so it persists across multiple program starts, remember Python forces random seeds for hashes, causing inconsistent values. Check out this post for more info.

Aaron
  • 132
  • 10