On Monday, April 28, 2014 8:34:46 PM UTC-7, russ m wrote:
>
> Hi All,
>
> Is there any good way to hook a model such that the hook function is run 
> *before* a select query is issued to the database?
>
> My use case is a database that is caching resources from a crappy REST 
> source that only supports "get resource by ID" and "get all resources 
> modified since this timestamp" operations. At the moment the cache is 
> updated on a schedule (so is always a little stale, and is read-only to our 
> application), and I'd like to try out refreshing the cache on every 
> retrieval of a Sequel::Model object.
>

If you are refreshing the cache on every retrieval, then you probably 
should not be caching at all.  
 

> As far as I can tell I can't use the Sequel::Model#initialize like the 
> AfterInitialize 
> plugin does, since model objects aren't instantiated unless the dataset 
> query returns rows.
>
> Before I wade into datasets to see where/how to hook them, can anybody 
> point me at a good spot to look at doing this?
>

The only place I can think of to do this is to override Dataset#each for 
the model's dataset:

  ModelClass.dataset_module{def each; refresh_cache; super end}

But that may refresh the cache more often than you want.  A better approach 
if this is a web application is to add a before hook or rack middleware 
that will reload the cache before the appropriate actions.  That way you 
are only refreshing once per web request.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to