liquid
liquid copied to clipboard
Serializing a compiled Template using Marshal.dump fails after version 5.6.0
Hello there,
Previous to release 5.6.0, up to 5.5.1, this code was possible (tested using ruby >= 3.2):
ct = Liquid::Template.parse(some_valid_liquid_string)
Marshal.dump(ct) # this can be saved to memcache or disk
but with version >= 5.6.0, it fails with the error:
Marshal.dump: can't dump anonymous class #<Class:0x0000000153633cc0> (TypeError)
By my investigation, this comes from the attribute @strainer_template which is now instantiated as an anonymous class, as part of the Environment instance.
I saw #760 conversation about the ability to cache a compiled template and as I understand it wasn't a main focus, it was something done in/before 2016 during hack days, but it worked up until 5.6.0, and it is als mentioned in the wiki as a strategy: https://github.com/Shopify/liquid/wiki/Liquid-for-Programmers#first-steps
The
parsestep creates a fully compiled template which can be re-used as often as you like. You can store it in memory or in a cache for faster rendering later.
Was this change intentional? Or is there any other way to cache (serialize) the compiled template?
Thanks!
PS: I did not do any elaborate benchmarks (yet) about compiling on each request vs fetching from cache, but IMO the way I see it is about moving work from IO (memcache) to CPU (re-compiling the template) on each request. If I could choose, I'd like the IO work better :-)