redis-rack-cache 2.0.2 → 2.1.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: b1c23b6c5393e28ef68e57bfae874015956fdf8e
4
- data.tar.gz: 3e8ea63a5474411a7ea800342e398a2606141e5d
3
+ metadata.gz: 2b5230f97382eccbaacff83196ec80685cd91787
4
+ data.tar.gz: cdf39ba1459ec99a691e40343e924179cbe8e69c
5
5
  SHA512:
6
- metadata.gz: aa1e4180b9f1ddcfa75c76cf4f6666719e02b5cfd9a01e3972b7e2888869af7c04a954e3e760b3059b6dc3457dde510172f7c8af10da9dcd8b356a83ab248e8c
7
- data.tar.gz: b957047d38075303aa834d053e051917efc52b874d2070b7649ddb578c6aa3e3a2229dbbecc071f8f8d6127df8d69b0d30fe8124ea1e33655e061dae70646114
6
+ metadata.gz: b6f40646f5eecf1276afe30345d1e15f62cdc4cc04f924e1cddae8a689a965ac0146d5884795768e7bc3cf49cc146e0e1c479a2621b1aaf99babad2558eb4db3
7
+ data.tar.gz: 289a3c824840723a7e16c271a0c8451af2fadde18f8637089982ea93fc4be266f179eddbdb1877319c8a9fbcf31e4d8a973b802976f8f94bfa04b270ebccaec7
data/.travis.yml CHANGED
@@ -1,6 +1,5 @@
1
1
  language: ruby
2
- before_install: gem install bundler
3
- script: 'bundle exec rake'
2
+ script: bundle exec rake
4
3
  rvm:
5
4
  - 2.2
6
5
  - 2.3
data/CODEOWNERS ADDED
@@ -0,0 +1 @@
1
+ * @tubbo
data/Gemfile CHANGED
@@ -5,5 +5,3 @@ git_source :github do |name|
5
5
  repo = name =~ %r{/} ? name : "#{name}/#{name}"
6
6
  "https://github.com/#{repo}.git"
7
7
  end
8
-
9
- gem 'redis-store-testing', github: 'redis-store/testing'
data/README.md CHANGED
@@ -5,6 +5,12 @@
5
5
 
