Skip to content

Commit da69671

Browse files
committed
small performance improvements
1 parent 3795173 commit da69671

File tree

7 files changed

+47
-53
lines changed

7 files changed

+47
-53
lines changed

lib/grape/cache.rb

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
# frozen_string_literal: true
22

3+
require 'active_support/cache'
4+
require 'active_support/concern'
35
require 'grape'
6+
require 'rack'
47

58
require 'grape/cache/configurable'
9+
require 'grape/cache/dsl'
10+
require 'grape/cache/helpers'
11+
require 'grape/cache/version'
12+
613
require 'grape/cache/extensions/dsl'
714
require 'grape/cache/extensions/instance'
815
require 'grape/cache/extensions/middleware/formatter'
9-
require 'grape/cache/helpers'
10-
require 'grape/cache/version'
1116

1217
module Grape
1318
# @nodoc
@@ -21,16 +26,16 @@ def self.read(key)
2126
end
2227

2328
# @param key [String] cache key
24-
# @param response [Object] response object
29+
# @param value [Object] response object
2530
# @param options [Object] (see ActiveSupport::Cache#write)
2631
# @return [Boolean] operation status
27-
def self.store(key, response, **options)
28-
return false unless response
32+
def self.write(key, value, **options)
33+
return false unless value
2934

3035
options.delete(:expires_in) if options[:expires_in]&.<=(0)
31-
config.backend.write(key, response, options)
36+
config.backend.write(key, value, options)
3237
rescue StandardError => e
33-
ActiveSupport::Notifications.instrument('grape_cache.store_error', error: e)
38+
ActiveSupport::Notifications.instrument('grape_cache.write_error', error: e)
3439
false
3540
end
3641
end

lib/grape/cache/configurable.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# frozen_string_literal: true
22

3-
require 'active_support/cache'
4-
require 'active_support/concern'
5-
63
module Grape
74
module Cache
85
# @nodoc

lib/grape/cache/dsl.rb

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,18 @@ def no_cache?
2727
end
2828

2929
def to_s
30-
options = []
31-
32-
options << (public? ? 'public' : 'private')
33-
options << (no_cache? ? 'no-cache' : ['max-age', max_age].join('='))
34-
options << 'must-revalidate' if must_revalidate?
35-
36-
options.join(', ')
30+
[
31+
(public? ? 'public' : 'private'),
32+
(no_cache? ? 'no-cache' : "max-age=#{max_age}"),
33+
('must-revalidate' if must_revalidate?)
34+
].compact.join(', ')
3735
end
3836
end
3937

40-
delegate :[], :[]=, :fetch, :slice, to: :@storage
38+
delegate :[], :[]=, :fetch, :slice, to: :@store
4139

4240
def initialize
43-
@storage = {
41+
@store = {
4442
expires_in: 0,
4543
race_condition_ttl: 5.seconds.to_i,
4644
cache_control: 'private, max-age=0, must-revalidate'
@@ -49,19 +47,19 @@ def initialize
4947

5048
# @return [void]
5149
def key(value = nil, &block)
52-
self[:key] = value || block
50+
@store[:key] = value || block
5351
end
5452

5553
# @param seconds [Integer] cache TTL
5654
# @return [void]
5755
def expires_in(seconds)
58-
self[:expires_in] = seconds.to_i
56+
@store[:expires_in] = seconds.to_i
5957
end
6058

6159
# @param seconds [Integer] cache race condition TTL
6260
# @return [void]
6361
def race_condition_ttl(seconds)
64-
self[:race_condition_ttl] = seconds.to_i
62+
@store[:race_condition_ttl] = seconds.to_i
6563
end
6664

6765
# @param options [Hash] parameters for "Cache-Control" header
@@ -70,8 +68,9 @@ def race_condition_ttl(seconds)
7068
# @option options :max_age [Integer] defaults to "expires_in"
7169
# @return [void]
7270
def cache_control(options = {})
73-
self[:cache_control] =
74-
CacheControl.new(options.reverse_merge(max_age: self[:expires_in])).to_s
71+
@store[:cache_control] = CacheControl.new(
72+
options.reverse_merge(max_age: @store[:expires_in])
73+
).to_s
7574
end
7675
end
7776
end

lib/grape/cache/extensions/dsl.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
# frozen_string_literal: true
22

3-
require 'active_support/concern'
4-
5-
require 'grape/cache/dsl'
6-
73
module Grape
84
module Cache
95
module Extensions

lib/grape/cache/extensions/instance.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@ def generate_cached_api_method(context, &block) # rubocop:disable Metrics/Method
1818

1919
key = context[:key]
2020
key = key.is_a?(Proc) ? instance_exec(&key) : key
21-
key = Grape::Cache::Helpers.expand_cache_key(env, *key)
21+
key = Grape::Cache::Helpers.expand_cache_key(env, key)
2222

23-
cached_response = Grape::Cache.read(key)
24-
(cached_response || instance_exec(&block)).tap do |response|
25-
env['grape-cache'] = {
26-
key: key,
27-
value: response,
28-
exists: !cached_response.nil?,
29-
options: context.slice(:expires_in, :race_condition_ttl).compact
30-
}
23+
env['grape-cache'] = { key: key }
24+
25+
if (value = Grape::Cache.read(key))
26+
env['grape-cache'][:hit] = true
27+
env['grape-cache'][:value] = value
28+
else
29+
env['grape-cache'][:hit] = false
30+
env['grape-cache'][:options] = context.slice(:expires_in, :race_condition_ttl).compact
31+
instance_exec(&block)
3132
end
3233
end
3334
end

lib/grape/cache/extensions/middleware/formatter.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ def fetch_formatter(*)
1010
formatter = super
1111

1212
lambda do |body, env|
13-
context = env['grape-cache']
13+
cache = env['grape-cache']
1414

15-
return formatter.call(body, env) unless context
16-
return context[:value] if context[:exists]
15+
return formatter.call(body, env) unless cache
16+
return cache[:value] if cache[:hit]
1717

1818
formatter.call(body, env).tap do |response|
19-
Grape::Cache.store(context[:key], response, **context[:options])
19+
Grape::Cache.write(cache[:key], response, **cache[:options])
2020
end
2121
end
2222
end

lib/grape/cache/helpers.rb

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
# frozen_string_literal: true
22

3-
require 'active_support/cache'
4-
require 'rack'
5-
63
module Grape
74
module Cache
85
# @nodoc
96
module Helpers
107
module_function
118

129
# @param env [Object] request env
13-
# @param *args [String, Integer] parameters which will be concatenated into cache key
10+
# @param key [#to_s, Array<#to_s>] any object or array of objects that respond to `#to_s`
1411
# @return [String] cache key
15-
def expand_cache_key(env, *args)
16-
keys = [
17-
env.fetch(Rack::REQUEST_METHOD),
18-
env.fetch(Rack::PATH_INFO),
19-
'-',
20-
*args
21-
].compact
12+
def expand_cache_key(env, key)
13+
key = [
14+
env[Rack::REQUEST_METHOD],
15+
env[Rack::PATH_INFO],
16+
'-'
17+
] + Array(key).compact
2218

23-
ActiveSupport::Cache.expand_cache_key(keys)
19+
ActiveSupport::Cache.expand_cache_key(key)
2420
end
2521
end
2622
end

0 commit comments

Comments
 (0)