From 2be03371ae069e29c8f008c981fa1316769fad50 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 23 Sep 2014 16:45:19 +1000 Subject: [PATCH] FEATURE: allow a central redis cache for assets --- config/discourse_defaults.conf | 3 +++ config/routes.rb | 2 +- lib/tasks/assets.rake | 23 ++++++++++++++++++++--- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/config/discourse_defaults.conf b/config/discourse_defaults.conf index 9ec995218..f88609348 100644 --- a/config/discourse_defaults.conf +++ b/config/discourse_defaults.conf @@ -88,6 +88,9 @@ redis_db = 0 # redis password redis_password = +# eg: redis://:password@host:port +asset_redis_url = + # enable Cross-origin Resource Sharing (CORS) directly at the application level enable_cors = false cors_origin = '*' diff --git a/config/routes.rb b/config/routes.rb index 81e2aa88b..5d585ac09 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -245,8 +245,8 @@ Discourse::Application.routes.draw do get "user_avatar/:hostname/:username/:size/:version.png" => "user_avatars#show", format: false, constraints: {hostname: /[\w\.-]+/} - get "uploads/:site/:id/:sha.:extension" => "uploads#show", constraints: {site: /\w+/, id: /\d+/, sha: /[a-z0-9]{15,16}/i, extension: /\w{2,}/} + get "uploads/:site/:sha" => "uploads#show", constraints: { site: /\w+/, sha: /[a-z0-9]{40}/} post "uploads" => "uploads#create" get "posts/by_number/:topic_id/:post_number" => "posts#by_number" diff --git a/lib/tasks/assets.rake b/lib/tasks/assets.rake index 184144c83..78f40f820 100644 --- a/lib/tasks/assets.rake +++ b/lib/tasks/assets.rake @@ -21,14 +21,31 @@ task 'assets:precompile:before' do module ::Sprockets + def self.redis + @redis ||= + ( + redis_url = GlobalSetting.asset_redis_url + if redis_url.present? + uri = URI.parse(redis_url) + options = {} + options[:password] = uri.password if uri.password.present? + options[:host] = uri.host + options[:port] = uri.port || 6379 + Redis.new(options) + else + DiscourseRedis.raw_connection + end + ) + end + def self.cache_compiled(type, data) digest = Digest::SHA1.hexdigest(data) key = "SPROCKETS_#{type}_#{digest}" - if compiled = $redis.get(key) - $redis.expire(key, 1.week) + if compiled = redis.get(key) + redis.expire(key, 1.week) else compiled = yield - $redis.setex(key, 1.week, compiled) + redis.setex(key, 1.week, compiled) end compiled end