Sha256: d849c0e1474663d41b5281279a48ce251c09b27b86d25c3f048253b10fb49351

Contents?: true

Size: 1.33 KB

Versions: 17

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

require 'redis'

module Marty
  module CacheAdapters
    class Redis < ::Delorean::Cache::Adapters::Base
      POST = '__RedisCache'

      def initialize(
        size_per_class: 1000,
        redis_url: Rails.application.config.marty.redis_url,
        expires_in: 48.hours
      )
        @redis = ::Redis.new(url: "redis://#{redis_url}")
        @expires_in = expires_in
      end

      def cache_item(klass:, cache_key:, item:)
        @redis.set(
          cache_key,
          Marshal.dump(item),
          ex: @expires_in.seconds.to_i
        )
      end

      def fetch_item(klass:, cache_key:, default: nil)
        r = @redis.get(cache_key)

        return default if r.nil?

        Marshal.load(r)
      end

      def cache_key(klass:, method_name:, args:)
        r = ["#{klass.name}#{POST}", method_name] + args.map do |arg|
          next arg.id if arg.respond_to?(:id)

          arg
        end.freeze

        Marshal.dump r
      end

      def clear!(klass:)
        keys = @redis.keys("*#{klass.name}#{POST}*")
        @redis.pipelined do
          keys.each do |key|
            @redis.del key
          end
        end
      end

      def clear_all!
        @redis.flushall
      end

      def cache_item?(klass:, method_name:, args:)
        !Mcfly.is_infinity(args&.first)
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
marty-14.3.0 lib/marty/cache_adapters/redis.rb
marty-14.0.0 lib/marty/cache_adapters/redis.rb
marty-13.0.2 lib/marty/cache_adapters/redis.rb
marty-11.0.0 lib/marty/cache_adapters/redis.rb
marty-10.0.3 lib/marty/cache_adapters/redis.rb
marty-10.0.2 lib/marty/cache_adapters/redis.rb
marty-10.0.0 lib/marty/cache_adapters/redis.rb
marty-9.5.1 lib/marty/cache_adapters/redis.rb
marty-9.5.0 lib/marty/cache_adapters/redis.rb
marty-9.3.3 lib/marty/cache_adapters/redis.rb
marty-9.3.2 lib/marty/cache_adapters/redis.rb
marty-9.3.0 lib/marty/cache_adapters/redis.rb
marty-8.5.0 lib/marty/cache_adapters/redis.rb
marty-8.4.1 lib/marty/cache_adapters/redis.rb
marty-8.3.1 lib/marty/cache_adapters/redis.rb
marty-8.2.0 lib/marty/cache_adapters/redis.rb
marty-8.0.0 lib/marty/cache_adapters/redis.rb