Skip to content

Commit 7500002

Browse files
authored
Merge pull request #515 from Shopify/update-rubocop-shopify
Update rubocop-shopify and correct offenses
2 parents 17afab8 + 5c5446b commit 7500002

File tree

84 files changed

+156
-56
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+156
-56
lines changed

Gemfile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
# frozen_string_literal: true
2+
23
source "https://rubygems.org"
34
gemspec
45

5-
# Skip rubocop 1.16.1 until the next release which will include the fix:
6-
# https://github.com/rubocop/rubocop/pull/9862
7-
gem "rubocop", "~> 1.5", "!= 1.16.1"
6+
gem "rubocop", "~> 1.5"
87

9-
gem "rubocop-shopify", "~> 2.0.1", require: false
8+
gem "rubocop-shopify", "~> 2.6.0", require: false
109

1110
gem "mysql2", "~> 0.5.3", platform: :mri
1211
gem "pg", ">= 0.18", "< 2.0", platform: :mri

Rakefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env rake
22
# frozen_string_literal: true
3+
34
require "bundler/gem_tasks"
45

56
require "rake/testtask"
@@ -23,7 +24,7 @@ end
2324

2425
desc("Update serialization format test fixture.")
2526
task :update_serialization_format do
26-
%w(mysql2 postgresql).each do |db|
27+
["mysql2", "postgresql"].each do |db|
2728
ENV["DB"] = db
2829
ruby "./test/helpers/update_serialization_format.rb"
2930
end

gemfiles/Gemfile.latest-release

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
source "https://rubygems.org"
22
gemspec path: ".."
33

4-
# Skip rubocop 1.16.1 until the next release which will include the fix:
5-
# https://github.com/rubocop/rubocop/pull/9862
6-
gem "rubocop", "~> 1.5", "!= 1.16.1"
7-
gem "rubocop-shopify", "~> 2.0.1", require: false
4+
gem "rubocop", "~> 1.5"
5+
gem "rubocop-shopify", "~> 2.6.0", require: false
86

97
gem "activerecord"
108
gem "activesupport"

identity_cache.gemspec

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- encoding: utf-8 -*-
22
# frozen_string_literal: true
3+
34
require File.expand_path("../lib/identity_cache/version", __FILE__)
45

56
Gem::Specification.new do |gem|
@@ -15,11 +16,11 @@ Gem::Specification.new do |gem|
1516
gem.email = ["gems@shopify.com"]
1617
gem.description = "Opt-in read through Active Record caching."
1718
gem.summary = "IdentityCache lets you specify how you want to cache your " \
18-
"model objects, at the model level, and adds a number of " \
19-
"convenience methods for accessing those objects through " \
20-
"the cache. Memcached is used as the backend cache store, " \
21-
"and the database is only hit when a copy of the object " \
22-
"cannot be found in Memcached."
19+
"model objects, at the model level, and adds a number of " \
20+
"convenience methods for accessing those objects through " \
21+
"the cache. Memcached is used as the backend cache store, " \
22+
"and the database is only hit when a copy of the object " \
23+
"cannot be found in Memcached."
2324
gem.homepage = "https://github.com/Shopify/identity_cache"
2425

2526
gem.files = Dir.chdir(File.expand_path(__dir__)) do
@@ -35,11 +36,11 @@ Gem::Specification.new do |gem|
3536

3637
gem.metadata["allowed_push_host"] = "https://rubygems.org"
3738

38-
gem.add_dependency("ar_transaction_changes", "~> 1.1")
3939
gem.add_dependency("activerecord", ">= 5.2")
40+
gem.add_dependency("ar_transaction_changes", "~> 1.1")
4041

41-
gem.add_development_dependency("rake", "~> 13.0")
42+
gem.add_development_dependency("minitest", "~> 5.14")
4243
gem.add_development_dependency("mocha", "~> 1.12")
44+
gem.add_development_dependency("rake", "~> 13.0")
4345
gem.add_development_dependency("spy", "~> 1.0")
44-
gem.add_development_dependency("minitest", "~> 5.14")
4546
end

lib/identity_cache.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
require "active_record"
34
require "active_support/core_ext/module/attribute_accessors"
45
require "ar_transaction_changes"
@@ -84,6 +85,7 @@ class << self
8485

8586
def append_features(base) # :nodoc:
8687
raise AlreadyIncludedError if base.include?(IdentityCache)
88+
8789
super
8890
end
8991

@@ -200,6 +202,7 @@ def with_fetch_read_only_records(value = true)
200202
def fetch_read_only_records
201203
v = Thread.current[:identity_cache_fetch_read_only_records]
202204
return v unless v.nil?
205+
203206
@fetch_read_only_records
204207
end
205208

@@ -209,9 +212,9 @@ def eager_load!
209212

210213
private
211214

