Sha256: 69633dfefa9b45af037fe177139598bf36f19d4005c8bfa830e3d21c7465e140

Contents?: true

Size: 843 Bytes

Versions: 8

Compression:

Stored size: 843 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 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

8 entries across 8 versions & 1 rubygems

Version Path
active_cached_resource-0.1.6 lib/active_cached_resource/caching_strategies/sql_cache.rb
active_cached_resource-0.1.5 lib/active_cached_resource/caching_strategies/sql_cache.rb
active_cached_resource-0.1.4 lib/active_cached_resource/caching_strategies/sql_cache.rb
active_cached_resource-0.1.3 lib/active_cached_resource/caching_strategies/sql_cache.rb
active_cached_resource-0.1.2 lib/active_cached_resource/caching_strategies/sql_cache.rb
active_cached_resource-0.1.1 lib/active_cached_resource/caching_strategies/sql_cache.rb
active_cached_resource-0.1.0 lib/active_cached_resource/caching_strategies/sql_cache.rb
active_cached_resource-0.0.1.pre lib/active_cached_resource/caching_strategies/sql_cache.rb