Sha256: 952f385e68dcb4bfdf062ed0d39c9c20f0c09b953400cafe1f2ce98a5da2c702

Contents?: true

Size: 906 Bytes

Versions: 3

Compression:

Stored size: 906 Bytes

Contents

require 'redis'
require 'legion/cache/pool'

module Legion
  module Cache
    module Redis
      include Legion::Cache::Pool
      extend self

      def client(pool_size: 20, timeout: 5, **)
        return @client unless @client.nil?

        @pool_size = pool_size
        @timeout = timeout

        @client = ConnectionPool.new(size: pool_size, timeout: timeout) do
          ::Redis.new
        end
        @connected = true
        @client
      end

      def get(key)
        client.with { |conn| conn.get(key) }
      end
      alias fetch get

      def set(key, value, ttl: nil)
        args = {}
        args[:ex] = ttl unless ttl.nil?
        client.with { |conn| conn.set(key, value, **args) == 'OK' }
      end

      def delete(key)
        client.with { |conn| conn.del(key) == 1 }
      end

      def flush
        client.with { |conn| conn.flushdb == 'OK' }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
legion-cache-1.2.0 lib/legion/cache/redis.rb
legion-cache-1.1.1 lib/legion/cache/redis.rb
legion-cache-1.1.0 lib/legion/cache/redis.rb