Class: Racknga::Middleware::Cache
- Inherits:
-
Object
- Object
- Racknga::Middleware::Cache
- Defined in:
- lib/racknga/middleware/cache.rb
Overview
This is a middleware that provides page cache.
This stores page contents into a groonga database. A groonga database can access by multi process. It means that your Rack application processes can share the same cache. For example, Passenger runs your Rack application with multi processes.
Cache key is the request URL by default. It can be customized by env. For example, Racknga::Middleware::PerUserAgentCache and Racknga::Middleware::JSONP use it.
This only caches the following responses:
-
200 status response.
-
text/*, */json, */xml or /+xml content type response.
Usage:
use Racnkga::Middleware::Cache, :database_path => "var/cache/db"
run YourApplication
Constant Summary collapse
- KEY =
"racknga.cache.key"
- START_TIME =
"racknga.cache.start_time"
Instance Attribute Summary collapse
-
#database ⇒ Racknga::CacheDatabase
readonly
The database used by this middleware.
Instance Method Summary collapse
-
#call(environment) ⇒ Object
For Rack.
-
#close_database ⇒ Object
close the cache database.
-
#ensure_database ⇒ Object
ensures creating cache database.
-
#initialize(application, options = {}) ⇒ Cache
constructor
A new instance of Cache.
Constructor Details
#initialize(application, options = {}) ⇒ Cache
Returns a new instance of Cache.
98 99 100 101 102 103 104 |
# File 'lib/racknga/middleware/cache.rb', line 98 def initialize(application, ={}) @application = application @options = Utils.( || {}) database_path = @options[:database_path] raise ArgumentError, ":database_path is missing" if database_path.nil? @database = CacheDatabase.new(database_path) end |
Instance Attribute Details
#database ⇒ Racknga::CacheDatabase (readonly)
Returns the database used by this middleware.
94 95 96 |
# File 'lib/racknga/middleware/cache.rb', line 94 def database @database end |
Instance Method Details
#call(environment) ⇒ Object
For Rack.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/racknga/middleware/cache.rb', line 107 def call(environment) request = Rack::Request.new(environment) return @application.call(environment) unless use_cache?(request) age = @database.configuration.age key = normalize_key(environment[KEY] || request.fullpath) environment[START_TIME] = Time.now cache = @database.responses record = cache[key] if record and record.age == age handle_request_with_cache(cache, key, age, record, request) else handle_request(cache, key, age, request) end end |
#close_database ⇒ Object
close the cache database.
128 129 130 |
# File 'lib/racknga/middleware/cache.rb', line 128 def close_database @database.close_database end |
#ensure_database ⇒ Object
ensures creating cache database.
123 124 125 |
# File 'lib/racknga/middleware/cache.rb', line 123 def ensure_database @database.ensure_database end |