6
6
  __`redis-rack-cache`__ provides a Redis backed store for __Rack::Cache__, an HTTP cache. See the main [redis-store readme](https://github.com/redis-store/redis-store) for general guidelines.
7
7
 
8
+ **NOTE:** This gem is necessary in addition to
9
+ [redis-rails](https://github.com/redis-store/redis-rails) if you use
10
+ Redis to store the Rails cache. `redis-rails` does not pull in this gem
11
+ by default since not all applications use `Rack::Cache` as their HTTP
12
+ cache.
13
+
8
14
  ## Installation
9
15
 
10
16
  ```ruby
@@ -14,7 +20,34 @@ gem 'redis-rack-cache'
14
20
 
15
21
  ## Usage
16
22
 
17
- If you are using redis-store with Rails, consider using the [redis-rails gem](https://github.com/redis-store/redis-rails) instead. For standalone usage:
23
+ In a Rails app, you can configure your `Rack::Cache` stores like this:
24
+
25
+ ```ruby
26
+ # config/environments/production.rb
27
+ Rails.application.configure do
28
+ config.action_dispatch.rack_cache = {
29
+ metastore: "#{Rails.credentials.redis_url}/1/rack_cache_metastore",
30
+ entitystore: "#{Rails.credentials.redis_url}/1/rack_cache_entitystore"
31
+ # NOTE: `:meta_store` and `:entity_store` are also supported.
32
+ }
33
+ end
34
+ ```
35
+
36
+ For more complicated setups, like when using custom options, the
37
+ following syntax can also be used:
38
+
39
+ ```ruby
40
+ # config/environments/production.rb
41
+ Rails.application.configure do
42
+ config.action_dispatch.rack_cache = {
43
+ meta_store: ::Rack::Cache::MetaStore::Redis.new("#{Rails.credentials.redis_url}/1/rack_cache_metastore", default_ttl: 10.days.to_i),
44
+ entity_store: ::Rack::Cache::EntityStore::Redis.new("#{Rails.credentials.redis_url}/1/rack_cache_entitystore", default_ttl: 120.days.to_i)
45
+ # NOTE: `:metastore` and `:entitystore` are also supported.
46
+ }
47
+ end
48
+ ```
49
+
50
+ For standalone usage (in non-Rails apps):
18
51
 
19
52
  ```ruby
20
53
  # config.ru
@@ -0,0 +1,33 @@
1
+ require 'rack/utils'
2
+
3
+ module Rack
4
+ module Cache
5
+ module RedisBase
6
+ def self.included(base)
7
+ base.extend ClassMethods
8
+ base.instance_eval do
9
+ extend Rack::Utils
10
+
11
+ attr_reader :cache, :options, :default_ttl
12
+ end
13
+ end
14
+
15
+ module ClassMethods
16
+ def resolve(uri)
17
+ new ::Redis::Store::Factory.resolve(uri.to_s)
18
+ end
19
+ end
20
+
21
+ def initialize(server, options = {})
22
+ @cache = ::Redis::Store::Factory.create(server)
23
+ @options = options
24
+ @default_ttl = options[:default_ttl] || ::Redis::Rack::Cache::DEFAULT_TTL
25
+ end
26
+
27
+ def open(key)
28
+ data = read(key)
29
+ data && [data]
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,29 +1,12 @@
1
1
  require 'rack/cache/entity_store'
2
2
  require 'redis-rack-cache/constants'
3
+ require 'rack/cache/redis_base'
3
4
 
4
5
  module Rack
5
6
  module Cache
6
7
  class EntityStore
7
- class RedisBase < self
8
- # The underlying ::Redis instance used to communicate with the Redis daemon.
9
- attr_reader :cache
10
-
11
- extend Rack::Utils
12
-
13
- def open(key)
14
- data = read(key)
15
- data && [data]
16
- end
17
-
18
- def self.resolve(uri)
19
- new ::Redis::Store::Factory.resolve(uri.to_s)
20
- end
21
- end
22
-
23
- class Redis < RedisBase
24
- def initialize(server, options = {})
25
- @cache = ::Redis::Store::Factory.create(server)
26
- end
8
+ class Redis < self
9
+ include RedisBase
27
10
 
28
11
  def exist?(key)
29
12
  cache.exists key
@@ -35,10 +18,11 @@ module Rack
35
18
 
36
19
  def write(body, ttl=0)
37
20
  buf = StringIO.new
38
- key, size = slurp(body){|part| buf.write(part) }
21
+ key, size = slurp(body) {|part| buf.write(part) }
22
+ ttl = ttl.to_i.zero? ? default_ttl : ttl
39
23
 
40
- ttl = ::Redis::Rack::Cache::DEFAULT_TTL if ttl.zero?
41
- [key, size] if cache.setex(key, ttl, buf.string)
24
+ return unless cache.setex(key, ttl, buf.string)
25
+ [key, size]
42
26
  end
43
27
 
44
28
  def purge(key)
@@ -47,7 +31,7 @@ module Rack
47
31
  end
48
32
  end
49
33
 
50
- REDIS = Redis
34
+ REDIS = REDISS = Redis
51
35
  end
52
36
  end
53
37
  end
@@ -1,37 +1,21 @@
1
1
  require 'digest/sha1'
2
- require 'rack/utils'
3
2
  require 'rack/cache/key'
4
3
  require 'rack/cache/meta_store'
5
4
  require 'redis-rack-cache/constants'
5
+ require 'rack/cache/redis_base'
6
6
 
7
7
  module Rack
8
8
  module Cache
9
9
  class MetaStore
10
- class RedisBase < self
11
- extend Rack::Utils
12
-
13
- # The Redis::Store object used to communicate with the Redis daemon.
14
- attr_reader :cache
15
-
16
- def self.resolve(uri)
17
- new ::Redis::Store::Factory.resolve(uri.to_s)
18
- end
19
- end
20
-
21
- class Redis < RedisBase
22
- # The Redis instance used to communicated with the Redis daemon.
23
- attr_reader :cache
24
-
25
- def initialize(server, options = {})
26
- @cache = ::Redis::Store::Factory.create(server)
27
- end
10
+ class Redis < self
11
+ include RedisBase
28
12
 
29
13
  def read(key)
30
14
  cache.get(hexdigest(key)) || []
31
15
  end
32
16
 
33
17
  def write(key, entries, ttl=0)
34
- ttl = ::Redis::Rack::Cache::DEFAULT_TTL if ttl.zero?
18
+ ttl = ttl.to_i.zero? ? default_ttl : ttl
35
19
  cache.setex(hexdigest(key), ttl, entries)
36
20
  end
37
21
 
@@ -41,7 +25,7 @@ module Rack
41
25
  end
42
26
  end
43
27
 
44
- REDIS = Redis
28
+ REDIS = REDISS = Redis
45
29
  end
46
30
  end
47
31
  end
@@ -1,7 +1,7 @@
1
1
  class Redis
2
2
  module Rack
3
3
  module Cache
4
- VERSION = '2.0.2'
4
+ VERSION = '2.1.0'
5
5
  end
6
6
  end
7
7
  end
@@ -19,11 +19,11 @@ Gem::Specification.new do |s|
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ['lib']
21
21
 
22
- s.add_dependency 'redis-store', '>= 1.2', '< 2'
22
+ s.add_dependency 'redis-store', '>= 1.6', '< 2'
23
23
  s.add_dependency 'rack-cache', '>= 1.6', '< 2'
24
24
 
25
25
  s.add_development_dependency 'rake', '~> 10'
26
- s.add_development_dependency 'bundler', '~> 1.3'
26
+ s.add_development_dependency 'bundler'
27
27
  s.add_development_dependency 'mocha', '~> 0.14.0'
28
28
  s.add_development_dependency 'minitest', '~> 5'
29
29
  s.add_development_dependency 'redis-store-testing'
data/stdout ADDED
File without changes
@@ -11,6 +11,17 @@ describe Rack::Cache::EntityStore::Redis do
11
11
  @store = ::Rack::Cache::EntityStore::Redis.new :host => 'localhost'
12
12
  end
13
13
 
14
+ it 'respects the default_tll options' do
15
+ @store = ::Rack::Cache::EntityStore::Redis.new({ :host => 'localhost' }, { :default_ttl => 120 })
16
+ @store.default_ttl.must_equal(120)
17
+ end
18
+
19
+ it 'properly delegates the TTL to redis' do
20
+ @store = ::Rack::Cache::EntityStore::Redis.new({ :host => 'localhost' }, { :default_ttl => 120 })
21
+ key, size = @store.write(['She rode to the devil,'])
22
+ assert @store.cache.ttl(key) <= 120
23
+ end
24
+
14
25
  it 'has the class referenced by homonym constant' do
15
26
  ::Rack::Cache::EntityStore::REDIS.must_equal(::Rack::Cache::EntityStore::Redis)
16
27
  end
@@ -20,6 +31,10 @@ describe Rack::Cache::EntityStore::Redis do
20
31
  cache.must_be_kind_of(::Redis)
21
32
  cache.id.must_equal("redis://127.0.0.1:6379/0")
22
33
 
34
+ cache = ::Rack::Cache::EntityStore::Redis.resolve(uri("rediss://127.0.0.1")).cache
35
+ cache.must_be_kind_of(::Redis)
36
+ cache.instance_variable_get(:@client).scheme.must_equal('rediss')
37
+
23
38
  cache = ::Rack::Cache::EntityStore::Redis.resolve(uri("redis://127.0.0.1:6380")).cache
24
39
  cache.id.must_equal("redis://127.0.0.1:6380/0")
25
40
 
@@ -13,6 +13,26 @@ describe Rack::Cache::MetaStore::Redis do
13
13
  @entity_store.cache.flushall
14
14
  end
15
15
 
16
+ it "respects the default_tll options" do
17
+ @store = ::Rack::Cache::MetaStore::Redis.new({ :host => "localhost" }, { :default_ttl => 120 })
18
+ @store.default_ttl.must_equal(120)
19
+
20
+ @entity_store = ::Rack::Cache::EntityStore::Redis.new({ :host => "localhost" }, { :default_ttl => 120 })
21
+ @entity_store.default_ttl.must_equal(120)
22
+ end
23
+
24
+ it "properly delegates the TTL to redis" do
25
+ key = '/test/'
26
+ @store = ::Rack::Cache::MetaStore::Redis.new({ :host => "localhost" }, { :default_ttl => 120 })
27
+
28
+ @store.write(key, [[{},{}]])
29
+ assert @store.cache.ttl(key) <= 120
30
+
31
+ @entity_store = ::Rack::Cache::EntityStore::Redis.new({ :host => "localhost" }, { :default_ttl => 120 })
32
+ key, size = @entity_store.write(['She rode to the devil,'])
33
+ assert @entity_store.cache.ttl(key) <= 120
34
+ end
35
+
16
36
  it "has the class referenced by homonym constant" do
17
37
  ::Rack::Cache::MetaStore::REDIS.must_equal(::Rack::Cache::MetaStore::Redis)
18
38
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-rack-cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luca Guidi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-11 00:00:00.000000000 Z
11
+ date: 2019-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis-store
@@ -16,7 +16,7 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.2'
19
+ version: '1.6'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
22
  version: '2'
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: '1.2'
29
+ version: '1.6'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '2'
@@ -68,16 +68,16 @@ dependencies:
68
68
  name: bundler
69
69
  requirement: !ruby/object:Gem::Requirement
70
70
  requirements:
71
- - - "~>"
71
+ - - ">="
72
72
  - !ruby/object:Gem::Version
73
- version: '1.3'
73
+ version: '0'
74
74
  type: :development
75
75
  prerelease: false
76
76
  version_requirements: !ruby/object:Gem::Requirement
77
77
  requirements:
78
- - - "~>"
78
+ - - ">="
79
79
  - !ruby/object:Gem::Version
80
- version: '1.3'
80
+ version: '0'
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: mocha
83
83
  requirement: !ruby/object:Gem::Requirement
@@ -145,6 +145,7 @@ files:
145
145
  - ".gitignore"
146
146
  - ".travis.yml"
147
147
  - Appraisals
148
+ - CODEOWNERS
148
149
  - Gemfile
149
150
  - MIT-LICENSE
150
151
  - README.md
@@ -152,12 +153,14 @@ files:
152
153
  - bin/appraisal
153
154
  - gemfiles/rack_cache_1.6.gemfile
154
155
  - gemfiles/rack_cache_1.7.gemfile
156
+ - lib/rack/cache/redis_base.rb
155
157
  - lib/rack/cache/redis_entitystore.rb
156
158
  - lib/rack/cache/redis_metastore.rb
157
159
  - lib/redis-rack-cache.rb
158
160
  - lib/redis-rack-cache/constants.rb
159
161
  - lib/redis-rack-cache/version.rb
160
162
  - redis-rack-cache.gemspec
163
+ - stdout
161
164
  - test/rack/cache/entitystore/pony.jpg
162
165
  - test/rack/cache/entitystore/redis_test.rb
163
166
  - test/rack/cache/metastore/redis_test.rb
@@ -183,7 +186,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
183
186
  version: '0'
184
187
  requirements: []
185
188
  rubyforge_project: redis-rack-cache
186
- rubygems_version: 2.6.14
189
+ rubygems_version: 2.6.14.1
187
190
  signing_key:
188
191
  specification_version: 4
189
192
  summary: A Redis backend store for Rack::Cache