Skip to content

Commit

Permalink
Switch to use GitHub Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Dec 17, 2021
1 parent f69eadc commit a500f4f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 40 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: build
on: push
jobs:
build:
if: "!contains(github.event.head_commit.message, '[skip ci]')"
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- ruby: 3.0
gemfile: Gemfile
- ruby: 2.7
gemfile: gemfiles/Gemfile-6-1
- ruby: 2.7
gemfile: gemfiles/Gemfile-6-0-paranoia
- ruby: 2.6
gemfile: gemfiles/Gemfile-6-0
- ruby: 2.6
gemfile: gemfiles/Gemfile-5-2
env:
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
USE_OFFICIAL_GEM_SOURCE: 1
steps:
- uses: actions/checkout@v2
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- run: bundle exec rake test
17 changes: 0 additions & 17 deletions .travis.yml

This file was deleted.

7 changes: 0 additions & 7 deletions gemfiles/Gemfile-edge

This file was deleted.

31 changes: 16 additions & 15 deletions lib/second_level_cache/active_record/fetch_by_uniq_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ def fetch_by_uniq_keys(where_values)

if obj_id
record = begin
find(obj_id)
rescue StandardError
nil
end
find(obj_id)
rescue
nil
end
end
return record if record_attributes_equal_where_values?(record, where_values)
record = where(where_values).first
Expand Down Expand Up @@ -42,20 +42,21 @@ def fetch_by_uniq_key!(value, uniq_key_name)
end

private
def cache_uniq_key(where_values)
keys = where_values.collect do |k, v|
v = Digest::MD5.hexdigest(v) if v.respond_to?(:size) && v.size >= 32
[k, v].join("_")
end

ext_key = keys.join(",")
"#{SecondLevelCache.configure.cache_key_prefix}/uniq_key_#{name}_#{ext_key}"
def cache_uniq_key(where_values)
keys = where_values.collect do |k, v|
v = Digest::MD5.hexdigest(v) if v.respond_to?(:size) && v.size >= 32
[k, v].join("_")
end

def record_attributes_equal_where_values?(record, where_values)
# https://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-type_for_attribute
where_values.all? { |k, v| record&.read_attribute(k) == type_for_attribute(k).cast(v) }
end
ext_key = keys.join(",")
"#{SecondLevelCache.configure.cache_key_prefix}/uniq_key_#{name}_#{ext_key}"
end

def record_attributes_equal_where_values?(record, where_values)
# https://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-type_for_attribute
where_values.all? { |k, v| record&.read_attribute(k) == type_for_attribute(k).cast(v) }
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ module ActiveRecord
module Associations
module Preloader
module Association
# In Rails 7, override load_query for assign self
# Override load_query method for add Association instance in arguments to LoaderQuery
# https://github.com/rails/rails/blob/7-0-stable/activerecord/lib/active_record/associations/preloader/association.rb#L148
def loader_query
::ActiveRecord::Associations::Preloader::Association::LoaderQuery.new(self, scope, association_key_name)
end

# Override load_records_for_keys for use SecondLevelCache before preload association
# https://github.com/rails/rails/blob/8f5b35b6107c28125b571b9842e248b13f804e5c/activerecord/lib/active_record/associations/preloader/association.rb#L7
module LoaderQuery
attr_reader :association

Expand Down

0 comments on commit a500f4f

Please sign in to comment.