Widget Caching

5 views
Skip to first unread message

Alex Chaffee

unread,
Dec 21, 2009, 8:23:51 PM12/21/09
to erector
I just checked in some experimental code for widget caching. Here's the lowdown:

* declare a widget as "cachable" (or "cacheable") in its class file
* when it's first rendered (in #to_s or #widget) it'll save the
rendered text off into a hash table
* next time it's rendered it'll use that text instead
* the widget is keyed off of class and parameters (but not block -- in
fact, if the widget takes a block then it's not cachable at all,
sorry)
* the hash is tied to the Widget class so it stays around forever (or
until server restart, whichever comes first)... but I'd like to move
to Moneta so we can use memcache or Redis or maybe just an in-memory
LRU cache

I also made Page's HEAD emit only the externals from widgets that were
actually rendered on it, not all externals from everywhere in
ObjectSpace like it was before.

It's on the "output" branch but I'd like to get some resolution on the
Rails issues from the other thread so we can get this stuff onto the
main branch.

http://github.com/pivotal/erector/commit/9c6bdca1ae33f81cb02cc0129a4c25e9b41fbdbe
http://github.com/pivotal/erector/commit/9bd07ccf67cf821f403af135ca2bd55fec56e3fe

---
Alex Chaffee - al...@cohuman.com - http://alexch.github.com
Stalk me: http://friendfeed.com/alexch | http://twitter.com/alexch |
http://alexch.tumblr.com

John Firebaugh

unread,
Dec 22, 2009, 1:36:10 PM12/22/09
to ale...@gmail.com, erector
> It's on the "output" branch but I'd like to get some resolution on the
> Rails issues from the other thread so we can get this stuff onto the
> main branch.

Working on it.

Would anyone object to changing Widget#h to be idempotent, i.e.
h(text) returns raw(text.html_escape), and h(h(text)) doesn't
double-escape? It would make integrating with rails output safety
easier, would match the behavior of ERB::Util.h in rails 3.0 and
2.3+rails_xss plugin, and we could remove this warning:

# Note that the #text method automatically HTML-escapes
# its parameter, so be careful *not* to do something like text(h("2<4"))
# since that will double-escape the less-than sign (you'll get
# "2&amp;lt;4" instead of "2&lt;4").

Andy Peterson

unread,
Dec 22, 2009, 4:15:49 PM12/22/09
to john.fi...@gmail.com, ale...@gmail.com, erector
Since you're asking,
I would prefer to work on muting the warning rather than having the h routine not do what it is told.

My reasoning is that trying to make an encoding method idempotent doesn't really seem
feasible.  Without some additional metadata, it doesn't know whether the text is was
given was encoded yet... then it starts making heuristic guesses which are harder for
programmers to reason about and use.

Sometimes I want 2<4, sometimes 2&lt;4, and sometimes even 2&amp;lt;4.

Andy


--

You received this message because you are subscribed to the Google Groups "erector" group.
To post to this group, send email to ere...@googlegroups.com.
To unsubscribe from this group, send email to erector+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/erector?hl=en.





--
Andy Peterson | Carbon Five | 415.546.0500 x17 |
mailto:an...@carbonfive.com

John Firebaugh

unread,
Dec 22, 2009, 4:34:57 PM12/22/09
to Andy Peterson, ale...@gmail.com, erector
It's not a ruby interpreter warning I'm talking about, it's an
admonition in the documentation for the method -- warning you, in
effect, to watch out for the fact that it is not idempotent.

The way idempotency is accomplished is by tagging the result with a
special String subclass (RawString) and then checking for the presence
of that tag (albeit a bit indirectly, in order to be compatible with
rails and other libraries with a similar concept).

It is still quite possible to output 2<4, 2&lt;4 or 2&amp;lt;4. Choose between:

rawtext "2<4" # => "2<4"
text! "2<4" # => "2<4"
text "2<4" # => "2&lt;4"
text "2&lt;4" # => "2&amp;lt;4"
text CGI.html_escape("2<4") # => "2&amp;lt;4"

Patch:

http://github.com/bigfix/erector/commit/838ed3c38f8959dd24676752c26eb51851739aef

Reply all
Reply to author
Forward
0 new messages