clubby789
clubby789

Reputation: 2733

Check age of cherrypy sessions data

I have a website using Cherrpy that gets some data from a database and saves it using sessions. However, this increases page load times when the data hasn't changed. Is there a way that I can check when session data was saved, and only fetch the data again if it was saved more than 30 seconds ago?

machines = getInfo.getMachines()
cherrypy.session['machines'] = json.dumps(machines)

I found a sessions timeout setting for Cherrypy, but as that is counted in minutes, it's too long for my purposes.

Upvotes: 0

Views: 35

Answers (1)

If you use fractions of a minute, it'll work:

class SessionEnabledRoot:
    """Example session enabled root node."""

    _cp_config = {
        'tools.sessions.on': True,
        'tools.sessions.timeout': 0.5,  # <-- Half of a minute
    }

    def index(self):
        """Handle queries against ``/``."""
        return "Hello from /"

    def hi(self):
        """Handle queries against ``/hi``."""
        return "Hello from /hi"

    def default(self):
        """Handle queries against ``/{{ anything }}``."""
        return "Hello from catch-all"


def main():
    """Initialize a web app and run a web server."""
    cherrypy.quickstart(SessionEnabledRoot())


__name__ == '__main__' and main()

Upvotes: 1

Related Questions