212-
def fetch_in_batches(keys)
215+
def fetch_in_batches(keys, &block)
213216
keys.each_slice(BATCH_SIZE).each_with_object({}) do |slice, result|
214-
result.merge!(cache.fetch_multi(*slice) { |missed_keys| yield missed_keys })
217+
result.merge!(cache.fetch_multi(*slice, &block))
215218
end
216219
end
217220
end

lib/identity_cache/belongs_to_caching.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
module IdentityCache
34
module BelongsToCaching
45
extend ActiveSupport::Concern

lib/identity_cache/cache_fetcher.rb

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class FillLock
1515
class << self
1616
def from_cache(marker, client_id, data_version)
1717
raise ArgumentError unless marker == FILL_LOCKED
18+
1819
new(client_id: client_id, data_version: data_version)
1920
end
2021

@@ -69,13 +70,11 @@ def fetch_multi(keys, &block)
6970
results
7071
end
7172

72-
def fetch(key, fill_lock_duration: nil, lock_wait_tries: 2)
73+
def fetch(key, fill_lock_duration: nil, lock_wait_tries: 2, &block)
7374
if fill_lock_duration && IdentityCache.should_fill_cache?
74-
fetch_with_fill_lock(key, fill_lock_duration, lock_wait_tries) do
75-
yield
76-
end
75+
fetch_with_fill_lock(key, fill_lock_duration, lock_wait_tries, &block)
7776
else
78-
fetch_without_fill_lock(key) { yield }
77+
fetch_without_fill_lock(key, &block)
7978
end
8079
end
8180

@@ -88,16 +87,19 @@ def fetch_without_fill_lock(key)
8887
unless value.nil?
8988
return value
9089
end
90+
9191
data = yield
9292
break unless IdentityCache.should_fill_cache?
93+
9394
data
9495
end
9596
data
9697
end
9798

98-
def fetch_with_fill_lock(key, fill_lock_duration, lock_wait_tries)
99+
def fetch_with_fill_lock(key, fill_lock_duration, lock_wait_tries, &block)
99100
raise ArgumentError, "fill_lock_duration must be greater than 0.0" unless fill_lock_duration > 0.0
100101
raise ArgumentError, "lock_wait_tries must be greater than 0" unless lock_wait_tries > 0
102+
101103
lock = nil
102104
using_fallback_key = false
103105
expiration_options = EMPTY_HASH
@@ -122,13 +124,14 @@ def fetch_with_fill_lock(key, fill_lock_duration, lock_wait_tries)
122124
return data
123125
else
124126
raise LockWaitTimeout if lock_wait_tries <= 0
127+
125128
lock_wait_tries -= 1
126129

127130
# If fill failed in the other client, then it might be failing fast
128131
# so avoid waiting the typical amount of time for a lock wait. The
129132
# semian gem can be used to handle failing fast when the database is slow.
130133
if lock.fill_failed?
131-
return fetch_without_fill_lock(key) { yield }
134+
return fetch_without_fill_lock(key, &block)
132135
end
133136

134137
# lock wait
@@ -167,8 +170,10 @@ def fetch_with_fill_lock(key, fill_lock_duration, lock_wait_tries)
167170
def mark_fill_failure_on_lock(key, expiration_options)
168171
@cache_backend.cas(key, expiration_options) do |value|
169172
break unless FillLock.cache_value?(value)
173+
170174
lock = FillLock.from_cache(*value)
171175
break if lock.client_id != client_id
176+
172177
lock.mark_failed
173178
lock.cache_value
174179
end
@@ -229,10 +234,12 @@ def fill_with_lock(key, data, my_lock, expiration_options)
229234
upserted = upsert(key, expiration_options) do |value|
230235
return false if value.nil? || value == IdentityCache::DELETED
231236
return true unless FillLock.cache_value?(value) # already filled
237+
232238
current_lock = FillLock.from_cache(*value)
233239
if current_lock.data_version != my_lock.data_version
234240
return false # invalidated then relocked
235241
end
242+
236243
data
237244
end
238245

@@ -285,6 +292,7 @@ def cas_multi(keys)
285292

286293
break if updates.empty?
287294
break unless IdentityCache.should_fill_cache?
295+
288296
updates
289297
end
290298
result
@@ -298,6 +306,7 @@ def add_multi(keys)
298306

299307
def add(key, value, expiration_options = EMPTY_HASH)
300308
return false unless IdentityCache.should_fill_cache?
309+
301310
@cache_backend.write(key, value, { unless_exist: true, **expiration_options })
302311
end
303312
end

lib/identity_cache/cache_hash.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
# Use CityHash for fast hashing if it is available; use Digest::MD5 otherwise
34
begin
45
require "cityhash"

lib/identity_cache/cache_invalidation.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
module IdentityCache
34
module CacheInvalidation
45
CACHE_KEY_NAMES = [:ids_variable_name, :id_variable_name, :records_variable_name]

lib/identity_cache/cache_key_generation.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
module IdentityCache
34
module CacheKeyGeneration
45
extend ActiveSupport::Concern

0 commit comments

Comments
 (0)