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

Inherits:
Rack::Cache::EntityStore show all
Defined in:
lib/rack/cache/entity_store.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, GAE, GAECACHE, HEAP, MEM, MEMCACHE, MEMCACHED, NOOP

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ Disk

Returns a new instance of Disk.



90
91
92
93
# File 'lib/rack/cache/entity_store.rb', line 90

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.



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

def root
  @root
end

Instance Method Details

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/rack/cache/entity_store.rb', line 95

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.



116
117
118
119
120
# File 'lib/rack/cache/entity_store.rb', line 116

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

#purge(key) ⇒ Object



140
141
142
143
144
145
# File 'lib/rack/cache/entity_store.rb', line 140

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

#read(key) ⇒ Object



99
100
101
102
103
# File 'lib/rack/cache/entity_store.rb', line 99

def read(key)
  File.open(body_path(key), 'rb') { |f| f.read }
rescue Errno::ENOENT
  nil
end

#write(body, ttl = nil) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/rack/cache/entity_store.rb', line 122

def write(body, ttl=nil)
  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