FEATURE: allow a central redis cache for assets

This commit is contained in:
Sam 2014-09-23 16:45:19 +10:00
parent 58eabb03e5
commit 2be03371ae
3 changed files with 24 additions and 4 deletions

View file

@ -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 = '*'

View file

@ -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"

View file

@ -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