From a38236e554f1bc9d62544ef607afa07dcf515311 Mon Sep 17 00:00:00 2001 From: Uysim Ty Date: Fri, 25 May 2018 16:11:55 +0700 Subject: [PATCH] default redis pool --- README.md | 19 ++++++++++--------- lib/counter/cache/config.rb | 2 +- lib/counter/cache/redis.rb | 8 +++++++- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 5abfe28..046e944 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,24 @@ -# Counter::Cache +# Counter::Cache [![Gem Version](https://badge.fury.io/rb/counter-cache.png)](http://badge.fury.io/rb/ventable) [![Build Status](https://travis-ci.org/wanelo/counter-cache.svg?branch=master&fl)](https://travis-ci.org/wanelo/counter-cache) [![Code Climate](https://codeclimate.com/github/wanelo/counter-cache.png)](https://codeclimate.com/github/wanelo/counter-cache) -Counting things is hard, counting them at scale is even harder, so control when things are counted. +Counting things is hard, counting them at scale is even harder, so control when things are counted. -Any time your application performs pagination, the underlying library probably issues a `select count(*) from ...` +Any time your application performs pagination, the underlying library probably issues a `select count(*) from ...` request to the database, because all paginators need to know how many pages there are. This works on a small-to-medium -dataset, and in an application with relatively low web traffic. But at high traffic volume, live counts saturate CPU on the -database server. This is because sorting typically happens on CPU of the database server, using a small amount of heap -RAM or even worse — using temp files, which grinds the disk IO to a hault. Web requests become slower and +dataset, and in an application with relatively low web traffic. But at high traffic volume, live counts saturate CPU on the +database server. This is because sorting typically happens on CPU of the database server, using a small amount of heap +RAM or even worse — using temp files, which grinds the disk IO to a hault. Web requests become slower and slower, start to pile up in various queues, and eventually saturate all of the app servers. -There you are, the site is down. +There you are, the site is down. This gem provides a solution that works at scale, and will help you keep your site up. This library is battle-tested at Wanelo, where it has been running for several years. -### Overview +### Overview [Rails Counter Caches](http://railscasts.com/episodes/23-counter-cache-column) are a convenient way to keep counters on models that have many children. Without them, you always do live counts, which do not scale. But at high scale, Rails @@ -237,7 +237,8 @@ In an initializer such as `config/initializers/counter_cache.rb`, write the conf Counter::Cache.configure do |c| c.default_worker_adapter = MyCustomWorkAdapter c.recalculation_delay = 6.hours # Default delay for recalculations - c.redis_pool = Redis.new + c.redis_url = 'redis://localhost:6379/1' #Default redis url + c.redis_pool = Redis.new # Default redis pool c.counting_data_store = MyCustomDataStore # Default is Counter::Cache::Redis end ``` diff --git a/lib/counter/cache/config.rb b/lib/counter/cache/config.rb index 53ce0a8..90309b3 100644 --- a/lib/counter/cache/config.rb +++ b/lib/counter/cache/config.rb @@ -2,7 +2,7 @@ module Counter module Cache class Config # TODO:: Confer with paul/kig about adapting the counting data store - attr_accessor :default_worker_adapter, :recalculation_delay, :redis_pool, :counting_data_store + attr_accessor :default_worker_adapter, :recalculation_delay, :redis_pool, :redis_url, :counting_data_store def initialize self.counting_data_store = Counter::Cache::Redis.new diff --git a/lib/counter/cache/redis.rb b/lib/counter/cache/redis.rb index b173612..ca6613d 100644 --- a/lib/counter/cache/redis.rb +++ b/lib/counter/cache/redis.rb @@ -28,7 +28,13 @@ def del(key) private def with_redis - redis_pool = Counter::Cache.configuration.redis_pool + redis_pool = if Counter::Cache.configuration.redis_pool.present? + Counter::Cache.configuration.redis_pool + elsif Counter::Cache.configuration.redis_url.present? + Redis.new(url: Counter::Cache.configuration.redis_url) + else + Redis.new # redis in the same machine with app + end return yield redis_pool unless redis_pool.respond_to?(:with) redis_pool.with do |redis|