Class: Rack::Cache::EntityStore::Disk

Inherits:
Rack::Cache::EntityStore show all
Defined in:
lib/rack/cache/entitystore.rb

Overview

Stores entity bodies on disk at the specified path.

Defined Under Namespace

Classes: Body

Constant Summary

Constants inherited from Rack::Cache::EntityStore

DISK, FILE, HEAP, MEM, MEMCACHE, MEMCACHED

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ Disk

Returns a new instance of Disk.



83
84
85
86
# File 'lib/rack/cache/entitystore.rb', line 83

def initialize(root)
  @root = root
  FileUtils.mkdir_p root, :mode => 0755
end

Instance Attribute Details

#rootObject (readonly)

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



81
82
83
# File 'lib/rack/cache/entitystore.rb', line 81

def root
  @root
end

Instance Method Details

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/rack/cache/entitystore.rb', line 88

def exist?(key)
  File.exist?(body_path(key))
end

#open(key) ⇒ Object

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



109
110
111
112
113
# File 'lib/rack/cache/entitystore.rb', line 109

def open(key)
  Body.open(body_path(key), 'rb')
rescue Errno::ENOENT
  nil
end

#purge(key) ⇒ Object



133
134
135
136
137
138
# File 'lib/rack/cache/entitystore.rb', line 133

def purge(key)
  File.unlink body_path(key)
  nil
rescue Errno::ENOENT
  nil
end

#read(key) ⇒ Object



92
93
94
95
96
# File 'lib/rack/cache/entitystore.rb', line 92

def read(key)
  File.read(body_path(key))
rescue Errno::ENOENT
  nil
end

#write(body) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rack/cache/entitystore.rb', line 115

def write(body)
  filename = ['buf', $$, Thread.current.object_id].join('-')
  temp_file = storage_path(filename)
  key, size =
    File.open(temp_file, 'wb') { |dest|
      slurp(body) { |part| dest.write(part) }
    }

  path = body_path(key)
  if File.exist?(path)
    File.unlink temp_file
  else
    FileUtils.mkdir_p File.dirname(path), :mode => 0755
    FileUtils.mv temp_file, path
  end
  [key, size]
end