lib/legion/cache/redis.rb in legion-cache-1.0.0 vs lib/legion/cache/redis.rb in legion-cache-1.1.0

- old
+ new

@@ -1,50 +1,43 @@ require 'redis' +require 'legion/cache/pool' module Legion module Cache module Redis + include Legion::Cache::Pool extend self - def client + def client(pool_size: 20, timeout: 5, **) return @client unless @client.nil? - @client = ::Redis.new + @pool_size = pool_size + @timeout = timeout + + @client = ConnectionPool.new(size: pool_size, timeout: timeout) do + ::Redis.new + end @connected = true @client end - def connected? - @connected ||= false - end - - def close - client.close - end - - def restart - close - @client = nil - client - end - def get(key) - client.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.set(key, value, **args) == 'OK' + client.with { |conn| conn.set(key, value, **args) == 'OK' } end def delete(key) - client.del(key) == 1 + client.with { |conn| conn.del(key) == 1 } end def flush - client.flushdb == 'OK' + client.with { |conn| conn.flushdb == 'OK' } end end end end