Class Rack::Cache::EntityStore::Disk
In: lib/rack/cache/entitystore.rb
Parent: EntityStore

Stores entity bodies on disk at the specified path.

Methods

body_path   exist?   new   open   purge   read   resolve   spread   storage_path   write  

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

[Source]

    # File lib/rack/cache/entitystore.rb, line 90
90:       def initialize(root)
91:         @root = root
92:         FileUtils.mkdir_p root, :mode => 0755
93:       end

Protected Class methods

[Source]

     # File lib/rack/cache/entitystore.rb, line 162
162:       def self.resolve(uri)
163:         path = File.expand_path(uri.opaque || uri.path)
164:         new path
165:       end

Public Instance methods

[Source]

    # File lib/rack/cache/entitystore.rb, line 95
95:       def exist?(key)
96:         File.exist?(body_path(key))
97:       end

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

[Source]

     # File lib/rack/cache/entitystore.rb, line 116
116:       def open(key)
117:         Body.open(body_path(key), 'rb')
118:       rescue Errno::ENOENT
119:         nil
120:       end

[Source]

     # File lib/rack/cache/entitystore.rb, line 140
140:       def purge(key)
141:         File.unlink body_path(key)
142:         nil
143:       rescue Errno::ENOENT
144:         nil
145:       end

[Source]

     # File lib/rack/cache/entitystore.rb, line 99
 99:       def read(key)
100:         File.open(body_path(key), 'rb') { |f| f.read }
101:       rescue Errno::ENOENT
102:         nil
103:       end

[Source]

     # File lib/rack/cache/entitystore.rb, line 122
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

[Source]

     # File lib/rack/cache/entitystore.rb, line 158
158:       def body_path(key)
159:         storage_path spread(key)
160:       end

[Source]

     # File lib/rack/cache/entitystore.rb, line 152
152:       def spread(key)
153:         key = key.dup
154:         key[2,0] = '/'
155:         key
156:       end

[Source]

     # File lib/rack/cache/entitystore.rb, line 148
148:       def storage_path(stem)
149:         File.join root, stem
150:       end

[Validate]