sorentwo / readthis

:newspaper: Pooled active support compliant caching with redis
MIT License
504 stars 40 forks source link

Usage with Puma and connection_pool #65

Closed ViTVetal closed 5 years ago

ViTVetal commented 5 years ago

Hello! I want to use readthis_store on multithreading Puma. I use Redis in application and for the cache. I use connection_pool more

#initializers/redis.rb

require 'connection_pool'

REDIS = ConnectionPool.new(size: 10) { Redis.new db: 15, driver: :hiredis }

#Somewhere in the app

REDIS.with do |conn|
  conn.geoadd($DRIVER_LOCATIONS, latitude, longitude, id)
end
development.rb

config.cache_store = :readthis_store, { 
  expires_in: 1.minutes.to_i,
  namespace: 'cache',
  redis: { url: 'redis://localhost:6379/1', driver: :hiredis }
}

Should I use same connection pool for cache and if yes how can I do it?

sorentwo commented 5 years ago

Readthis already uses connection_pool to manage a pool of redis connections. You can access to the pool directly, as documented in the README:

Rails.cache.pool.with do |conn|
  conn.geoadd($DRIVER_LOCATIONS, latitude, longitude, id)
end

You can configure the pool_size and the pool_timeout directly in your cache_store setup:

config.cache_store = :readthis_store, { 
  expires_in: 1.minutes.to_i,
  namespace: 'cache',
  pool_size: 10,
  redis: { url: 'redis://localhost:6379/15', driver: :hiredis }
}
ViTVetal commented 5 years ago

@sorentwo So there is no matter that I also use Redis in application for non cache related things (geoadd etc) and Redis will use same db for cache and for other things (as you can see for pool I used db №15 and for cache №1). Right?

sorentwo commented 5 years ago

No, so long as all of the data is ephemeral there isn't any problem with doing that. I've written about this a bit in the past: https://sorentwo.com/2015/07/27/optimizing-redis-usage-for-caching.html

ViTVetal commented 5 years ago

@sorentwo Thanks! But in Rails 5+ applications Action Cable uses Redis as adapter for production:

#config/cable.yml

development:
  adapter: async

test:
  adapter: async

production:
  adapter: redis
  url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
  channel_prefix: app_production

Is there any way to provide the pool?