class Jekyll::Cache

Attributes

base_cache[R]
cache_dir[RW]
disk_cache_enabled[R]

Public Class Methods

clear() click to toggle source

Clear all caches

# File lib/jekyll/cache.rb, line 25
def clear
  delete_cache_files
  base_cache.each_value(&:clear)
end
clear_if_config_changed(config) click to toggle source

Compare the current config to the cached config If they are different, clear all caches

Returns nothing.

# File lib/jekyll/cache.rb, line 34
def clear_if_config_changed(config)
  config = config.inspect
  cache = Jekyll::Cache.new "Jekyll::Cache"
  return if cache.key?("config") && cache["config"] == config

  clear
  cache = Jekyll::Cache.new "Jekyll::Cache"
  cache["config"] = config
  nil
end
disable_disk_cache!() click to toggle source

Disable Marshaling cached items to disk

# File lib/jekyll/cache.rb, line 20
def disable_disk_cache!
  @disk_cache_enabled = false
end
new(name) click to toggle source

Get an existing named cache, or create a new one if none exists

name - name of the cache

Returns nothing.

# File lib/jekyll/cache.rb, line 62
def initialize(name)
  @cache = Jekyll::Cache.base_cache[name] ||= {}
  @name = name.gsub(%r![^\w\s-]!, "-")
end

Private Class Methods

delete_cache_files() click to toggle source

Delete all cached items from all caches

Returns nothing.

# File lib/jekyll/cache.rb, line 50
def delete_cache_files
  FileUtils.rm_rf(@cache_dir) if disk_cache_enabled
end

Public Instance Methods

[](key) click to toggle source

Retrieve a cached item Raises if key does not exist in cache

Returns cached value

# File lib/jekyll/cache.rb, line 77
def [](key)
  return @cache[key] if @cache.key?(key)

  path = path_to(hash(key))
  if disk_cache_enabled? && File.file?(path) && File.readable?(path)
    @cache[key] = load(path)
  else
    raise
  end
end
[]=(key, value) click to toggle source

Add an item to cache

Returns nothing.

# File lib/jekyll/cache.rb, line 91
def []=(key, value)
  @cache[key] = value
  return unless disk_cache_enabled?

  path = path_to(hash(key))
  value = new Hash(value) if value.is_a?(Hash) && !value.default.nil?
  dump(path, value)
rescue TypeError
  Jekyll.logger.debug "Cache:", "Cannot dump object #{key}"
end
clear() click to toggle source

Clear this particular cache

# File lib/jekyll/cache.rb, line 68
def clear
  delete_cache_files
  @cache.clear
end
delete(key) click to toggle source

Remove one particular item from the cache

Returns nothing.

# File lib/jekyll/cache.rb, line 115
def delete(key)
  @cache.delete(key)
  File.delete(path_to(hash(key))) if disk_cache_enabled?
end
disk_cache_enabled?() click to toggle source
# File lib/jekyll/cache.rb, line 134
def disk_cache_enabled?
  !!Jekyll::Cache.disk_cache_enabled
end
getset(key) { || ... } click to toggle source

If an item already exists in the cache, retrieve it. Else execute code block, and add the result to the cache, and return that result.

# File lib/jekyll/cache.rb, line 104
def getset(key)
  self[key]
rescue StandardError
  value = yield
  self[key] = value
  value
end
key?(key) click to toggle source

Check if ‘key` already exists in this cache

Returns true if key exists in the cache, false otherwise

# File lib/jekyll/cache.rb, line 123
def key?(key)
  # First, check if item is already cached in memory
  return true if @cache.key?(key)
  # Otherwise, it might be cached on disk
  # but we should not consider the disk cache if it is disabled
  return false unless disk_cache_enabled?

  path = path_to(hash(key))
  File.file?(path) && File.readable?(path)
end

Private Instance Methods

delete_cache_files() click to toggle source

Remove all this caches items from disk

Returns nothing.

# File lib/jekyll/cache.rb, line 156
def delete_cache_files
  FileUtils.rm_rf(path_to) if disk_cache_enabled?
end
dump(path, value) click to toggle source

Given a path and a value, save value to disk at path. This should NEVER be called in Safe Mode

Returns nothing.

# File lib/jekyll/cache.rb, line 177
def dump(path, value)
  return unless disk_cache_enabled?

  FileUtils.mkdir_p(File.dirname(path))
  File.open(path, "wb") do |cached_file|
    Marshal.dump(value, cached_file)
  end
end
hash(key) click to toggle source

Given a key, return a SHA2 hash that can be used for caching this item to disk.

# File lib/jekyll/cache.rb, line 149
def hash(key)
  Digest::SHA2.hexdigest(key).freeze
end
load(path) click to toggle source

Load ‘path` from disk and return the result. This MUST NEVER be called in Safe Mode rubocop:disable Security/MarshalLoad

# File lib/jekyll/cache.rb, line 163
def load(path)
  raise unless disk_cache_enabled?

  cached_file = File.open(path, "rb")
  value = Marshal.load(cached_file)
  cached_file.close
  value
end
path_to(hash = nil) click to toggle source

Given a hashed key, return the path to where this item would be saved on disk.

# File lib/jekyll/cache.rb, line 141
def path_to(hash = nil)
  @base_dir ||= File.join(Jekyll::Cache.cache_dir, @name)
  return @base_dir if hash.nil?

  File.join(@base_dir, hash[0..1], hash[2..-1]).freeze
end