Sha256: 03742e929b31207ecce0612a5685951d020064c1209288b57989ee406c4bade3

Contents?: true

Size: 922 Bytes

Versions: 2

Compression:

Stored size: 922 Bytes

Contents

require_relative "base"

module ActiveCachedResource
  module CachingStrategies
    class SQLCache < Base
      def initialize(model, options = {})
        super()
        @model = model
        @batch_clear_size = options.fetch(:batch_clear_size, 1000)
      end

      protected

      def delete_raw(key)
        @model.where(key: key).delete_all
      end

      def read_raw(key)
        record = @model.where(key: key).where(@model.arel_table[:expires_at].gt(Time.current)).first
        record&.value
      end

      def write_raw(key, value, options = {})
        expires_at = Time.current + options.fetch(:expires_in)

        @model.create({key: key, value: value, expires_at: expires_at})
      end

      def clear_raw(pattern)
        @model.where(@model.arel_table[:key].matches("#{pattern}%")).in_batches(of: @batch_clear_size) do |batch|
          batch.delete_all
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_cached_resource-0.1.8 lib/active_cached_resource/caching_strategies/sql_cache.rb
active_cached_resource-0.1.7 lib/active_cached_resource/caching_strategies/sql_cache.rb