Commit 10354f7a authored by Robert Speicher's avatar Robert Speicher Committed by Rémy Coutable

Merge branch 'cache-clear' into 'master'

Use SCAN during 'rake cache:clear'

This allows 'rake cache:clear' to delete millions of keys without
choking. It requires Redis 2.8.0 or newer but we needed that already
anyway.

See merge request !2872
parent d3e79985
...@@ -6,6 +6,8 @@ I18n.config.enforce_available_locales = false ...@@ -6,6 +6,8 @@ I18n.config.enforce_available_locales = false
Bundler.require(:default, Rails.env) Bundler.require(:default, Rails.env)
module Gitlab module Gitlab
REDIS_CACHE_NAMESPACE = 'cache:gitlab'
class Application < Rails::Application class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here. # Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers # Application configuration should go into files in config/initializers
...@@ -89,7 +91,7 @@ module Gitlab ...@@ -89,7 +91,7 @@ module Gitlab
redis_config_hash[:path] = redis_uri.path redis_config_hash[:path] = redis_uri.path
end end
redis_config_hash[:namespace] = 'cache:gitlab' redis_config_hash[:namespace] = REDIS_CACHE_NAMESPACE
redis_config_hash[:expires_in] = 2.weeks # Cache should not grow forever redis_config_hash[:expires_in] = 2.weeks # Cache should not grow forever
config.cache_store = :redis_store, redis_config_hash config.cache_store = :redis_store, redis_config_hash
......
namespace :cache do namespace :cache do
CLEAR_BATCH_SIZE = 1000
REDIS_SCAN_START_STOP = '0' # Magic value, see http://redis.io/commands/scan
desc "GitLab | Clear redis cache" desc "GitLab | Clear redis cache"
task :clear => :environment do task :clear => :environment do
# Hack into Rails.cache until https://github.com/redis-store/redis-store/pull/225
# is accepted (I hope) and we can update the redis-store gem.
redis_store = Rails.cache.instance_variable_get(:@data) redis_store = Rails.cache.instance_variable_get(:@data)
redis_store.keys.each_slice(1000) do |key_slice| cursor = [REDIS_SCAN_START_STOP, []]
redis_store.del(*key_slice) loop do
cursor = redis_store.scan(
cursor.first,
match: "#{Gitlab::REDIS_CACHE_NAMESPACE}*",
count: CLEAR_BATCH_SIZE
)
keys = cursor.last
redis_store.del(*keys) if keys.any?
break if cursor.first == REDIS_SCAN_START_STOP
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment