double_write_cache_stores 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e749fc471df644f9c669db716fd8896d68d8902f
4
+ data.tar.gz: 864f2ecff4026e31e2aceda833c2ec1e38e661fe
5
+ SHA512:
6
+ metadata.gz: 1737393014aee79ee2594a702ac8c169d2e2ebd6c6ed2c151c54cf356ceaa6e6bc9cb41a507005189f716b9af6466e67c70e212fa2ead1208a7c640777c4859c
7
+ data.tar.gz: b3e72722462b2e50ca51d4a7f9b64d71f23337b37d480a281f14a25b83d1bcc9474c0e0ee0bdabf67e7bbe7ce8a5eb2eb6e4e8acaa5006fd5f9709f7e9406491
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ bin/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in double_write_cache_stores.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 hirocaster
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # DoubleWriteCacheStores
2
+
3
+ pre-warning(double write to cach store and other cache store) cache store wrapper. will switch cache store.
4
+
5
+ ## Support
6
+
7
+ - ActiveSupport::Cache::DalliStore(Dalli)
8
+ - Padrino::Cache::Store::Memcache
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'double_write_cache_stores'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install double_write_cache_stores
23
+
24
+ ## Usage
25
+
26
+ ### Padrino
27
+
28
+ `config/apps.rb`
29
+ ````
30
+ read_and_write_cache_store = ActiveSupport::Cache.lookup_store :dalli_store, 'localhost:11211'
31
+ write_only_cache_store = ActiveSupport::Cache.lookup_store :dalli_store, 'localhost:21211'
32
+
33
+ set :cache, DoubleWriteCacheStores::Client.new(read_and_write_cache_store, write_only_cache_store)
34
+ ````
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it ( http://github.com/hirocaster/double_write_cache_stores/fork )
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'double_write_cache_stores/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "double_write_cache_stores"
8
+ spec.version = DoubleWriteCacheStores::VERSION
9
+ spec.authors = ["hirocaster"]
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. }
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "activesupport", "= 3.2.15"
25
+ spec.add_development_dependency "dalli", "= 2.6.4"
26
+ spec.add_development_dependency "pry"
27
+ spec.add_development_dependency "padrino", "0.10.7"
28
+ spec.add_development_dependency "tilt", "1.3.7"
29
+ end
@@ -0,0 +1,4 @@
1
+ require "double_write_cache_stores/version"
2
+ require "double_write_cache_stores/client"
3
+ module DoubleWriteCacheStores
4
+ end
@@ -0,0 +1,76 @@
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}"
7
+ end
8
+ @write_only_store = write_only_store_servers
9
+ end
10
+ end
11
+
12
+ def get(key)
13
+ @read_and_write_store.get key
14
+ end
15
+
16
+ def read(key)
17
+ @read_and_write_store.read key
18
+ end
19
+
20
+ def delete(key)
21
+ @read_and_write_store.delete key
22
+ @write_only_store.delete key if @write_only_store
23
+ end
24
+
25
+ def set(key, value, options = nil)
26
+ write_cache_store __method__, key, value, options
27
+ end
28
+
29
+ def write(key, value, options = nil)
30
+ write_cache_store __method__, key, value, options
31
+ end
32
+
33
+ def touch(key)
34
+ result = false
35
+ read_and_write_backend = @read_and_write_store.instance_variable_get '@backend'
36
+ if read_and_write_backend && read_and_write_backend.respond_to?(:touch)
37
+ result = read_and_write_backend.touch key
38
+ write_only_store_touch key
39
+ end
40
+ result
41
+ end
42
+
43
+ def flush
44
+ if flush_cache_store || flush_cache_store(:clear)
45
+ true
46
+ else
47
+ false
48
+ end
49
+ end
50
+
51
+ private
52
+
53
+ def write_cache_store(method, key, value, options = nil)
54
+ @read_and_write_store.send method, key, value, options
55
+ @write_only_store.send method, key, value, options if @write_only_store
56
+ end
57
+
58
+ def flush_cache_store(method = :flush)
59
+ if @read_and_write_store.respond_to? method
60
+ if @write_only_store && @write_only_store.respond_to?(method)
61
+ @write_only_store.send method
62
+ end
63
+ @read_and_write_store.send method
64
+ else
65
+ false
66
+ end
67
+ end
68
+
69
+ def write_only_store_touch(key)
70
+ if @write_only_store
71
+ if write_only_backend = @write_only_store.instance_variable_get('@backend')
72
+ write_only_backend.touch key if write_only_backend.respond_to?(:touch)
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,3 @@
1
+ module DoubleWriteCacheStores
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,137 @@
1
+ require 'spec_helper'
2
+
3
+ describe DoubleWriteCacheStores::Client do
4
+ let :read_and_write_store do
5
+ ActiveSupport::Cache.lookup_store :dalli_store, 'localhost:11211'
6
+ end
7
+
8
+ let :write_only_store do
9
+ ActiveSupport::Cache.lookup_store :dalli_store, 'localhost:21211'
10
+ end
11
+
12
+ describe '#initialize' do
13
+ it 'different cache store instance' do
14
+ expect{ subject.new read_and_write_store, 'bad instance object' }.to raise_error
15
+ end
16
+ end
17
+
18
+ let :copy_cache_store do
19
+ DoubleWriteCacheStores::Client.new read_and_write_store, write_only_store
20
+ end
21
+
22
+ describe '#write' do
23
+ before do
24
+ copy_cache_store.write 'key', 'example-value', :expires_in => 1.day
25
+ end
26
+ it 'set value to multi store' do
27
+ expect(read_and_write_store.read 'key').to eq 'example-value'
28
+ expect(write_only_store.read 'key').to eq 'example-value'
29
+ end
30
+ end
31
+
32
+ describe '#delete' do
33
+ before do
34
+ copy_cache_store.write 'will-delete-key', 'example-will-delete-value', :expires_in => 1.day
35
+ end
36
+ it 'delete key-value' do
37
+ expect(read_and_write_store.read 'will-delete-key').to eq 'example-will-delete-value'
38
+ expect(write_only_store.read 'will-delete-key').to eq 'example-will-delete-value'
39
+
40
+ copy_cache_store.delete 'will-delete-key'
41
+
42
+ expect(read_and_write_store.read 'will-delete-key').to be_nil
43
+ expect(write_only_store.read 'will-delete-key').to be_nil
44
+ end
45
+ end
46
+
47
+ describe '#touch' do
48
+ let :options do
49
+ { :namespace => "app_v1", :compress => true }
50
+ end
51
+ let :support_touch_read_and_write_store do
52
+ Padrino::Cache::Store::Memcache.new(::Dalli::Client.new('localhost:11211', options))
53
+ end
54
+ let :support_touch_write_only_store do
55
+ Padrino::Cache::Store::Memcache.new(::Dalli::Client.new('localhost:21211', options))
56
+ end
57
+ let :support_touch_copy_cache_store do
58
+ DoubleWriteCacheStores::Client.new support_touch_read_and_write_store, support_touch_write_only_store
59
+ end
60
+ before do
61
+ support_touch_copy_cache_store.set 'touch-key', 'touch-value', :expires_in => 1.day
62
+ end
63
+ it 'example' do
64
+ expect(support_touch_copy_cache_store.touch 'touch-key').to be_true
65
+ expect(support_touch_copy_cache_store.touch 'non-set-key').to be_nil
66
+ end
67
+ context 'when touch non support backend' do
68
+ before do
69
+ copy_cache_store.write 'unsupport-touch-key', 'touch-value', :expires_in => 1.day
70
+ end
71
+ it 'not doing touch' do
72
+ expect(copy_cache_store.touch 'unsupport-touch-key').to be_false
73
+ end
74
+ end
75
+ end
76
+
77
+ describe '#read' do
78
+ context 'when standard case' do
79
+ before do
80
+ copy_cache_store.write 'key', 'example-read-value', :expires_in => 1.day
81
+ end
82
+ it 'get read key value from multi store' do
83
+ expect(copy_cache_store.read 'key').to eq 'example-read-value'
84
+ end
85
+ it 'not get no set key-value' do
86
+ expect(copy_cache_store.read 'not-set-key').to be_nil
87
+ end
88
+ end
89
+ context 'when not set copy cache store' do
90
+ let :not_copy_cache_store do
91
+ DoubleWriteCacheStores::Client.new read_and_write_store
92
+ end
93
+ before do
94
+ not_copy_cache_store.write 'no-copy-key', 'example-read-value', :expires_in => 1.day
95
+ end
96
+ it 'not sync cache store' do
97
+ expect(read_and_write_store.read 'no-copy-key').to eq 'example-read-value'
98
+ expect(write_only_store.read 'no-copy-key').to be_nil
99
+ end
100
+ end
101
+ end
102
+
103
+ describe '#flush' do
104
+ context 'when not support flush method in cache store' do
105
+ before do
106
+ copy_cache_store.write 'will-flush-key', 'will-flush-value', :expires_in => 1.day
107
+ end
108
+ it 'example' do
109
+ expect(copy_cache_store.flush).to eq true
110
+ expect(copy_cache_store.read 'will-flush-key').to be_nil
111
+ end
112
+ end
113
+ context 'when support flush method in backend cache store' do
114
+ let :options do
115
+ { :namespace => "app_v1", :compress => true }
116
+ end
117
+ let :support_flash_read_and_write_store do
118
+ Padrino::Cache::Store::Memcache.new(::Dalli::Client.new('localhost:11211', options))
119
+ end
120
+ let :support_flash_write_only_store do
121
+ Padrino::Cache::Store::Memcache.new(::Dalli::Client.new('localhost:21211', options))
122
+ end
123
+ let :support_flash_copy_cache_store do
124
+ DoubleWriteCacheStores::Client.new support_flash_read_and_write_store, support_flash_write_only_store
125
+ end
126
+ before do
127
+ support_flash_copy_cache_store.set 'will-flush-key', 'will-flush-value', :expires_in => 1.day
128
+ end
129
+ it 'example' do
130
+ expect(support_flash_copy_cache_store.get 'will-flush-key').to eq 'will-flush-value'
131
+ expect(support_flash_copy_cache_store.flush).to be_true
132
+ expect(support_flash_copy_cache_store.get 'will-flush-key').to be_nil
133
+ end
134
+ end
135
+ end
136
+
137
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe DoubleWriteCacheStores do
4
+ it 'should have a version number' do
5
+ DoubleWriteCacheStores::VERSION.should_not be_nil
6
+ end
7
+ end
@@ -0,0 +1,26 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'double_write_cache_stores'
3
+ require 'active_support'
4
+ require 'dalli'
5
+ require 'pry'
6
+ require 'padrino-cache'
7
+
8
+ PID_DIR = File.expand_path(File.dirname(__FILE__) + "/../tmp/pids")
9
+ PORTS = [11211, 21211]
10
+
11
+ RSpec.configure do |conf|
12
+
13
+ FileUtils.mkdir_p PID_DIR
14
+
15
+ PORTS.each do |port|
16
+ pid = File.expand_path(PID_DIR + "/memcached-spec-#{port}.pid")
17
+ %x{ memcached -d -p #{port} -P #{pid} }
18
+ end
19
+
20
+ conf.after(:suite) do
21
+ PORTS.each do |port|
22
+ pid = File.expand_path(PID_DIR + "/memcached-spec-#{port}.pid")
23
+ %x{ cat #{pid} | xargs kill -9 }
24
+ end
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,174 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: double_write_cache_stores
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - hirocaster
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 3.2.15
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 3.2.15
69
+ - !ruby/object:Gem::Dependency
70
+ name: dalli
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 2.6.4
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 2.6.4
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: padrino
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '='
102
+ - !ruby/object:Gem::Version
103
+ version: 0.10.7
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '='
109
+ - !ruby/object:Gem::Version
110
+ version: 0.10.7
111
+ - !ruby/object:Gem::Dependency
112
+ name: tilt
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '='
116
+ - !ruby/object:Gem::Version
117
+ version: 1.3.7
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '='
123
+ - !ruby/object:Gem::Version
124
+ version: 1.3.7
125
+ description: " pre-warning(double write to cach store and other cache store) cache
126
+ store wrapper. will switch cache store. "
127
+ email:
128
+ - hohtsuka@gmail.com
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - ".rspec"
135
+ - ".travis.yml"
136
+ - Gemfile
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - double_write_cache_stores.gemspec
141
+ - lib/double_write_cache_stores.rb
142
+ - lib/double_write_cache_stores/client.rb
143
+ - lib/double_write_cache_stores/version.rb
144
+ - spec/double_write_cache_stores/client_spec.rb
145
+ - spec/double_write_cache_stores_spec.rb
146
+ - spec/spec_helper.rb
147
+ homepage: ''
148
+ licenses:
149
+ - MIT
150
+ metadata: {}
151
+ post_install_message:
152
+ rdoc_options: []
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ requirements: []
166
+ rubyforge_project:
167
+ rubygems_version: 2.2.0
168
+ signing_key:
169
+ specification_version: 4
170
+ summary: Double write cache stores wrapper.
171
+ test_files:
172
+ - spec/double_write_cache_stores/client_spec.rb
173
+ - spec/double_write_cache_stores_spec.rb
174
+ - spec/spec_helper.rb