Module: RedisCacheable::Connectable::ClassMethods

Defined in:
lib/redis_cacheable/connectable.rb

Instance Method Summary collapse

Instance Method Details

#redis(&blk) ⇒ Object

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/redis_cacheable/connectable.rb', line 21

def redis(&blk)
  raise ArgumentError.new("Need block") unless blk

  unless Connectable.redis_connection
    config = RedisCacheable::Configuration.config
    Connectable.redis_connection = ConnectionPool.new(size: config.pool_size, timeout: config.timeout) {
      Redis.new(host: config.host, port: config.port, driver: config.driver.to_sym)
    }
  end

  Connectable.redis_connection.with do |conn|
    namespaced_redis = Redis::Namespace.new(redis_namespace, redis: conn)
    blk.call(namespaced_redis)
  end
end

#redis_namespaceObject



14
15
16
17
18
19
# File 'lib/redis_cacheable/connectable.rb', line 14

def redis_namespace
  config = RedisCacheable::Configuration.config
  namespace = [to_s.underscore]
  namespace.unshift config.namespace_prefix if config.namespace_prefix
  namespace.join("_")
end