hooopo/second_level_cache

View on GitHub
lib/second_level_cache/active_record/base.rb

Summary

Maintainability
A
0 mins
Test Coverage
# frozen_string_literal: true

module SecondLevelCache
  module ActiveRecord
    module Base
      def self.prepended(base)
        base.after_commit :update_second_level_cache, on: :update
        base.after_commit :write_second_level_cache, on: :create
        base.after_commit :expire_second_level_cache, on: :destroy

        class << base
          prepend ClassMethods
        end
      end

      module ClassMethods
        def update_counters(id, counters)
          super(id, counters).tap do
            Array(id).each { |i| expire_second_level_cache(i) }
          end
        end
      end
    end
  end
end