double_write_cache_stores 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2f2e06c115f9f6f2c93e70bd217a9f0d2277e4b8
4
- data.tar.gz: 026266132650bcfe4a9b11f8a7ea78cbaeaaceeb
3
+ metadata.gz: 13d2f0ce282efcf499bf8fb33bbfead44f82d2d3
4
+ data.tar.gz: e5b840f484ca4cabb14ce53c952132f57b42709f
5
5
  SHA512:
6
- metadata.gz: 7abdfd76502de8e42de745dbdd1ab4c729a126e3154e0572383720c70a27d06cd6e0e5e072963c57aaa8e1ca9833c81a4686278a489535e0b28d12e93735d2d7
7
- data.tar.gz: 4ffd5deef56bd569014e711abf799c39217f2ae484c0ebc612d8868626a9f2152be6ac62c12f6a62f32c948c133ed24f7ac23788a681d8f47d6507efae24868b
6
+ metadata.gz: 5838cbbac0c8a973188aa26a85dea08895a2479049d9003b5c940220a05884b5f08c07dfab26c7caa4aa9b676f7239c3e4a015385d8dda3d10434f463a05349d
7
+ data.tar.gz: 9a1acfaf37c69dccd577ca88c2f58ff230cdf4e6d5d5c3391a599b240ae7cd1445cf7a9a3b4428556771e362a63e6071159814dc39f44e3c4cdbfcbff91218f5
@@ -0,0 +1,60 @@
1
+ AllCops:
2
+ Exclude:
3
+ - "vendor/**/*"
4
+ DisplayCopNames: true
5
+
6
+ Style/AsciiComments:
7
+ Enabled: false
8
+
9
+ Style/BracesAroundHashParameters:
10
+ Enabled: false
11
+
12
+ Style/Documentation:
13
+ Enabled: false
14
+
15
+ Style/ExtraSpacing:
16
+ Enabled: false
17
+
18
+ Style/GuardClause:
19
+ MinBodyLength: 5
20
+
21
+ Style/MethodDefParentheses:
22
+ EnforcedStyle: require_parentheses
23
+
24
+ Style/ModuleFunction:
25
+ Enabled: false
26
+
27
+ Style/StringLiterals:
28
+ EnforcedStyle: double_quotes
29
+
30
+ Style/SingleSpaceBeforeFirstArg:
31
+ Enabled: false
32
+
33
+ Style/IndentationConsistency:
34
+ EnforcedStyle: rails
35
+
36
+ Style/SignalException:
37
+ EnforcedStyle: only_raise
38
+
39
+ Style/HashSyntax:
40
+ EnforcedStyle: ruby19_no_mixed_keys
41
+ Exclude:
42
+ - "**/*.rake"
43
+ - "Rakefile"
44
+
45
+ Metrics/LineLength:
46
+ Max: 160
47
+ Exclude:
48
+ - "db/migrate/*.rb"
49
+
50
+ NumericLiterals:
51
+ MinDigits: 7
52
+
53
+ Lint/AssignmentInCondition:
54
+ Enabled: false
55
+
56
+ Lint/BlockAlignment:
57
+ Enabled: false
58
+
59
+ Metrics/MethodLength:
60
+ Max: 12
@@ -0,0 +1 @@
1
+ 2.2.3
@@ -1,3 +1,21 @@
1
1
  language: ruby
2
+ install: bundle install --jobs=3 --retry=3
3
+ cache:
4
+ directories:
5
+ - vendor/bundle
2
6
  rvm:
3
- - 2.1.0
7
+ - 2.2.3
8
+ - 2.1.7
9
+ - 2.0.0-p647
10
+
11
+ gemfile:
12
+ - Gemfile
13
+ - activesupport.gemfile
14
+
15
+ addons:
16
+ apt:
17
+ packages:
18
+ - memcached
19
+
20
+ script:
21
+ - bundle exec rake spec
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source 'https://rubygems.org'
1
+ source "https://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in double_write_cache_stores.gemspec
4
4
  gemspec
