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



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

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

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



88
89
90
# File 'lib/double_write_cache_stores/client.rb', line 88

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

#delete(key) ⇒ Object



43
44
45
46
47
# File 'lib/double_write_cache_stores/client.rb', line 43

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/double_write_cache_stores/client.rb', line 67

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



59
60
61
62
63
64
65
# File 'lib/double_write_cache_stores/client.rb', line 59

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
# 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
  end
end

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



110
111
112
113
114
115
116
117
118
# File 'lib/double_write_cache_stores/client.rb', line 110

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



100
101
102
103
104
105
106
# File 'lib/double_write_cache_stores/client.rb', line 100

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



83
84
85
# File 'lib/double_write_cache_stores/client.rb', line 83

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

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



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

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
               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



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

def touch(key, ttl = nil)
  result = @read_and_write_store.touch key, ttl
  @write_only_store.touch key, ttl if @write_only_store
  result
end

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



93
94
95
96
# File 'lib/double_write_cache_stores/client.rb', line 93

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