-2

I have never dealt with cache in my life, but I would like to start now.

I have a python program that gets results from a website based on a query.

Problem: to make this query, the website takes ages to load.

Wanted Result: make the query once, store the result somehow, and retrieve it if query presents itself a second time in a period of X minutes, otherwise it expires.

How is this achieved in python?

Nathan
  • 1,135
  • 2
  • 12
  • 27
Con7e
  • 225
  • 4
  • 20
  • What are you trying to achieve? Unfortunately your question is a bit unclear. – ρss Jun 03 '14 at 20:51
  • Get the result and store in `redis` and next time read from the `redis`? – pynovice Jun 03 '14 at 20:53
  • Use memcached instead. It is readily across most of O/S. http://stackoverflow.com/questions/868690/good-examples-of-python-memcache-memcached-being-used-in-python – Chhavi Gangwal Jun 05 '14 at 19:30

1 Answers1

0

Your best bet is to use redis. It's a very dynamic and powerful key value store. First, install redis on your system then install redispy via pip. Then let's jump to the code:

r = redis.StrictRedis(host='localhost', port=6379, db=0) # Make the connection with redis
query = r.get('data')  # Try to get the data from redis
if query:
   # If data exist from redis
   # Pass your data wherever you want
else:
   # If data doesn't exist in the redis
   # Get the data 
   data_from_query = **your_data_from_query**
   r.set('data', data_from_query) #Store the data in redis
   r.expire('data', 10) #Make data expire in 10 seconds
pynovice
  • 7,424
  • 25
  • 69
  • 109