Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hooking up to Redis? #102

Closed
MarkMurphy opened this issue Nov 10, 2014 · 11 comments
Closed

Hooking up to Redis? #102

MarkMurphy opened this issue Nov 10, 2014 · 11 comments

Comments

@MarkMurphy
Copy link

Would be nice to see an example in the REDME of how this can be configured to connect to Redis. Can someone provide me with a link which explains how to do that?

@zmillman
Copy link
Contributor

By default, Rack::Attack will use whichever caching backend is configured as Rails.cache, although you can specify a different cache in your initializer:

From the example configuration:

  ### Configure Cache ###

  # If you don't want to use Rails.cache (Rack::Attack's default), then
  # configure it here.
  #
  # Note: The store is only used for throttling (not blacklisting and
  # whitelisting). It must implement .increment and .write like
  # ActiveSupport::Cache::Store

  # Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new

This looks like a pretty good gem for handling the connection/configuration: https://github.com/redis-store/redis-rails

@ktheory
Copy link
Collaborator

ktheory commented Nov 11, 2014

Yeah, if you have config.cache_store = :redis_store in your Rails app, there's no additional configuration for Rack::Attack.

If that's not clear, a PR improving the documentation is welcome. Thanks! 😎

@ktheory ktheory closed this as completed Nov 11, 2014
@MarkMurphy
Copy link
Author

@ktheory @zmillman Thanks, much appreciated. I've been using Rack::Attack for a few months now and the reason I'm now looking to change the cache store is because this gem eats up large amounts of space on my server via cache files it stores on the filesystem. The documentation really should make it more clear that when using this gem, you should be using something other than the default rails cache and why.

@fluxsaas
Copy link

fluxsaas commented Dec 8, 2014

here is an example, which helped me:

https://gist.github.com/ktheory/5087320

not sure if that is the best way to include ist.

or you could use the Rack::Attack::StoreProxy::RedisStoreProxy if you have Redis.current hooked up to a Redis server:

Redis.current = Redis.new(host: ENV['REDIS_HOST'], port: ENV['REDIS_PORT'])
class Rack::Attack
  Rack::Attack.cache.store = Rack::Attack::StoreProxy::RedisStoreProxy.new(Redis.current)
end

@Chrisgo-75
Copy link

Thank you @fluxsaas. You helped me with figuring out how to configure Rack::Attack to use Redis as its caching mechanism.

# File: rack_attack.rb
class Rack::Attack
    Rack::Attack.cache.store = ActiveSupport::Cache::RedisStore.new("redis://example.com:6666/0/cache", { expires_in: 480.minutes })
    ... ...
end

@waruboy
Copy link

waruboy commented Oct 25, 2016

@fluxsaas exactly what I need. Redis connection to RedisToGo on heroku app. Thanks!

@saygun
Copy link

saygun commented Jul 4, 2017

Those who faced same problem in Rails 5 might check

config/development.rb

config.cache_store = :null_store # comment out this one

@Skillvendor
Copy link

@saygun You have no idea how much i love you

@lordplagus02
Copy link

Is there a 2020 method for this or have the solutions remained the same?

@grzuy
Copy link
Collaborator

grzuy commented Jan 22, 2020

Hi @lordplagus02,

Since v5.4.0 (https://github.com/kickstarter/rack-attack/blob/master/CHANGELOG.md#540---2018-07-02) you can connect rack-attack directly to redis by setting the store to the Redis client:

Rack::Attack.cache.store = Redis.new(...) 

@Aryk
Copy link

Aryk commented Nov 22, 2021

Does this library automatically set expirations on the redis objects. LIke for example, if we have a "period" of 5.minutes on some throttling. After 5 minutes that key is gone right? Or is there no purging mechanism?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests