Our application fetches the correct database server from a pool of database servers. So each query is really 2 queries, and they look like this:
- Fetch the correct DB server
- Execute the query
We do this so we can take DB servers online and offline as necessary, as well as for load-balancing.
But the first query seems like it could be cached to memory, so it only actually queries the database every 5 or 10 minutes or so.
What’s the best way to do this?
Thanks.
EDIT this is for a Pylons web application
Just make a cache(python dict) which stores the first query and return it everytime, clear the cache every N mins, for this you make a decorator or cache class e.g.
Output:
Now this decorator can be used on any function whose results are to be cached upto a time or may be upto a event, I would make this decorator a class , so that it can take how many seconds to wait before cache refresh, and also you can see how best to generate key.