Class: Racket::Utils::Views::TemplateCache

Inherits:
Object
  • Object
show all
Defined in:
lib/racket/utils/views/template_cache.rb

Overview

Class for caching templates. This class adheres to the Moneta API (github.com/minad/moneta#user-content-moneta-api), even though it is not using the Moneta framework.

Constant Summary collapse

DEFAULT_OPTIONS =

Default options for template cache

{ expires: 0 }.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ TemplateCache

Returns a new instance of TemplateCache.



38
39
40
41
42
# File 'lib/racket/utils/views/template_cache.rb', line 38

def initialize(options)
  @expirations = {}
  @items = {}
  @options = DEFAULT_OPTIONS.merge(options)
end

Class Method Details

.service(_options = {}) ⇒ Proc

Returns a service proc that can be used by the registry.

Parameters:

  • _options (Hash) (defaults to: {})

    (unused)

Returns:

  • (Proc)


34
35
36
# File 'lib/racket/utils/views/template_cache.rb', line 34

def self.service(_options = {})
  -> { new({}) }
end

Instance Method Details

#[](key) ⇒ Object



44
45
46
# File 'lib/racket/utils/views/template_cache.rb', line 44

def [](key)
  load(key)
end

#[]=(key, value) ⇒ Object



48
49
50
# File 'lib/racket/utils/views/template_cache.rb', line 48

def []=(key, value)
  store(key, value)
end

#clear(_options = {}) ⇒ Object



52
53
54
55
# File 'lib/racket/utils/views/template_cache.rb', line 52

def clear(_options = {})
  @expirations.clear
  @items.clear
end

#closeObject



57
58
59
# File 'lib/racket/utils/views/template_cache.rb', line 57

def close
  clear
end

#create(_key, _value, _options = {}) ⇒ Object

Raises:

  • (NotImplementedError)


61
62
63
# File 'lib/racket/utils/views/template_cache.rb', line 61

def create(_key, _value, _options = {})
  raise NotImplementedError
end

#decrement(_key, _amount = 1, _options = {}) ⇒ Object

Raises:

  • (NotImplementedError)


65
66
67
# File 'lib/racket/utils/views/template_cache.rb', line 65

def decrement(_key, _amount = 1, _options = {})
  raise NotImplementedError
end

#delete(key, _options = {}) ⇒ Object



69
70
71
72
# File 'lib/racket/utils/views/template_cache.rb', line 69

def delete(key, _options = {})
  @expirations.delete(key)
  @items.delete(key)
end

#featuresObject



74
75
76
# File 'lib/racket/utils/views/template_cache.rb', line 74

def features
  []
end

#fetch(*args) ⇒ Object

This method handles both forms of fetch. With a default block - fetch(key, options = {}, &block) With a default value - fetch(key, value, options = {})



81
82
83
84
85
# File 'lib/racket/utils/views/template_cache.rb', line 81

def fetch(*args)
  key = args.shift
  return load(key) if key?(key)
  block_given? ? yield : args.first
end

#increment(_key, _amount = 1, _options = {}) ⇒ Object

Raises:

  • (NotImplementedError)


87
88
89
# File 'lib/racket/utils/views/template_cache.rb', line 87

def increment(_key, _amount = 1, _options = {})
  raise NotImplementedError
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/racket/utils/views/template_cache.rb', line 91

def key?(key)
  @items.key?(key)
end

#load(key, _options = {}) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/racket/utils/views/template_cache.rb', line 95

def load(key, _options = {})
  return @items[key] unless @expirations.key?(key)
  if Time.now > @expirations[key]
    @expirations.delete(key)
    @items.delete(key)
  end
  @items[key]
end

#store(key, value, options = {}) ⇒ Object



104
105
106
107
# File 'lib/racket/utils/views/template_cache.rb', line 104

def store(key, value, options = {})
  set_expiration(key, options.fetch(:expires, @options[:expires]))
  @items[key] = value
end

#supports?(feature) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/racket/utils/views/template_cache.rb', line 109

def supports?(feature)
  features.include?(feature)
end