redis_set_store 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support"
4
+ require "redis"
5
+ require "redis-activesupport"
6
+
7
+ require "redis_set_store/version"
8
+ require "active_support/cache/redis_set_store"
9
+ require "redis_set_store/railtie" if defined? Rails
10
+
11
+ # A Rails cache implementation that is backed by redis and uses sets to track
12
+ # keys for rapid expiration of large numbers of keys.
13
+ module RedisSetStore
14
+ mattr_accessor :logger, :cache
15
+ end
16
+
17
+ unless defined? Rails
18
+ RedisSetStore.logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))
19
+ RedisSetStore.cache = ActiveSupport::Cache::RedisSetStore.new(/\A[^:]+:\d+/)
20
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RedisSetStore
4
+ # Bootstrap data into your cache to start using the RedisSetStore.
5
+ #
6
+ # RedisSetStore depends on looking up cache keys in redis sets to limit the
7
+ # search scope when matching keys or deleting matched keys. If you have an
8
+ # existing cache, then those set entries won't exist for the existing cache
9
+ # entries and the RedisSetStore#matched and RedisSetStore#delete_matched
10
+ # methods won't find any keys.
11
+ #
12
+ # To address that you can bootstrap your existing cache using this utility.
13
+ #
14
+ # require "redis_set_store/bootstrap"
15
+ # bootstrap = RedisSetStore::Bootstrap.new
16
+ # bootstrap.bootstrap_set("user:1")
17
+ # bootstrap.bootstrap_set("user:2")
18
+ #
19
+ # Running this will use the redis KEYS method which is very slow (the reason
20
+ # for this gem), so ideally set up a copy or slave of your cache redis
21
+ # instance to use as a read source so you don't affect your running cache.
22
+ # You can the configure the source for migration.
23
+ #
24
+ # require "redis_set_store/bootstrap"
25
+ # slave = Redis.new(host: "10.0.1.1")
26
+ # bootstrap = RedisSetStore::Bootstrap.new(source: slave)
27
+ # bootstrap.bootstrap_set("user:1")
28
+ # bootstrap.bootstrap_set("user:2")
29
+ class Bootstrap
30
+ # Creates a new Bootstrap utility.
31
+ #
32
+ # source: The source Redis instance. If not provided uses
33
+ # the destination redis or RedisSetStore.cache.redis, which
34
+ # under Rails is the configure Rails.cache store.
35
+ #
36
+ # destination: The destination Redis instance. If not provided uses
37
+ # RedisSetStore.cache.redis as a default, which under Rails
38
+ # is the configure Rails.cache store.
39
+ def initialize(source: nil, destination: nil)
40
+ @source = source || destination || RedisSetStore.cache.redis
41
+ @destination = destination || RedisSetStore.cache.redis
42
+ end
43
+
44
+ # Bootstrap a redis set for the matching keys in the cache.
45
+ #
46
+ # prefix: The prefix to match for keys and to use as the set key.
47
+ #
48
+ # Reads all keys from the `source` redis instance that match the prefix
49
+ # (followed by ":") and records them in the set used by RedisSetStore.
50
+ # The prefix is expected to the be the same as the `set_owner_regexp`
51
+ # parameters for ActiveSupport::Cache::RedisSetStore.
52
+ #
53
+ # Note that because this uses the KEYS method in Redis it scans _every key_
54
+ # in the database each time it is called which can be slow and will lock
55
+ # your database. You should configure a separate redis instance to read
56
+ # from as described in RedisSetStore::Bootstrap or run this with the
57
+ # cache offline.
58
+ def bootstrap_set(prefix)
59
+ keys = @source.keys "#{prefix}:*"
60
+ return if keys.empty?
61
+ set_manager.sadd(prefix, keys)
62
+ end
63
+
64
+ private
65
+
66
+ def set_manager
67
+ return @set_manager if @set_manager
68
+
69
+ if defined?(ActiveSupport::Cache::RedisSetStore::SetOwner::STORE)
70
+ return @set_manager = ActiveSupport::Cache::RedisSetStore::SetOwner::STORE
71
+ end
72
+
73
+ @set_manager = @destination
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RedisSetStore
4
+ class Railtie < Rails::Railtie
5
+ initializer "Blueprint::Cache logger" do
6
+ RedisSetStore.logger = Rails.logger
7
+ end
8
+
9
+ initializer "Blueprint::Cache cache" do
10
+ RedisSetStore.cache = Rails.cache
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RedisSetStore
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "redis_set_store/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "redis_set_store"
9
+ spec.version = RedisSetStore::VERSION
10
+ spec.authors = ["Jeremy Wadsack"]
11
+ spec.email = ["jeremy.wadsack@gmail.com"]
12
+ spec.summary = "A Rails cache implementation, backed by redis, for rapid expiration of lots of keys"
13
+ spec.description = "A Rails cache implementation that is backed by redis and uses sets to track keys for rapid "\
14
+ "expiration of large numbers of keys"
15
+ spec.homepage = "https://github.com/keylimetoolbox/redis_set_store"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = "bin"
22
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
23
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.15"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ spec.add_development_dependency "mocha", "~> 1"
30
+ spec.add_development_dependency "appraisal", "~> 2.0"
31
+ spec.add_development_dependency "rubocop", "~> 0"
32
+ spec.add_development_dependency "bundler-audit", "~> 0"
33
+
34
+ spec.add_dependency "activesupport", ">= 4.2"
35
+ spec.add_dependency "railties", ">= 4.2"
36
+ spec.add_dependency "redis-rails", "~> 5.0"
37
+ spec.add_dependency "redis", "~> 3.0"
38
+ spec.add_dependency "redis-store", "~> 1.1"
39
+ end
metadata ADDED
@@ -0,0 +1,240 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis_set_store
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeremy Wadsack
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-11 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.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mocha
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: appraisal
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
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: bundler-audit
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: activesupport
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '4.2'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '4.2'
125
+ - !ruby/object:Gem::Dependency
126
+ name: railties
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '4.2'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '4.2'
139
+ - !ruby/object:Gem::Dependency
140
+ name: redis-rails
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '5.0'
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '5.0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: redis
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '3.0'
160
+ type: :runtime
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '3.0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: redis-store
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: '1.1'
174
+ type: :runtime
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '1.1'
181
+ description: A Rails cache implementation that is backed by redis and uses sets to
182
+ track keys for rapid expiration of large numbers of keys
183
+ email:
184
+ - jeremy.wadsack@gmail.com
185
+ executables:
186
+ - console
187
+ - setup
188
+ extensions: []
189
+ extra_rdoc_files: []
190
+ files:
191
+ - ".gitignore"
192
+ - ".rspec"
193
+ - ".rubocop.yml"
194
+ - ".ruby-gemset"
195
+ - ".ruby-version"
196
+ - Appraisals
197
+ - Gemfile
198
+ - LICENSE
199
+ - README.md
200
+ - Rakefile
201
+ - bin/console
202
+ - bin/setup
203
+ - gemfiles/rails_4_2.gemfile
204
+ - gemfiles/rails_4_2.gemfile.lock
205
+ - gemfiles/rails_5_0.gemfile
206
+ - gemfiles/rails_5_0.gemfile.lock
207
+ - gemfiles/rails_5_1.gemfile
208
+ - gemfiles/rails_5_1.gemfile.lock
209
+ - lib/active_support/cache/redis_set_store.rb
210
+ - lib/redis_set_store.rb
211
+ - lib/redis_set_store/bootstrap.rb
212
+ - lib/redis_set_store/railtie.rb
213
+ - lib/redis_set_store/version.rb
214
+ - redis_set_store.gemspec
215
+ homepage: https://github.com/keylimetoolbox/redis_set_store
216
+ licenses:
217
+ - MIT
218
+ metadata: {}
219
+ post_install_message:
220
+ rdoc_options: []
221
+ require_paths:
222
+ - lib
223
+ required_ruby_version: !ruby/object:Gem::Requirement
224
+ requirements:
225
+ - - ">="
226
+ - !ruby/object:Gem::Version
227
+ version: '0'
228
+ required_rubygems_version: !ruby/object:Gem::Requirement
229
+ requirements:
230
+ - - ">="
231
+ - !ruby/object:Gem::Version
232
+ version: '0'
233
+ requirements: []
234
+ rubyforge_project:
235
+ rubygems_version: 2.6.11
236
+ signing_key:
237
+ specification_version: 4
238
+ summary: A Rails cache implementation, backed by redis, for rapid expiration of lots
239
+ of keys
240
+ test_files: []