[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. Think of something like a category table in an e-commerce app--they're mostly static, but every now and then you need to change or add or delete a category. These occasional changes do, however, need to be made by a (privileged) user, rather than a developer, so I can't just put them in the config file and edit this and restart the app when necessary. Since I don't know when they might change, I don't know how I could cache them, because when a change is made it needs to take effect immediately. These tables are used on almost every request. The result is that most of my controllers end up looking something like this, or having something like this at the end of them: sub add : Local { my ( $self, $c ) = @_; $c-stash-{title} = 'Add a new lemma'; $c-stash-{batches} = $c-model('WordsDB::Batch')-search(); $c-stash-{statuses} = $c-model('WordsDB::Status')-search(); $c-stash-{regions} = $c-model('WordsDB::Region')-search(); $c-stash-{subjects} = $c-model('WordsDB::Subject')-search(); $c-stash-{template} = 'add.tt'; } 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? Thanks. Jesse Sheidlower ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] DB values that change very rarely
On Thu, Jan 10, 2013 at 10:15 AM, Jesse Sheidlower jes...@panix.com wrote: Since I don't know when they might change, I don't know how I could cache them, because when a change is made it needs to take effect immediately. These tables are used on almost every request. Use a cache that allows you to give names to cached objects. Store your category list in the cache under a name like global-category-list'. When ever you change the categories, you expire the named object. The you either let the next lookup regenerate the list, or regenerate it when the update is made to the list. All access to the cached data must be performed through a machanism that wraps the cache. Len. Did I hear you on how to do everything, talking about Crash Blossoms? -- lenja...@jaffesystems.com 614-404-4214 www.lenjaffe.com Proprietor: http://www.theycomewithcheese.com/ - An Homage to Fromage Perl Advent Planet http://www.lenjaffe.com/PerlAdventPlanet/ - Advent Calendars: Perlish and otherwise. Greenbar http://www.greenbartraining.org/: Grubmaster: 2012-2009, Grub Asst: 2008, Trained: 2007. ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
RE: [Catalyst] DB values that change very rarely
-Message d'origine- De : Jesse Sheidlower [mailto:jes...@panix.com] Envoyé : jeudi, 10. janvier 2013 16:16 À : catalyst@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 ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] sub-modules for model
On Wed, Dec 19, 2012 at 12:53 PM, will trillich will.trill...@serensoft.com wrote: Spent a bit of time googlilng this and apparnelty I'm not searching very effectively today... Short version: What's best-practices to get model library sub-modules hooked in to the main model? Hi there. As in all Perl there is always more than one way. I too had great confusion regarding Cat models when I started and certain pods and books will tell you _what_ is the best practice but not necessarily __how__. I wrote this a while back mainly as refrence to myself and later posted it here: ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] sub-modules for model
On Thu, Jan 10, 2013 at 10:37 PM, Alejandro Imass alejandro.im...@gmail.com wrote: On Wed, Dec 19, 2012 at 12:53 PM, will trillich will.trill...@serensoft.com wrote: Spent a bit of time googlilng this and apparnelty I'm not searching very effectively today... Short version: What's best-practices to get model library sub-modules hooked in to the main model? Hi there. As in all Perl there is always more than one way. I too had great confusion regarding Cat models when I started and certain pods and books will tell you _what_ is the best practice but not necessarily __how__. I wrote this a while back mainly as refrence to myself and later posted it here: Sorry, forgot the link :) http://www.perlmonks.org/?node_id=915657 Let me know if you have trouble downloading the sample code from our svn server since someone recently wiped all the server's config. Good luck, -- Alejandro Imass ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/