radiant / radiant

Radiant is a no-fluff, open source content management system designed for small teams.

This URL has Read+Write access

seancribbs (author)
Mon May 11 18:52:34 -0700 2009
commit  b775ebe1f6c469fcb0fbb03b33afe1ee44bdb48a
tree    701f0f930f7056c915b972c52e2a2050f0915707
parent  28a1595e01fac0bdd43008b168f1654cab88d45f
radiant / lib / radiant / cache.rb
6c88ddbe » seancribbs 2009-05-05 Introducing Radiant::Cache.... 1 require 'rack/cache'
2 # So we can subclass the storage types
3 require 'rack/cache/storage'
4 require 'rack/cache/metastore'
5 require 'rack/cache/entitystore'
6
7 module Radiant
8 module Cache
9 mattr_accessor :meta_stores, :entity_stores, :use_x_sendfile, :use_x_accel_redirect
10 self.meta_stores ||= []
11 self.entity_stores ||= []
12 self.use_x_sendfile = false
13 self.use_x_accel_redirect = nil
14
15 def self.new(app, options={})
16 self.use_x_sendfile = options.delete(:use_x_sendfile) if options[:use_x_sendfile]
17 self.use_x_accel_redirect = options.delete(:use_x_accel_redirect) if options[:use_x_accel_redirect]
28a1595e » seancribbs 2009-05-11 Turn off verbose cache logg... 18 Rack::Cache.new(app, {
19 :entitystore => "radiant:cache/entity",
20 :metastore => "radiant:cache/meta",
21 :verbose => false}.merge(options))
6c88ddbe » seancribbs 2009-05-05 Introducing Radiant::Cache.... 22 end
23
24 def self.clear
25 meta_stores.each {|ms| ms.clear }
26 entity_stores.each {|es| es.clear }
27 end
28
29 class EntityStore < Rack::Cache::EntityStore::Disk
30 def initialize(root="#{Rails.root}/cache/entity")
31 super
32 Radiant::Cache.entity_stores << self
33 end
34
35 def clear
36 Dir[File.join(self.root, "*")].each {|file| FileUtils.rm_rf(file) }
37 end
38 end
39
40 class MetaStore < Rack::Cache::MetaStore::Disk
41 def initialize(root="#{Rails.root}/cache/meta")
42 super
43 Radiant::Cache.meta_stores << self
44 end
45
46 def clear
47 Dir[File.join(self.root, "*")].each {|file| FileUtils.rm_rf(file) }
48 end
49
50 private
51 def restore_response(hash, body)
52 # Cribbed from the Rack::Cache source
53 status = hash.delete('X-Status').to_i
54 response = Rack::Cache::Response.new(status, hash, body)
55
56 # Add acceleration headers
57 if Radiant::Cache.use_x_sendfile
58 accelerate(response, 'X-Sendfile', File.expand_path(body.path))
59 elsif Radiant::Cache.use_x_accel_redirect
60 virtual_path = File.expand_path(body.path)
61 entity_path = File.expand_path(Radiant::Cache.entity_stores.first.root)
62 virtual_path[entity_path] = Radiant::Cache.use_x_accel_redirect
63 accelerate(response,'X-Accel-Redirect', virtual_path)
64 end
65 response
66 end
67
68 def accelerate(response, header, value)
69 response.headers[header] = value
70 response.body = []
71 response.headers['Content-Length'] = '0'
72 end
73 end
74 end
75 end
76
77 # Add our classes as fake constants in the right place
78 class Rack::Cache::EntityStore
79 RADIANT = Radiant::Cache::EntityStore
80 end
81
82 class Rack::Cache::MetaStore
83 RADIANT = Radiant::Cache::MetaStore
84 end