class Rack::Cache::EntityStore::Disk

Stores entity bodies on disk at the specified path.

Attributes

root[R]

Path where entities should be stored. This directory is created the first time the store is instansiated if it does not already exist.

Public Class Methods

new(root) click to toggle source
   # File lib/rack/cache/entity_store.rb
90 def initialize(root)
91   @root = root
92   FileUtils.mkdir_p root, :mode => 0755
93 end

Protected Class Methods

resolve(uri) click to toggle source
    # File lib/rack/cache/entity_store.rb
162 def self.resolve(uri)
163   path = File.expand_path(uri.opaque || uri.path)
164   new path
165 end

Public Instance Methods

exist?(key) click to toggle source
   # File lib/rack/cache/entity_store.rb
95 def exist?(key)
96   File.exist?(body_path(key))
97 end
open(key) click to toggle source

Open the entity body and return an IO object. The IO object's each method is overridden to read 8K chunks instead of lines.

    # File lib/rack/cache/entity_store.rb
116 def open(key)
117   Body.open(body_path(key), 'rb')
118 rescue Errno::ENOENT
119   nil
120 end
purge(key) click to toggle source
    # File lib/rack/cache/entity_store.rb
140 def purge(key)
141   File.unlink body_path(key)
142   nil
143 rescue Errno::ENOENT
144   nil
145 end
read(key) click to toggle source
    # File lib/rack/cache/entity_store.rb
 99 def read(key)
100   File.open(body_path(key), 'rb') { |f| f.read }
101 rescue Errno::ENOENT
102   nil
103 end
write(body, ttl=nil) click to toggle source
    # File lib/rack/cache/entity_store.rb
122 def write(body, ttl=nil)
123   filename = ['buf', $$, Thread.current.object_id].join('-')
124   temp_file = storage_path(filename)
125   key, size =
126     File.open(temp_file, 'wb') { |dest|
127       slurp(body) { |part| dest.write(part) }
128     }
129 
130   path = body_path(key)
131   if File.exist?(path)
132     File.unlink temp_file
133   else
134     FileUtils.mkdir_p File.dirname(path), :mode => 0755
135     FileUtils.mv temp_file, path
136   end
137   [key, size]
138 end

Protected Instance Methods

body_path(key) click to toggle source
    # File lib/rack/cache/entity_store.rb
158 def body_path(key)
159   storage_path spread(key)
160 end
spread(key) click to toggle source
    # File lib/rack/cache/entity_store.rb
152 def spread(key)
153   key = key.dup
154   key[2,0] = '/'
155   key
156 end
storage_path(stem) click to toggle source
    # File lib/rack/cache/entity_store.rb
148 def storage_path(stem)
149   File.join root, stem
150 end