I'm relatively new to Moose. I'm trying to add the concept of a cached attribute. Essentially, there is a timestamp that is associated with an attribute, which is checked on each access. If the timestamp is older than a certain threshold, the value gets cleared (by the clearer function), and then the value will automatically be refetched (e.g. from the Database) by the build function.
The code to do the timers and database fetch is very straightforward.
What is less straightforward is the best way to do this in Moose. I have a somewhat reasonable solution, but I'd bet someone will have a far more elegant solution.
My solution works like this: you must declare an "around" method modifier, and pass the name of the attribute to a utility function. The utility function will automatically create a timer associated with the attribute (if needed), as well as reset the timer, and call the clearer method.
It occurs to me that this could be done more cleanly. For one, in the around method I must specify the name of the attribute twice. This allows me to use the same function (named "around_timer_cached" below) generically for all the attributes (note that the around method modifier passes the coderef, but not the attribute, which seems like a flaw). E.g.
around 'some_attribute' => sub { around_timer_cached ('some_attribute' +, @_) };
Is there a more elegant way to get the attribute name in the called "around" method? Also, is there a way to extend the attribute system so that this could just be yet another declarative parameter passed into the "has" decl (e.g. cached_secs => 600).
Finally, has someone already done such extensions to the attribute system?
In reply to Extending Moose by zerohero
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |