Jekyll includes a caching API, which is used both internally as well as exposed
for plugins, which can be used to cache the output of deterministic functions to
speed up site generation. This cache will be persistent across builds, but
cleared when Jekyll detects any changes to
Jekyll::Cache.new(name) → new_cache
If there has already been a cache created with
name, this will return a
reference to that existing Cache. Otherwise, create a new Cache called
If this Cache will be used by a Gem-packaged plugin,
name should either be the
name of the Gem, or prefixed with the name of the Gem followed by
:: (if a
plugin expects to use multiple Caches). If this Cache will be used internally by
name should be the name of the class that is using the Cache (ie:
Cached objects are shared between all Caches created with the same
are not shared between Caches with different names. There can be an object
stored with key
Jekyll::Cache.new("a") and an object stored with key
Jekyll::Cache.new("b") and these will not point to the same cached
object. This way, you do not need to ensure that keys are globally unique.
This is the most common way to utilize the Cache.
block is a bit of code that takes a lot of time to compute, but always
generates the same output given a particular input (like converting Markdown to
key is a
String (or an object with
to_s) that uniquely identifies
the input to the function.
key already exists in the Cache, it will be returned and
block will never
be executed. If
key does not exist in the Cache,
block will be executed and
the result will be added to the Cache and returned.
def cache @@cache ||= Jekyll::Cache.new("ConvertMarkdown") end def convert_markdown_to_html(markdown) cache.getset(markdown) do expensive_conversion_method(markdown) end end
In the above example,
expensive_conversion_method will only be called once for
markdown input. If
convert_markdown_to_html is called a second
time with the same input, the cached output will be returned.
Because posts will frequently remain unchanged from one build to the next, this is an effective way to avoid performing the same computations each time the site is built.
This will clear all cached objects from a particular Cache. The Cache will be empty, both in memory and on disk.
The following methods will probably only be used in special circumstances
cache[key] → value
key from Cache and returns its
value. Raises if
key does not exist
cache[key] = value
value to Cache under
key?(key) → true or false
key already exists in Cache. False otherwise.
key from Cache.