Class: Middleman::FragmentCaching::Cache
- Inherits:
-
Object
- Object
- Middleman::FragmentCaching::Cache
- Includes:
- Singleton
- Defined in:
- lib/middleman/fragment_caching/cache.rb
Overview
Cache management
Class Method Summary collapse
-
.create_store(kind, options = {}) ⇒ Moneta
Creates a store.
- .dir ⇒ Object
- .store ⇒ Object
Instance Method Summary collapse
- #clear! ⇒ Object
- #expires_keys ⇒ Object
- #find_or_set(key, &block) ⇒ Object
- #store ⇒ Object
- #stored_keys ⇒ Object
Class Method Details
.create_store(kind, options = {}) ⇒ Moneta
Creates a store
22 23 24 |
# File 'lib/middleman/fragment_caching/cache.rb', line 22 def create_store(kind, = {}) Moneta.new(kind, ) end |
.dir ⇒ Object
10 11 12 13 14 |
# File 'lib/middleman/fragment_caching/cache.rb', line 10 def dir @dir ||= File.('tmp/cache').tap do |path| FileUtils.mkdir_p(path) end end |
.store ⇒ Object
16 17 18 |
# File 'lib/middleman/fragment_caching/cache.rb', line 16 def store @store ||= create_store(:File, dir: dir) end |
Instance Method Details
#clear! ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/middleman/fragment_caching/cache.rb', line 56 def clear! expires_keys.each do |key| store.delete key end stored_keys.clear end |
#expires_keys ⇒ Object
52 53 54 |
# File 'lib/middleman/fragment_caching/cache.rb', line 52 def expires_keys store.keys - stored_keys.to_a end |
#find_or_set(key, &block) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/middleman/fragment_caching/cache.rb', line 37 def find_or_set(key, &block) key = key.to_s if !key.empty? && store.key?(key) content = store[key] else content = block.call store[key] = content unless key.empty? end stored_keys << key unless key.empty? content end |
#store ⇒ Object
29 30 31 |
# File 'lib/middleman/fragment_caching/cache.rb', line 29 def store self.class.store end |
#stored_keys ⇒ Object
33 34 35 |
# File 'lib/middleman/fragment_caching/cache.rb', line 33 def stored_keys @stored_keys ||= Set.new end |