Class: Bridgetown::Cache
- Inherits:
-
Object
- Object
- Bridgetown::Cache
- Defined in:
- lib/bridgetown-core/cache.rb
Class Attribute Summary collapse
-
.base_cache ⇒ Object
readonly
class-wide base cache reader.
-
.cache_dir ⇒ Object
class-wide cache location.
-
.disk_cache_enabled ⇒ Object
readonly
class-wide directive to write cache to disk.
Class Method Summary collapse
-
.clear ⇒ Object
Clear all caches.
-
.clear_if_config_changed(config) ⇒ Object
Compare the current config to the cached config If they are different, clear all caches.
-
.disable_disk_cache! ⇒ Object
Disable Marshaling cached items to disk.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Retrieve a cached item Raises if key does not exist in cache.
-
#[]=(key, value) ⇒ Object
Add an item to cache.
-
#clear ⇒ Object
Clear this particular cache.
-
#delete(key) ⇒ Object
Remove one particular item from the cache.
- #disk_cache_enabled? ⇒ Boolean
-
#getset(key) ⇒ Object
If an item already exists in the cache, retrieve it.
-
#initialize(name) ⇒ Cache
constructor
Get an existing named cache, or create a new one if none exists.
-
#key?(key) ⇒ Boolean
Check if
key
already exists in this cache.
Constructor Details
#initialize(name) ⇒ Cache
Get an existing named cache, or create a new one if none exists
60 61 62 63 |
# File 'lib/bridgetown-core/cache.rb', line 60 def initialize(name) @cache = Bridgetown::Cache.base_cache[name] ||= {} @name = name.gsub(%r![^\w\s-]!, "-") end |
Class Attribute Details
.base_cache ⇒ Object (readonly)
class-wide base cache reader
21 22 23 |
# File 'lib/bridgetown-core/cache.rb', line 21 def base_cache @base_cache end |
.cache_dir ⇒ Object
class-wide cache location
15 16 17 |
# File 'lib/bridgetown-core/cache.rb', line 15 def cache_dir @cache_dir end |
.disk_cache_enabled ⇒ Object (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
.clear ⇒ Object
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
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/bridgetown-core/cache.rb', line 36 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
75 76 77 78 79 80 81 82 |
# File 'lib/bridgetown-core/cache.rb', line 75 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
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/bridgetown-core/cache.rb', line 85 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 |
#clear ⇒ Object
Clear this particular cache
66 67 68 69 |
# File 'lib/bridgetown-core/cache.rb', line 66 def clear delete_cache_files @cache.clear end |
#delete(key) ⇒ Object
Remove one particular item from the cache
107 108 109 110 |
# File 'lib/bridgetown-core/cache.rb', line 107 def delete(key) @cache.delete(key) File.delete(path_to(hash(key))) if disk_cache_enabled? end |
#disk_cache_enabled? ⇒ Boolean
126 127 128 |
# File 'lib/bridgetown-core/cache.rb', line 126 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.
98 99 100 101 102 103 104 |
# File 'lib/bridgetown-core/cache.rb', line 98 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
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/bridgetown-core/cache.rb', line 115 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 |