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.

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.



77
78
79
80
# File 'lib/rack/cache/entitystore.rb', line 77

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.



75
76
77
# File 'lib/rack/cache/entitystore.rb', line 75

def root
  @root
end

Instance Method Details

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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.



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rack/cache/entitystore.rb', line 94

def open(key)
  io = File.open(body_path(key), 'rb')
  def io.each
    while part = read(8192)
      yield part
    end
  end
  io
rescue Errno::ENOENT
  nil
end

#read(key) ⇒ Object



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

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

#write(body) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rack/cache/entitystore.rb', line 106

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