Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9019651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:49:52+00:00 2026-06-16T04:49:52+00:00

I’m attempting to make a Flask web application where you have to request the

  • 0

I’m attempting to make a Flask web application where you have to request the entirety of a non-local website and I was wondering if it was possible to cache it for the purposes of speeding things up, because the website does not change that often but I still want it to update the cache once a day or so.

Anyway, I looked it up and found Flask-Cache, which seemed to do what I wanted so I made appropriate changes to it, and came up with adding this:

from flask.ext.cache import Cache
[...]
cache = Cache()
[...]
cache.init_app(app)
[...]
@cache.cached(timeout=86400, key_prefix='content')
def get_content():
    return lxml.html.fromstring(urllib2.urlopen('http://WEBSITE.com').read())

and then I make a call from the functions that need the content to proceed like so:

content = get_content()

Now I’d expect it to reuse the cached lxml.html object everytime a call is made, but that’s not what I’m seeing. The id of the object changes every time a call is made and there’s no speed-up at all. So have I misunderstood what Flask-Cache does, or am I doing something wrong here? I’ve tried using the memoize decorator instead, I’ve tried decreasing the timeout or removing it all together but nothing seems to be making anything difference.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:49:54+00:00Added an answer on June 16, 2026 at 4:49 am

    The default CACHE_TYPE is null which gives you a NullCache – so you get no caching at all which is what you observe. The documentation does not make this explicit, but this line in the source of Cache.init_app does:

    self.config.setdefault('CACHE_TYPE', 'null')
    

    To actually employ some caching, initialise your Cache instance to use a proper cache.

    cache = Cache(config={'CACHE_TYPE': 'simple'})
    

    Aside: Note that SimpleCache is great for development and testing, and this example, but you shouldn’t use it in production. Something like MemCached or RedisCache would be much better

    Now, with an actual cache in place, you will run into the next problem. On the second call, the cached lxml.html object will be retrieved from the Cache, but it is broken because these objects are not cacheable. Stacktrace looks like this:

    Traceback (most recent call last):
      File "/home/day/.virtualenvs/so-flask/lib/python2.7/site-packages/flask/app.py", line 1701, in __call__
        return self.wsgi_app(environ, start_response)
      File "/home/day/.virtualenvs/so-flask/lib/python2.7/site-packages/flask/app.py", line 1689, in wsgi_app
        response = self.make_response(self.handle_exception(e))
      File "/home/day/.virtualenvs/so-flask/lib/python2.7/site-packages/flask/app.py", line 1687, in wsgi_app
        response = self.full_dispatch_request()
      File "/home/day/.virtualenvs/so-flask/lib/python2.7/site-packages/flask/app.py", line 1360, in full_dispatch_request
        rv = self.handle_user_exception(e)
      File "/home/day/.virtualenvs/so-flask/lib/python2.7/site-packages/flask/app.py", line 1358, in full_dispatch_request
        rv = self.dispatch_request()
      File "/home/day/.virtualenvs/so-flask/lib/python2.7/site-packages/flask/app.py", line 1344, in dispatch_request
        return self.view_functions[rule.endpoint](**req.view_args)
      File "/home/day/q12030403.py", line 20, in index
        return "get_content returned: {0!r}\n".format(get_content())
      File "lxml.etree.pyx", line 1034, in lxml.etree._Element.__repr__ (src/lxml/lxml.etree.c:41389)
    
      File "lxml.etree.pyx", line 881, in lxml.etree._Element.tag.__get__ (src/lxml/lxml.etree.c:39979)
    
      File "apihelpers.pxi", line 15, in lxml.etree._assertValidNode (src/lxml/lxml.etree.c:12306)
    
    AssertionError: invalid Element proxy at 3056741852
    

    So instead of caching the lxml.html object, you should just cache the simple string – the content of the website that you downloaded, and then reparse that to get a fresh lxml.html object every time. Your cache still helps as you don’t hit the other website every time. Here is a full program to demonstrate that solution which works:

    from flask import Flask
    from flask.ext.cache import Cache
    import time
    import lxml.html
    import urllib2
    
    app = Flask(__name__)
    
    cache = Cache(config={'CACHE_TYPE': 'simple'})
    cache.init_app(app)
    
    @cache.cached(timeout=86400, key_prefix='content')
    def get_content():
        app.logger.debug("get_content called")
    #    return lxml.html.fromstring(urllib2.urlopen('http://daybarr.com/wishlist').read())
        return urllib2.urlopen('http://daybarr.com/wishlist').read()
    
    @app.route("/")
    def index():
        app.logger.debug("index called")
        return "get_content returned: {0!r}\n".format(get_content())
    
    if __name__ == "__main__":
        app.run(debug=True)
    

    When I run the program, and make two requests to http://127.0.0.1:5000/, I get this output. Note that get_content is not called the second time, because the content is served from cache.

     * Running on http://127.0.0.1:5000/
     * Restarting with reloader
    --------------------------------------------------------------------------------
    DEBUG in q12030403 [q12030403.py:20]:
    index called
    --------------------------------------------------------------------------------
    --------------------------------------------------------------------------------
    DEBUG in q12030403 [q12030403.py:14]:
    get_content called
    --------------------------------------------------------------------------------
    127.0.0.1 - - [21/Dec/2012 00:03:28] "GET / HTTP/1.1" 200 -
    --------------------------------------------------------------------------------
    DEBUG in q12030403 [q12030403.py:20]:
    index called
    --------------------------------------------------------------------------------
    127.0.0.1 - - [21/Dec/2012 00:03:33] "GET / HTTP/1.1" 200 -
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.