Class: Bridgetown::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/bridgetown-core/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/bridgetown-core/cache.rb', line 66

def initialize(name)
  @cache = Bridgetown::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/bridgetown-core/cache.rb', line 21

def base_cache
  @base_cache
end

.cache_dirObject

class-wide cache location



15
16
17
# File 'lib/bridgetown-core/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/bridgetown-core/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/bridgetown-core/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/bridgetown-core/cache.rb', line 38

def clear_if_config_changed(config)
  config = config.inspect
  cache = Bridgetown::Cache.new "Bridgetown::Cache"
  return if cache.key?("config") && cache["config"] == config

  clear
  cache = Bridgetown::Cache.new "Bridgetown::Cache"
  cache["config"] = config
  nil
end

.disable_disk_cache!Object

Disable Marshaling cached items to disk



24
25
26
# File 'lib/bridgetown-core/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
# File 'lib/bridgetown-core/cache.rb', line 81

def [](key)
  return @cache[key] if @cache.key?(key)

  path = path_to(hash(key))
  raise unless disk_cache_enabled? && File.file?(path) && File.readable?(path)

  @cache[key] = load(path)
end

#[]=(key, value) ⇒ Object

Add an item to cache

Returns nothing.



93
94
95
96
97
98
99
100
101
102
# File 'lib/bridgetown-core/cache.rb', line 93

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
  Bridgetown.logger.debug "Cache:", "Cannot dump object #{key}"
end

#clearObject

Clear this particular cache



72
73
74
75
# File 'lib/bridgetown-core/cache.rb', line 72

def clear
  delete_cache_files
  @cache.clear
end

#delete(key) ⇒ Object

Remove one particular item from the cache

Returns nothing.



117
118
119
120
# File 'lib/bridgetown-core/cache.rb', line 117

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

#disk_cache_enabled?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/bridgetown-core/cache.rb', line 136

def disk_cache_enabled?
  !!Bridgetown::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.



106
107
108
109
110
111
112
# File 'lib/bridgetown-core/cache.rb', line 106

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)


125
126
127
128
129
130
131
132
133
134
# File 'lib/bridgetown-core/cache.rb', line 125

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