Class: Bizside::Cache::Store

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

Direct Known Subclasses

FileStore

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Store

Returns a new instance of Store.



9
10
11
# File 'lib/bizside/cache/store.rb', line 9

def initialize(options = nil)
  @options = options ? options.dup : {}
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#cleanup(options = nil) ⇒ Object

Raises:

  • (NotImplementedError)


122
123
124
# File 'lib/bizside/cache/store.rb', line 122

def cleanup(options = nil)
  raise NotImplementedError.new("does not support cleanup")
end

#clear(options = nil) ⇒ Object

Raises:

  • (NotImplementedError)


126
127
128
# File 'lib/bizside/cache/store.rb', line 126

def clear(options = nil)
  raise NotImplementedError.new("does not support clear")
end

#decrement(name, amount = 1, options = nil) ⇒ Object

Raises:

  • (NotImplementedError)


118
119
120
# File 'lib/bizside/cache/store.rb', line 118

def decrement(name, amount = 1, options = nil)
  raise NotImplementedError.new("does not support decrement")
end

#delete(name, options = nil) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/bizside/cache/store.rb', line 93

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

  instrument(:delete, name) do
    delete_entry(namespaced_key(name, options), options)
  end
end

#delete_matched(matcher, options = nil) ⇒ Object

Raises:

  • (NotImplementedError)


110
111
112
# File 'lib/bizside/cache/store.rb', line 110

def delete_matched(matcher, options = nil)
  raise NotImplementedError.new("does not support delete_matched")
end

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

Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
# File 'lib/bizside/cache/store.rb', line 101

def exist?(name, options = nil)
  options = merged_options(options)

  instrument(:exist?, name) do
    entry = read_entry(namespaced_key(name, options), options)
    (entry && !entry.expired?) || false
  end
end

#fetch(name, options = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bizside/cache/store.rb', line 13

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

    cached_entry = find_cached_entry(key, name, options) unless options[:force]
    entry = handle_expired_entry(cached_entry, key, options)

    if entry
      get_entry_value(entry, name, options)
    else
      save_block_result_to_cache(name, options) { |_name| yield _name }
    end
  else
    read(name, options)
  end
end

#fetch_multi(*names) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/bizside/cache/store.rb', line 70

def fetch_multi(*names)
  options = names.extract_options!
  options = merged_options(options)
  results = read_multi(*names, options)

  names.each_with_object({}) do |name, memo|
    memo[name] = results.fetch(name) do
      value = yield name
      write(name, value, options)
      value
    end
  end
end

#increment(name, amount = 1, options = nil) ⇒ Object

Raises:

  • (NotImplementedError)


114
115
116
# File 'lib/bizside/cache/store.rb', line 114

def increment(name, amount = 1, options = nil)
  raise NotImplementedError.new("does not support increment")
end

#read(name, options = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bizside/cache/store.rb', line 31

def read(name, options = nil)
  options = merged_options(options)
  key = namespaced_key(name, options)
  instrument(:read, name, options) do |payload|
    entry = read_entry(key, options)
    if entry
      if entry.expired?
        delete_entry(key, options)
        payload[:hit] = false if payload
        nil
      else
        payload[:hit] = true if payload
        entry.value
      end
    else
      payload[:hit] = false if payload
      nil
    end
  end
end

#read_multi(*names) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/bizside/cache/store.rb', line 52

def read_multi(*names)
  options = names.extract_options!
  options = merged_options(options)
  results = {}
  names.each do |name|
    key = namespaced_key(name, options)
    entry = read_entry(key, options)
    if entry
      if entry.expired?
        delete_entry(key, options)
      else
        results[name] = entry.value
      end
    end
  end
  results
end

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



84
85
86
87
88
89
90
91
# File 'lib/bizside/cache/store.rb', line 84

def write(name, value, options = nil)
  options = merged_options(options)

  instrument(:write, name, options) do
    entry = Bizside::Cache::Entry.new(value, options)
    write_entry(namespaced_key(name, options), entry, options)
  end
end