Class: DoubleWriteCacheStores::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/double_write_cache_stores/client.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(read_and_write_store_servers, write_only_store_servers = nil) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
10
11
12
13
# File 'lib/double_write_cache_stores/client.rb', line 5

def initialize(read_and_write_store_servers, write_only_store_servers = nil)
  @read_and_write_store = read_and_write_store_servers
  if write_only_store_servers
    if read_and_write_store_servers.class != write_only_store_servers.class
      raise "different cache store instance. #{read_and_write_store_servers.class} != #{write_only_store_servers.class}"
    end
    @write_only_store = write_only_store_servers
  end
end

Instance Attribute Details

#read_and_write_storeObject

Returns the value of attribute read_and_write_store.



3
4
5
# File 'lib/double_write_cache_stores/client.rb', line 3

def read_and_write_store
  @read_and_write_store
end

#write_only_storeObject

Returns the value of attribute write_only_store.



3
4
5
# File 'lib/double_write_cache_stores/client.rb', line 3

def write_only_store
  @write_only_store
end

Instance Method Details

#[](key) ⇒ Object



15
16
17
# File 'lib/double_write_cache_stores/client.rb', line 15

def [](key)
  get key
end

#[]=(key, value) ⇒ Object



53
54
55
# File 'lib/double_write_cache_stores/client.rb', line 53

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

#decrement(key, amount = 1, options = {}) ⇒ Object Also known as: decr



104
105
106
# File 'lib/double_write_cache_stores/client.rb', line 104

def decrement(key, amount = 1, options = {})
  decrement_cache_store key, amount, options
end

#delete(key) ⇒ Object



47
48
49
50
51
# File 'lib/double_write_cache_stores/client.rb', line 47

def delete(key)
  result = @read_and_write_store.delete key
  @write_only_store.delete key if @write_only_store
  result
end

#fetch(name, options = {}, &_block) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/double_write_cache_stores/client.rb', line 83

def fetch(name, options = {}, &_block)
  raise UnSupportException "Unsupported #fetch from client object." unless @read_and_write_store.respond_to?(:fetch)

  delete name if options[:force]

  if options[:race_condition_ttl]
    fetch_race_condition name, options { yield }
  else
    unless value = get_or_read_method_call(name)
      value = yield
      write_cache_store name, value, options
    end
    value
  end
end

#flushObject



75
76
77
78
79
80
81
# File 'lib/double_write_cache_stores/client.rb', line 75

def flush
  if flush_cache_store || flush_cache_store(:clear)
    true
  else
    false
  end
end

#get_cas(key) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/double_write_cache_stores/client.rb', line 19

def get_cas(key)
  if @read_and_write_store.respond_to? :get_cas
    @read_and_write_store.get_cas key
  elsif @read_and_write_store.respond_to? :read_cas
    @read_and_write_store.read_cas key
  elsif @read_and_write_store.respond_to? :dalli
    @read_and_write_store.dalli.get_cas key
  end
end

#get_multi_or_read_multi_method_call(*keys) ⇒ Object Also known as: get_multi, read_multi



126
127
128
129
130
131
132
133
134
# File 'lib/double_write_cache_stores/client.rb', line 126

def get_multi_or_read_multi_method_call(*keys)
  if @read_and_write_store.respond_to? :get_multi
    @read_and_write_store.get_multi(*keys)
  elsif @read_and_write_store.respond_to? :read_multi
    @read_and_write_store.read_multi(*keys)
  else
    raise UnSupportException "Unsupported multi keys get or read from client object."
  end
end

#get_or_read_method_call(key) ⇒ Object Also known as: get, read



116
117
118
119
120
121
122
# File 'lib/double_write_cache_stores/client.rb', line 116

def get_or_read_method_call(key)
  if @read_and_write_store.respond_to? :get
    @read_and_write_store.get key
  elsif @read_and_write_store.respond_to? :read
    @read_and_write_store.read key
  end
end

#increment(key, amount = 1, options = {}) ⇒ Object Also known as: incr



99
100
101
# File 'lib/double_write_cache_stores/client.rb', line 99

def increment(key, amount = 1, options = {})
  increment_cache_store key, amount, options
end

#set_cas(key, value, cas = 0, options = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/double_write_cache_stores/client.rb', line 29

def set_cas(key, value, cas = 0, options = nil)
  cas_unique = if @read_and_write_store.respond_to? :set_cas
                 @read_and_write_store.set_cas key, value, cas, options
               elsif @read_and_write_store.respond_to? :read_cas
                 options ||= {}
                 options[:cas] = cas
                 @read_and_write_store.write_cas key, value, options
               elsif @read_and_write_store.respond_to? :dalli
                 @read_and_write_store.dalli.set_cas key, value, cas, options
               end

  if @write_only_store && cas_unique
    set_or_write_method_call @write_only_store, key, value, options
  end

  cas_unique
end

#touch(key, ttl = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/double_write_cache_stores/client.rb', line 57

def touch(key, ttl = nil)
  result = if @read_and_write_store.respond_to? :touch
             @read_and_write_store.touch key, ttl
           elsif @read_and_write_store.respond_to? :dalli
             @read_and_write_store.dalli.touch key, ttl
           end

  if @write_only_store
    if @write_only_store.respond_to? :touch
      @write_only_store.touch key, ttl
    elsif @write_only_store.respond_to? :dalli
      @write_only_store.dalli.touch key, ttl
    end
  end

  result
end

#write_cache_store(key, value, options = nil) ⇒ Object Also known as: set, write



109
110
111
112
# File 'lib/double_write_cache_stores/client.rb', line 109

def write_cache_store(key, value, options = nil)
  set_or_write_method_call @read_and_write_store, key, value, options
  set_or_write_method_call @write_only_store, key, value, options if @write_only_store
end