question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Falling Back to Cache if 404

See original GitHub issue

Is there a recognised pattern or method for using the cache if the live page 404s or there is no network connection?

For example, one way might be to try to get the page without the cache, and if that fails, then try to use the cache:

try:
    with requests_cache.disabled():
        r=requests.get('http://httpbin.org/ip')
        if r.status_code!= requests.codes.ok:
            raise ValueError()
        #If this is good, add it to the cache somehow?
        #...
except:
    r=requests.get('http://httpbin.org/ip')

print(getattr(r, 'from_cache', False),r.text)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:6

github_iconTop GitHub Comments

1reaction
JWCookcommented, Apr 22, 2021

Fyi, changes for the first option I mentioned (manually saving a response) are now in the master branch and in pre-release build 0.7.0.dev255. You can use CachedSession.cache.save_response(response) to manually save a response object.

The second option (adding a raise_for_status option) will be done in issue #210. I’ll close this issue for now, then, unless there’s a different way you would prefer to handle 404s.

1reaction
yakutovichacommented, May 16, 2019

In case someone else has similar issue as mine, here is the solution I came up with:

import requests
import requests_cache
from IPython.lib import backgroundjobs as bg

def update_cache():
    """Run this process asynchronously"""
    requests_cache.install_cache(cache_name='apps_meta', backend='sqlite', expire_after=1, old_data_on_error=True) # Make cache expired
    requests.get(registry_url) # Update cache
    requests_cache.install_cache(cache_name='apps_meta', backend='sqlite') # Return to initial setup


requests_cache.install_cache(cache_name='apps_meta', backend='sqlite') # at start getting data from cache
update_cache_background = bg.BackgroundJobFunc(update_cache) # create cache update job
update_cache_background.start() # start cache update in the background
Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting "no-cache" for 404 files · Issue #245 · evansd/whitenoise
I'm thinking on how to tackle this and I see two possibilities: a custom middleware that sees if the 404 has STATIC_URL as...
Read more >
Why 404.html causes uncaught error during service worker ...
This means the service worker will only install if all of the resources in cache.addAll have been cached. Firebase returns 404 Not Found...
Read more >
ERROR 404 after a while, clear cache fixes it - Drupal
After a while I get suddenly a 404 error page instead of my node pages (no field added, type=page) in the default language...
Read more >
Facing 404 error. It broke my website. - WordPress.org
The homepage is fetching deleted cached css file which gives 404 error. ... AO FAQ for more info. if it is active be...
Read more >
Dynamic Routes Details - Next.js
If fallback is false , then any paths not returned by getStaticPaths will result in a 404 page. If fallback is true ,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found