data/README.md CHANGED
@@ -1,17 +1,14 @@
1
1
  # DoubleWriteCacheStores
2
2
 
3
- pre-warning(double write to cach store and other cache store) cache store wrapper. will switch cache store.
3
+ [![Build Status](https://travis-ci.org/hirocaster/double_write_cache_stores.svg?branch=master)](https://travis-ci.org/hirocaster/double_write_cache_stores)
4
4
 
5
- ## Convertible interface, get/set by cache store
6
-
7
- - Padrino::Cache(moneta)
8
- - ActiveSupport::Cache::DalliStore(Dalli)
9
- - Padrino::Cache::Store::Memcache
5
+ pre-warming(double write to cach store and other cache store) cache store wrapper. will switch cache store.
10
6
 
11
7
  ## Support backend cache store
12
8
 
13
- - ActiveSupport::Cache::DalliStore(Dalli)
14
- - Padrino::Cache::Store::Memcache
9
+ - ActiveSupport::Cache::MemCacheStore
10
+ - ActiveSupport::Cache::DalliStore in Dalli
11
+ - Dalli::Client
15
12
 
16
13
  ## Installation
17
14
 
@@ -32,6 +29,7 @@ Or install it yourself as:
32
29
  ### Padrino
33
30
 
34
31
  `config/apps.rb`
32
+
35
33
  ````
36
34
  read_and_write_cache_store = ActiveSupport::Cache.lookup_store :dalli_store, 'localhost:11211'
37
35
  write_only_cache_store = ActiveSupport::Cache.lookup_store :dalli_store, 'localhost:21211'
@@ -44,10 +42,14 @@ set :cache, DoubleWriteCacheStores::Client.new(read_and_write_cache_store, write
44
42
  `config/application.rb`
45
43
 
46
44
  ```ruby
47
- read_and_write_cache_store = ActiveSupport::Cache.lookup_store :mem_cache_store, 'localhost:11211'
48
- write_only_cache_store = ActiveSupport::Cache.lookup_store :mem_cache_store, 'localhost:21211'
45
+ options = { expires_in: 1.week, compress: true }
46
+
47
+ read_and_write_cache_store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost:11211", options
48
+ config.middleware.insert_before "Rack::Runtime", read_and_write_cache_store.middleware
49
+
50
+ write_only_cache_store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost:21211", options
49
51
 
50
- config.cache_store = DoubleWriteCacheStores::Client.new(read_and_write_cache_store, write_only_cache_store)
52
+ config.cache_store = DoubleWriteCacheStores::Client.new read_and_write_cache_store, write_only_cache_store
51
53
  ```
52
54
 
53
55
  #### in application
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "rack"
4
+ gem "activesupport"
5
+
6
+ # Specify your gem's dependencies in active_record_sharding.gemspec
7
+ gemspec
@@ -0,0 +1,62 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ double_write_cache_stores (0.3.0)
5
+ dalli (~> 2.7.4)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (4.2.4)
11
+ i18n (~> 0.7)
12
+ json (~> 1.7, >= 1.7.7)
13
+ minitest (~> 5.1)
14
+ thread_safe (~> 0.3, >= 0.3.4)
15
+ tzinfo (~> 1.1)
16
+ coderay (1.1.0)
17
+ dalli (2.7.4)
18
+ diff-lcs (1.2.5)
19
+ i18n (0.7.0)
20
+ json (1.8.3)
21
+ method_source (0.8.2)
22
+ minitest (5.8.2)
23
+ pry (0.10.3)
24
+ coderay (~> 1.1.0)
25
+ method_source (~> 0.8.1)
26
+ slop (~> 3.4)
27
+ rack (1.6.4)
28
+ rake (10.4.2)
29
+ rspec (3.3.0)
30
+ rspec-core (~> 3.3.0)
31
+ rspec-expectations (~> 3.3.0)
32
+ rspec-mocks (~> 3.3.0)
33
+ rspec-core (3.3.2)
34
+ rspec-support (~> 3.3.0)
35
+ rspec-expectations (3.3.1)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.3.0)
38
+ rspec-mocks (3.3.2)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.3.0)
41
+ rspec-support (3.3.0)
42
+ slop (3.6.0)
43
+ thread_safe (0.3.5)
44
+ tilt (1.3.7)
45
+ tzinfo (1.2.2)
46
+ thread_safe (~> 0.1)
47
+
48
+ PLATFORMS
49
+ ruby
50
+
51
+ DEPENDENCIES
52
+ activesupport
53
+ bundler (~> 1.5)
54
+ double_write_cache_stores!
55
+ pry
56
+ rack
57
+ rake
58
+ rspec
59
+ tilt (= 1.3.7)
60
+
61
+ BUNDLED WITH
62
+ 1.10.6
@@ -1,29 +1,28 @@
1
1
  # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
2
+ lib = File.expand_path("../lib", __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'double_write_cache_stores/version'
4
+ require "double_write_cache_stores/version"
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "double_write_cache_stores"
8
8
  spec.version = DoubleWriteCacheStores::VERSION
9
9
  spec.authors = ["hirocaster"]
10
10
  spec.email = ["hohtsuka@gmail.com"]
11
- spec.summary = %q{ Double write cache stores wrapper. }
12
- spec.description = %q{ pre-warning(double write to cach store and other cache store) cache store wrapper. will switch cache store. }
11
+ spec.summary = "Double write cache stores wrapper."
12
+ spec.description = "pre-warming(double write to cach store and other cache store) cache store wrapper. will switch cache store."
13
13
  spec.homepage = ""
14
14
  spec.license = "MIT"
15
15
 
16
- spec.files = `git ls-files`.split($/)
16
+ spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_runtime_dependency "dalli", "~> 2.7.4"
22
+
21
23
  spec.add_development_dependency "bundler", "~> 1.5"
22
24
  spec.add_development_dependency "rake"
23
25
  spec.add_development_dependency "rspec"
24
- spec.add_development_dependency "activesupport", "= 3.2.15"
25
- spec.add_development_dependency "dalli", "= 2.7.0"
26
26
  spec.add_development_dependency "pry"
27
- spec.add_development_dependency "padrino", "0.10.7"
28
27
  spec.add_development_dependency "tilt", "1.3.7"
29
28
  end
@@ -1,11 +1,15 @@
1
1
  # monky patch
2
2
  # support cas interface for ActiveSupport::Cache::DalliStore
3
3
  module DalliStorePatch
4
- def read_cas(name, options=nil)
5
- options ||= {}
4
+ def touch(key, ttl = nil)
5
+ ttl ||= options[:expires_in].to_i
6
+ @data.touch key, ttl
7
+ end
8
+
9
+ def read_cas(name, options = {})
6
10
  name = namespaced_key(name, options)
7
11
 
8
- instrument(:get_cas, name) do |payload|
12
+ instrument(:get_cas, name) do |_payload|
9
13
  with { |c| c.get_cas(name) }
10
14
  end
11
15
  rescue Dalli::DalliError => e
@@ -14,12 +18,11 @@ module DalliStorePatch
14
18
  false
15
19
  end
16
20
 
17
- def write_cas(name, value, options=nil)
18
- options ||= {}
21
+ def write_cas(name, value, options = {})
19
22
  name = namespaced_key(name, options)
20
23
  expires_in = options[:expires_in]
21
24
 
22
- instrument(:set_cas, name, value) do |payload|
25
+ instrument(:set_cas, name, value) do |_payload|
23
26
  cas = options.delete(:cas) || 0
24
27
  expires_in = options.delete(:expires_in)
25
28
  with { |c| c.set_cas(name, value, cas, expires_in, options) }
@@ -31,11 +34,9 @@ module DalliStorePatch
31
34
  end
32
35
  end
33
36
 
34
-
35
37
  begin
36
- require "active_support"
37
38
  require "active_support/cache/dalli_store"
38
39
 
39
40
  ActiveSupport::Cache::DalliStore.send(:include, DalliStorePatch)
40
- rescue => exception
41
+ rescue LoadError # rubocop:disable Lint/HandleExceptions
41
42
  end
@@ -1,6 +1,26 @@
1
+ module DoubleWriteCacheStores
2
+ loaded_active_support = false
3
+
4
+ begin
5
+ require "active_support"
6
+ loaded_active_support = true
7
+ rescue LoadError # rubocop:disable Lint/HandleExceptions
8
+ end
9
+
10
+ loaded_active_support.freeze
11
+
12
+ LOADED_ACTIVE_SUPPORT = loaded_active_support
13
+
14
+ def self.loaded_active_support?
15
+ LOADED_ACTIVE_SUPPORT
16
+ end
17
+ end
18
+
1
19
  require "double_write_cache_stores/version"
2
20
  require "double_write_cache_stores/client"
3
21
  require "double_write_cache_stores/base_exception"
22
+
23
+ require "dalli"
4
24
  require "dalli_store_patch"
5
- module DoubleWriteCacheStores
6
- end
25
+
26
+ require "mem_cache_store_patch" if DoubleWriteCacheStores.loaded_active_support?
@@ -1,217 +1,193 @@
1
- class DoubleWriteCacheStores::Client
2
- def initialize(read_and_write_store_servers, write_only_store_servers = nil)
3
- @read_and_write_store = read_and_write_store_servers
4
- if write_only_store_servers
5
- if read_and_write_store_servers.class != write_only_store_servers.class
6
- fail "different cache store instance. #{read_and_write_store_servers.class} != #{write_only_store_servers.class}"
1
+ module DoubleWriteCacheStores
2
+ class Client # rubocop:disable Metrics/ClassLength
3
+ attr_accessor :read_and_write_store, :write_only_store
4
+
5
+ def initialize(read_and_write_store_servers, write_only_store_servers = nil)
6
+ @read_and_write_store = read_and_write_store_servers
7
+ if write_only_store_servers
8
+ if read_and_write_store_servers.class != write_only_store_servers.class
9
+ raise "different cache store instance. #{read_and_write_store_servers.class} != #{write_only_store_servers.class}"
10
+ end
11
+ @write_only_store = write_only_store_servers
7
12
  end
8
- @write_only_store = write_only_store_servers
9
13
  end
10
- end
11
-
12
- def [](key)
13
- get key
14
- end
15
-
16
- def get(key)
17
- get_or_read_method_call key
18
- end
19
-
20
- def get_multi(*keys)
21
- get_multi_or_read_multi_method_call *keys
22
- end
23
14
 
24
- def get_cas(key)
25
- if @read_and_write_store.respond_to? :get_cas
26
- @read_and_write_store.get_cas key
27
- elsif @read_and_write_store.respond_to? :read_cas
28
- @read_and_write_store.read_cas key
15
+ def [](key)
16
+ get key
29
17
  end
30
- end
31
18
 
32
- def set_cas(key, value, cas=0, options=nil)
33
- cas_unique = if @read_and_write_store.respond_to? :set_cas
34
- @read_and_write_store.set_cas key, value, cas, options
35
- elsif @read_and_write_store.respond_to? :read_cas
36
- options ||= {}
37
- options[:cas] = cas
38
- @read_and_write_store.write_cas key, value, options
39
- end
40
-
41
- if @write_only_store && cas_unique
42
- set_or_write_method_call @write_only_store, key, value, options
19
+ def get_cas(key)
20
+ if @read_and_write_store.respond_to? :get_cas
21
+ @read_and_write_store.get_cas key
22
+ elsif @read_and_write_store.respond_to? :read_cas
23
+ @read_and_write_store.read_cas key
24
+ end
43
25
  end
44
26
 
45
- cas_unique
46
- end
47
-
48
- def read(key)
49
- get_or_read_method_call key
50
- end
51
-
52
- def read_multi(*keys)
53
- get_multi_or_read_multi_method_call *keys
54
- end
55
-
56
- def delete(key)
57
- @read_and_write_store.delete key
58
- @write_only_store.delete key if @write_only_store
59
- end
60
-
61
- def []=(key, value)
62
- set key, value
63
- end
27
+ def set_cas(key, value, cas = 0, options = nil)
28
+ cas_unique = if @read_and_write_store.respond_to? :set_cas
29
+ @read_and_write_store.set_cas key, value, cas, options
30
+ elsif @read_and_write_store.respond_to? :read_cas
31
+ options ||= {}
32
+ options[:cas] = cas
33
+ @read_and_write_store.write_cas key, value, options
34
+ end
35
+
36
+ if @write_only_store && cas_unique
37
+ set_or_write_method_call @write_only_store, key, value, options
38
+ end
64
39
 
65
- def set(key, value, options = nil)
66
- write_cache_store key, value, options
67
- end
40
+ cas_unique
41
+ end
68
42
 
69
- def write(key, value, options = nil)
70
- write_cache_store key, value, options
71
- end
43
+ def delete(key)
44
+ result = @read_and_write_store.delete key
45
+ @write_only_store.delete key if @write_only_store
46
+ result
47
+ end
72
48
 
73
- def touch(key, ttl=nil)
74
- result = false
75
- if defined?(Dalli) && @read_and_write_store.is_a?(Dalli::Client)
76
- result = @read_and_write_store.touch key, ttl
77
- else
78
- read_and_write_backend = @read_and_write_store.instance_variable_get('@backend') || @read_and_write_store.instance_variable_get('@data')
79
- if read_and_write_backend && read_and_write_backend.respond_to?(:touch)
80
- result = read_and_write_backend.touch key, ttl
81
- write_only_store_touch key, ttl
82
- end
49
+ def []=(key, value)
50
+ set key, value
83
51
  end
84
- result
85
- end
86
52
 
87
- def flush
88
- if flush_cache_store || flush_cache_store(:clear)
89
- true
90
- else
91
- false
53
+ def touch(key, ttl = nil)
54
+ result = @read_and_write_store.touch key, ttl
55
+ @write_only_store.touch key, ttl if @write_only_store
56
+ result
92
57
  end
93
- end
94
58
 
95
- def fetch(name, options = nil)
96
- if @read_and_write_store.respond_to?(:fetch) ||
97
- (@write_only_store && @write_only_store.respond_to?(:fetch))
98
- if block_given?
99
- result = @read_and_write_store.fetch(name, options = nil) { yield }
100
- @write_only_store.fetch(name, options = nil) { yield } if @write_only_store
101
- result
59
+ def flush
60
+ if flush_cache_store || flush_cache_store(:clear)
61
+ true
102
62
  else
103
- result = @read_and_write_store.fetch(name, options = nil)
104
- @write_only_store.fetch(name, options = nil) if @write_only_store
105
- result
63
+ false
106
64
  end
107
- else
108
- raise UnSupportException.new "Unsupported #fetch from client object."
109
65
  end
110
- end
111
66
 
112
- def increment(key, amount = 1, options = {})
113
- increment_cache_store key, amount, options
114
- end
115
- alias_method :incr, :increment
67
+ def fetch(name, options = {}, &_block)
68
+ raise UnSupportException "Unsupported #fetch from client object." unless @read_and_write_store.respond_to?(:fetch)
116
69
 
117
- def decrement(key, amount = 1, options = {})
118
- decrement_cache_store key, amount, options
119
- end
120
- alias_method :decr, :decrement
121
-
122
- private
123
-
124
- def write_cache_store(key, value, options = nil)
125
- set_or_write_method_call @read_and_write_store, key, value, options
126
- set_or_write_method_call @write_only_store, key, value, options if @write_only_store
127
- end
70
+ delete name if options[:force]
128
71
 
129
- def set_or_write_method_call cache_store, key, value, options
130
- if cache_store.respond_to? :set
131
- if defined?(Dalli) && cache_store.is_a?(Dalli::Client)
132
- ttl = options[:expires_in] if options
133
- cache_store.set key, value, ttl, options
72
+ if options[:race_condition_ttl]
73
+ fetch_race_condition name, options { yield }
134
74
  else
135
- cache_store.set key, value, options
75
+ unless value = get_or_read_method_call(name)
76
+ value = yield
77
+ write_cache_store name, value, options
78
+ end
79
+ value
136
80
  end
137
- elsif cache_store.respond_to? :write
138
- cache_store.write key, value, options
139
81
  end
140
- end
141
82
 
142
- def get_or_read_method_call key
143
- if @read_and_write_store.respond_to? :get
144
- @read_and_write_store.get key
145
- elsif @read_and_write_store.respond_to? :read
146
- @read_and_write_store.read key
83
+ def increment(key, amount = 1, options = {})
84
+ increment_cache_store key, amount, options
147
85
  end
148
- end
86
+ alias_method :incr, :increment
149
87
 
150
- def get_multi_or_read_multi_method_call(*keys)
151
- if @read_and_write_store.respond_to? :get_multi
152
- @read_and_write_store.get_multi *keys
153
- elsif @read_and_write_store.respond_to? :read_multi
154
- @read_and_write_store.read_multi *keys
155
- else
156
- raise UnSupportException.new "Unsupported multi keys get or read from client object."
88
+ def decrement(key, amount = 1, options = {})
89
+ decrement_cache_store key, amount, options
157
90
  end
158
- end
91
+ alias_method :decr, :decrement
159
92
 
160
- def increment_cache_store(key, amount, options)
161
- rw_store_value = incr_or_increment_method_call @read_and_write_store, key, amount, options
162
- return rw_store_value unless @write_only_store
163
- incr_or_increment_method_call @write_only_store, key, amount, options
164
- end
93
+ def write_cache_store(key, value, options = nil)
94
+ set_or_write_method_call @read_and_write_store, key, value, options
95
+ set_or_write_method_call @write_only_store, key, value, options if @write_only_store
96
+ end
97
+ alias_method :set, :write_cache_store
98
+ alias_method :write, :write_cache_store
99
+
100
+ def get_or_read_method_call(key)
101
+ if @read_and_write_store.respond_to? :get
102
+ @read_and_write_store.get key
103
+ elsif @read_and_write_store.respond_to? :read
104
+ @read_and_write_store.read key
105
+ end
106
+ end
107
+ alias_method :get, :get_or_read_method_call
108
+ alias_method :read, :get_or_read_method_call
109
+
110
+ def get_multi_or_read_multi_method_call(*keys)
111
+ if @read_and_write_store.respond_to? :get_multi
112
+ @read_and_write_store.get_multi(*keys)
113
+ elsif @read_and_write_store.respond_to? :read_multi
114
+ @read_and_write_store.read_multi(*keys)
115
+ else
116
+ raise UnSupportException "Unsupported multi keys get or read from client object."
117
+ end
118
+ end
119
+ alias_method :get_multi, :get_multi_or_read_multi_method_call
120
+ alias_method :read_multi, :get_multi_or_read_multi_method_call
165
121
 
166
- def decrement_cache_store(key, amount, options)
167
- rw_store_value = decr_or_decrement_method_call @read_and_write_store, key, amount, options
168
- return rw_store_value unless @write_only_store
169
- decr_or_decrement_method_call @write_only_store, key, amount, options
170
- end
122
+ private
171
123
 
172
- def incr_or_increment_method_call(cache_store, key, amount, options)
173
- if defined?(Dalli) && cache_store.is_a?(Dalli::Client)
174
- ttl = options[:expires_in] if options
175
- default = options.has_key?(:initial) ? options[:initial] : amount
176
- cache_store.incr key, amount, ttl, default
177
- elsif cache_store.respond_to? :increment
178
- options[:initial] = amount unless options.has_key?(:initial)
179
- cache_store.increment key, amount, options
180
- end
181
- end
124
+ def fetch_race_condition(key, options, &_block)
125
+ result = fetch_to_cache_store(@read_and_write_store, key, options) { yield }
126
+ fetch_to_cache_store(@write_only_store, key, options) { result } if @write_only_store && @write_only_store.respond_to?(:fetch)
127
+ result
128
+ end
182
129
 
183
- def decr_or_decrement_method_call(cache_store, key, amount, options)
184
- if defined?(Dalli) && cache_store.is_a?(Dalli::Client)
185
- ttl = options[:expires_in] if options
186
- default = options.has_key?(:initial) ? options[:initial] : 0
187
- cache_store.decr key, amount, ttl, default
188
- elsif cache_store.respond_to? :decrement
189
- options[:initial] = 0 unless options.has_key?(:initial)
190
- cache_store.decrement key, amount, options
191
- end
192
- end
130
+ def fetch_to_cache_store(cache_store, key, options, &_block)
131
+ if cache_store.is_a? Dalli::Client
132
+ ttl = options[:expires_in]
133
+ cache_store.fetch key, ttl, options { yield }
134
+ else
135
+ cache_store.fetch key, options { yield }
136
+ end
137
+ end
193
138
 
194
- def flush_cache_store(method = :flush)
195
- if @read_and_write_store.respond_to? method
196
- if @write_only_store && @write_only_store.respond_to?(method)
197
- @write_only_store.send method
139
+ def set_or_write_method_call(cache_store, key, value, options)
140
+ if cache_store.respond_to? :set
141
+ ttl = options[:expires_in] if options
142
+ cache_store.set key, value, ttl, options
143
+ elsif cache_store.respond_to? :write
144
+ cache_store.write key, value, options
145
+ end
198
146
  end
199
- @read_and_write_store.send method
200
- else
201
- false
202
- end
203
- end
204
147
 
205
- def write_only_store_touch(key, ttl)
206
- if @write_only_store
207
- if defined?(Dalli) && @write_only_store.is_a?(Dalli::Client)
208
- @write_only_store.touch key, ttl
209
- else
210
- write_only_backend = @write_only_store.instance_variable_get('@backend') || @write_only_store.instance_variable_get('@data')
211
- if write_only_backend
212
- write_only_backend.touch(key, ttl) if write_only_backend.respond_to?(:touch)
148
+ def increment_cache_store(key, amount, options)
149
+ rw_store_value = incr_or_increment_method_call @read_and_write_store, key, amount, options
150
+ return rw_store_value unless @write_only_store
151
+ incr_or_increment_method_call @write_only_store, key, amount, options
152
+ end
153
+
154
+ def decrement_cache_store(key, amount, options)
155
+ rw_store_value = decr_or_decrement_method_call @read_and_write_store, key, amount, options
156
+ return rw_store_value unless @write_only_store
157
+ decr_or_decrement_method_call @write_only_store, key, amount, options
158
+ end
159
+
160
+ def incr_or_increment_method_call(cache_store, key, amount, options)
161
+ ttl = options[:expires_in] if options
162
+ default = options.key?(:initial) ? options[:initial] : amount
163
+ if cache_store.is_a? Dalli::Client
164
+ cache_store.incr key, amount, ttl, default
165
+ elsif cache_store.respond_to? :increment
166
+ options[:initial] = amount unless options.key?(:initial)
167
+ cache_store.increment key, amount, options
168
+ end
169
+ end
170
+
171
+ def decr_or_decrement_method_call(cache_store, key, amount, options)
172
+ if cache_store.is_a?(Dalli::Client)
173
+ ttl = options[:expires_in] if options
174
+ default = options.key?(:initial) ? options[:initial] : 0
175
+ cache_store.decr key, amount, ttl, default
176
+ elsif cache_store.respond_to? :decrement
177
+ options[:initial] = 0 unless options.key?(:initial)
178
+ cache_store.decrement key, amount, options
179
+ end
180
+ end
181
+
182
+ def flush_cache_store(method = :flush)
183
+ if @read_and_write_store.respond_to? method
184
+ if @write_only_store && @write_only_store.respond_to?(method)
185
+ @write_only_store.send method
186
+ end
187
+ @read_and_write_store.send method
188
+ else
189
+ false
213
190
  end
214
191
  end
215
- end
216
192
  end
217
193
  end