Class: Jekyll::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/cache.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Cache

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

name - name of the cache

Returns nothing.


66
67
68
69
# File 'lib/jekyll/cache.rb', line 66

def initialize(name)
  @cache = Jekyll::Cache.base_cache[name] ||= {}
  @name = name.gsub(%r![^\w\s-]!, "-")
end

Class Attribute Details

.base_cacheObject (readonly)

class-wide base cache reader


21
22
23
# File 'lib/jekyll/cache.rb', line 21

def base_cache
  @base_cache
end

.cache_dirObject

class-wide cache location


15
16
17
# File 'lib/jekyll/cache.rb', line 15

def cache_dir
  @cache_dir
end

.disk_cache_enabledObject (readonly)

class-wide directive to write cache to disk


18
19
20
# File 'lib/jekyll/cache.rb', line 18

def disk_cache_enabled
  @disk_cache_enabled
end

Class Method Details

.clearObject

Clear all caches


29
30
31
32
# File 'lib/jekyll/cache.rb', line 29

def clear
  delete_cache_files
  base_cache.each_value(&:clear)
end

.clear_if_config_changed(config) ⇒ Object

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

Returns nothing.


38
39
40
41
42
43
44
45
46
47
# File 'lib/jekyll/cache.rb', line 38

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!Object

Disable Marshaling cached items to disk


24
25
26
# File 'lib/jekyll/cache.rb', line 24

def disable_disk_cache!
  @disk_cache_enabled = false
end

Instance Method Details

#[](key) ⇒ Object

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

Returns cached value


81
82
83
84
85
86
87
88
89
90
# File 'lib/jekyll/cache.rb', line 81

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) ⇒ Object

Add an item to cache

Returns nothing.


95
96
97
98
99
100
101
102
103
104
# File 'lib/jekyll/cache.rb', line 95

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

#clearObject

Clear this particular cache


72
73
74
75
# File 'lib/jekyll/cache.rb', line 72

def clear
  delete_cache_files
  @cache.clear
end

#delete(key) ⇒ Object

Remove one particular item from the cache

Returns nothing.


119
120
121
122
# File 'lib/jekyll/cache.rb', line 119

def delete(key)
  @cache.delete(key)
  File.delete(path_to(hash(key))) if disk_cache_enabled?
end

#disk_cache_enabled?Boolean

Returns:

  • (Boolean)

138
139
140
# File 'lib/jekyll/cache.rb', line 138

def disk_cache_enabled?
  !!Jekyll::Cache.disk_cache_enabled
end

#getset(key) ⇒ Object

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.


108
109
110
111
112
113
114
# File 'lib/jekyll/cache.rb', line 108

def getset(key)
  self[key]
rescue StandardError
  value = yield
  self[key] = value
  value
end

#key?(key) ⇒ Boolean

Check if `key` already exists in this cache

Returns true if key exists in the cache, false otherwise

Returns:

  • (Boolean)

127
128
129
130
131
132
133
134
135
136
# File 'lib/jekyll/cache.rb', line 127

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