1

I have a scenario where I want to cache a JSON response and use it further. I have total two requests out of which one I want to cache and use the response in another request, however the other request should not be cached. As of now what I have tried cache's all the requests. Here is what I have tried :

import requests
import requests_cache
requests_cache.install_cache('test_cache', expire_after=120)
r = requests.get('http://localhost:5000/')
print(r.content)
r1 = requests.get('http://localhost:5000/nocach')
print(r1.content)

Here I want only the requests should be cached for r and not for r1 .

Is there any other way that supports my scenario as of now I am using requests-cache which caches all requests , however my desired scenario woudl be not to cache all request but the ones that I want to be cached for specific time.

How can I achieve this any help ?

2 Answers 2

4

There is a feature in requests_cache to temporarily disable the cache feature. This is the method .disabled(). In the following snippet I use the with keyword to create the temporary scope in which the requests are not cached.

import requests
import requests_cache
requests_cache.install_cache('test_cache', expire_after=120)
r = requests.get('http://localhost:5000/')
print(r.content)
with requests_cache.disabled():
    r1 = requests.get('http://localhost:5000/nocach')
    print(r1.content)

Additionally, you can add a check if it was fetched from cache with the attribute from_cache

r1 = requests.get('http://localhost:5000/nocach')
print( hasattr(r1, 'from_cache') )

wich should return False if it was placed in the disabled cache code context

Sign up to request clarification or add additional context in comments.

7 Comments

Worked as a charm thanks !! but would using wit statements lead to slower code execution in python ?
Great! No, with is just a control-flow mechanism and can be likened to a if-else block.
Hi, can you please tell me how can i access the cached data.
@NeedToCodeAgain sure, try print(requests_cache.get_cache()) print('Cached URLS:') print('\n'.join(requests_cache.get_cache().urls))
Thank you @pastaleg for the prompt response, actualy i want to save "r.text" which is in json to cache and in the second api request i want to access the cache. I tried but not able to figure out how i can do this.
|
2

Option 1: CachedSession

Alternatively, you can use requests_cache.CachedSession to cache requests instead of install_cache(), and a regular requests.Session (or just requests.get()) for non-cached requests:

import requests
from requests_cache import CachedSession

session = CachedSession('test_cache', expire_after=120)

# Cached request
r = session.get('http://localhost:5000/')
print(r)

# Non-cached request
r1 = requests.get('http://localhost:5000/nocache')
print(r1)

Option 2: URL patterns

Another option, as of requests-cache 0.7+, is to use URL glob patterns to define which requests to cache. This works with both CachedSession and install_cache(), and is especially useful if you want to set a different expiration for different hosts.

from requests_cache import CachedSession

urls_expire_after = {
    'https://httpbin.org/get': 120,   # Cache for 2 minutes
    'https://httpbin.org/delay': -1,  # Cache indefinitely
    'https://httpbin.org/ip': 0,      # Don't cache
}
session = CachedSession('test_cache', urls_expire_after=urls_expire_after)

# Cached request with expiration
r = session.get('https://httpbin.org/get')
print(r)

# Cached request with no expiration
r1 = session.get('https://httpbin.org/delay/1')
print(r1)

# Non-cached request
r2 = session.get('https://httpbin.org/ip')
print(r2)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.