Class: DoubleWriteCacheStores::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(read_and_write_store_servers, write_only_store_servers = nil) ⇒ Client

Returns a new instance of Client.



2
3
4
5
6
7
8
9
10
# File 'lib/double_write_cache_stores/client.rb', line 2

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
      fail "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 Method Details

#[](key) ⇒ Object



12
13
14
# File 'lib/double_write_cache_stores/client.rb', line 12

def [](key)
  get key
end

#[]=(key, value) ⇒ Object



61
62
63
# File 'lib/double_write_cache_stores/client.rb', line 61

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

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



117
118
119
# File 'lib/double_write_cache_stores/client.rb', line 117

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

#delete(key) ⇒ Object



56
57
58
59
# File 'lib/double_write_cache_stores/client.rb', line 56

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

#fetch(name, options = nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/double_write_cache_stores/client.rb', line 95

def fetch(name, options = nil)
  if @read_and_write_store.respond_to?(:fetch) ||
      (@write_only_store && @write_only_store.respond_to?(:fetch))
    if block_given?
      result = @read_and_write_store.fetch(name, options = nil) { yield }
      @write_only_store.fetch(name, options = nil) { yield } if @write_only_store
      result
    else
      result = @read_and_write_store.fetch(name, options = nil)
      @write_only_store.fetch(name, options = nil) if @write_only_store
      result
    end
  else
    raise UnSupportException.new "Unsupported #fetch from client object."
  end
end

#flushObject



87
88
89
90
91
92
93
# File 'lib/double_write_cache_stores/client.rb', line 87

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

#get(key) ⇒ Object



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

def get(key)
  get_or_read_method_call key
end

#get_cas(key) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/double_write_cache_stores/client.rb', line 24

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(*keys) ⇒ Object



20
21
22
# File 'lib/double_write_cache_stores/client.rb', line 20

def get_multi(*keys)
  get_multi_or_read_multi_method_call *keys
end

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



112
113
114
# File 'lib/double_write_cache_stores/client.rb', line 112

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

#read(key) ⇒ Object



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

def read(key)
  get_or_read_method_call key
end

#read_multi(*keys) ⇒ Object



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

def read_multi(*keys)
  get_multi_or_read_multi_method_call *keys
end

#set(key, value, options = nil) ⇒ Object



65
66
67
# File 'lib/double_write_cache_stores/client.rb', line 65

def set(key, value, options = nil)
  write_cache_store key, value, options
end

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



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

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



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/double_write_cache_stores/client.rb', line 73

def touch(key, ttl=nil)
  result = false
  if defined?(Dalli) && @read_and_write_store.is_a?(Dalli::Client)
    result = @read_and_write_store.touch key, ttl
  else
    read_and_write_backend = @read_and_write_store.instance_variable_get('@backend') || @read_and_write_store.instance_variable_get('@data')
    if read_and_write_backend && read_and_write_backend.respond_to?(:touch)
      result = read_and_write_backend.touch key, ttl
      write_only_store_touch key, ttl
    end
  end
  result
end

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



69
70
71
# File 'lib/double_write_cache_stores/client.rb', line 69

def write(key, value, options = nil)
  write_cache_store key, value, options
end