Class: Middleman::FragmentCaching::Cache

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/middleman/fragment_caching/cache.rb

Overview

Cache management

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_store(kind, options = {}) ⇒ Moneta

Creates a store

Returns:



22
23
24
# File 'lib/middleman/fragment_caching/cache.rb', line 22

def create_store(kind, options = {})
  Moneta.new(kind, options)
end

.dirObject



10
11
12
13
14
# File 'lib/middleman/fragment_caching/cache.rb', line 10

def dir
  @dir ||= File.expand_path('tmp/cache').tap do |path|
    FileUtils.mkdir_p(path)
  end
end

.storeObject



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_keysObject



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

#storeObject



29
30
31
# File 'lib/middleman/fragment_caching/cache.rb', line 29

def store
  self.class.store
end

#stored_keysObject



33
34
35
# File 'lib/middleman/fragment_caching/cache.rb', line 33

def stored_keys
  @stored_keys ||= Set.new
end