Class: Zm::Support::Cache::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/zm/support/cache/store.rb

Direct Known Subclasses

FileStore, NullStore

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Store

Returns a new instance of Store.



10
11
12
13
14
15
# File 'lib/zm/support/cache/store.rb', line 10

def initialize(options = {})
  @options = options
  @coder = Cache::Entry.factory
  @digest = OpenSSL::Digest.new('sha256')
  @logger = nil
end

Instance Attribute Details

#logger=(value) ⇒ Object (writeonly)

Sets the attribute logger

Parameters:

  • value

    the value to set the attribute logger to.



8
9
10
# File 'lib/zm/support/cache/store.rb', line 8

def logger=(value)
  @logger = value
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/zm/support/cache/store.rb', line 7

def options
  @options
end

Instance Method Details

#cleanup(_options = nil) ⇒ Object

Raises:

  • (NotImplementedError)


91
92
93
# File 'lib/zm/support/cache/store.rb', line 91

def cleanup(_options = nil)
  raise NotImplementedError
end

#clear(_options = nil) ⇒ Object

Raises:

  • (NotImplementedError)


87
88
89
# File 'lib/zm/support/cache/store.rb', line 87

def clear(_options = nil)
  raise NotImplementedError
end

#delete(name, options = nil) ⇒ Object



71
72
73
74
75
76
# File 'lib/zm/support/cache/store.rb', line 71

def delete(name, options = nil)
  options = merged_options(options)
  key = normalize_key(name, options)

  delete_entry(key, **options)
end

#exist?(name, options = nil) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
# File 'lib/zm/support/cache/store.rb', line 78

def exist?(name, options = nil)
  options = merged_options(options)
  key = normalize_key(name, options)
  version = normalize_version(options)

  entry = read_entry(key, **options)
  (entry && !entry.expired? && !entry.mismatched?(version)) || false
end

#fetch(name, options = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/zm/support/cache/store.rb', line 17

def fetch(name, options = nil, &)
  if block_given?
    options = merged_options(options)
    key = normalize_key(name, options)

    entry = nil

    unless options[:force]
      cached_entry = read_entry(key, **options)
      entry = handle_expired_entry(cached_entry, key, options)

      entry = nil if entry&.mismatched?(normalize_version(options))
    end

    if entry
      @logger&.info 'Load from cache'
      entry.value
    else
      save_block_result_to_cache(key, options, &)
    end
  elsif options && options[:force]
    raise ArgumentError, 'Missing block: Calling `Cache#fetch` with `force: true` requires a block.'
  else
    read(name, options)
  end
end

#read(name, options = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/zm/support/cache/store.rb', line 44

def read(name, options = nil)
  options = merged_options(options)
  key     = normalize_key(name, options)
  version = normalize_version(options)

  entry = read_entry(key, **options)

  return unless entry

  if entry.expired? || entry.mismatched?(version)
    delete_entry(key, **options)
    nil
  else
    @logger&.info 'Load from cache'
    entry.value
  end
end

#write(name, value, options = nil) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/zm/support/cache/store.rb', line 62

def write(name, value, options = nil)
  options = merged_options(options)
  key = normalize_key(name, options)
  version = normalize_version(options)

  entry = Entry.new(value, **options, version: version)
  write_entry(key, entry, **options)
end