Skip to content

Commit

Permalink
FEATURE: allow a central redis cache for assets
Browse files Browse the repository at this point in the history
  • Loading branch information
SamSaffron committed Sep 23, 2014
1 parent 58eabb0 commit 2be0337
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
3 changes: 3 additions & 0 deletions config/discourse_defaults.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '*'
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@
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"
Expand Down
23 changes: 20 additions & 3 deletions lib/tasks/assets.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2be0337

Please sign in to comment.