[Catalyst] DB values that change very rarely

Dami Laurent (PJ) laurent.dami at justice.ge.ch
Thu Jan 10 20:45:06 GMT 2013


>-----Message d'origine-----
>De : Jesse Sheidlower [mailto:jester at panix.com]
>Envoyé : jeudi, 10. janvier 2013 16:16
>À : catalyst at lists.scsys.co.uk
>Objet : [Catalyst] DB values that change very rarely
>
>
>In one of my apps, I have a number of tables that contain values that
>change very rarely. 
[...]
>This means that almost every page generation hits the database a whole bunch of
>unnecessary times, and that all of my controllers are cluttered.
>
>This must be a fairly common problem. What's the best way to deal with
>it--both the desire to persist what is mostly static data, and to keep
>my controllers clean?

Hi Jesse,

First of all, you need a piece of information that tells you if the configuration data has changed. It can be a version number or a timestamp column, either associated to each table (if these tables change individually), or in a global "version table" that covers everything. Every time that a power user edits such data, your version number or timestamp must be updated. 

Or maybe all this configuration data that doesn't change very often could be stored, not in database, but in a config file (using YAML, XML, or Config::General). In that case you have the mtime of the file to tell you if the data has changed.

Based on this, your controller can use any of the CPAN caching modules to keep the data in memory; you still need to go to the datasource to check if the version number or timestamp has changed, but that's a much smaller request.

Another way would be to exploit the caching functionality of the browser itself. In that case, the configuration data should be at a separate URL, like /myApp/config.json. Every page should use client-side javascript to exploit the config.json data (for exemple for building the various OPTIONS of a SELECT). Each page of the application loads /myApp/config.json, either through a <script src="...">, or through an Ajax call.The controller that serves this config.json should implement conditional GET, i.e. read the "If-Modified-Since" or "If-None-Match" headers, ask the datasource for its timestamp, and return 302 NOT MODIFIED if the client tag is in sync with the server data. That's a client-server round trip, but it's very fast because there is almost no content to be exchanged. Furthermore, the advantage is that the /config.json data is shared among all pages of your app -- it only travelled once from the server to the client.

Hope this helps -- good luck,

 Laurent Dami



More information about the Catalyst mailing list