Class: Rack::Cache::EntityStore::Heap

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

Overview

Stores entity bodies on the heap using a Hash object.

Constant Summary

Constants inherited from Rack::Cache::EntityStore

DISK, FILE, GAE, GAECACHE, HEAP, MEM, MEMCACHE, MEMCACHED, NOOP

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}, options = {}) ⇒ Heap

Create the store with the specified backing Hash.



37
38
39
40
# File 'lib/rack/cache/entity_store.rb', line 37

def initialize(hash={}, options = {})
  @hash = hash
  @options = options
end

Class Method Details

.resolve(uri, options = {}) ⇒ Object



74
75
76
# File 'lib/rack/cache/entity_store.rb', line 74

def self.resolve(uri, options = {})
  new({}, options)
end

Instance Method Details

#exist?(key) ⇒ Boolean

Determine whether the response body with the specified key (SHA1) exists in the store.

Returns:

  • (Boolean)


44
45
46
# File 'lib/rack/cache/entity_store.rb', line 44

def exist?(key)
  @hash.include?(key)
end

#open(key) ⇒ Object

Return an object suitable for use as a Rack response body for the specified key.



50
51
52
# File 'lib/rack/cache/entity_store.rb', line 50

def open(key)
  (body = @hash[key]) && body.dup
end

#purge(key) ⇒ Object

Remove the body corresponding to key; return nil.



69
70
71
72
# File 'lib/rack/cache/entity_store.rb', line 69

def purge(key)
  @hash.delete(key)
  nil
end

#read(key) ⇒ Object

Read all data associated with the given key and return as a single String.



56
57
58
# File 'lib/rack/cache/entity_store.rb', line 56

def read(key)
  (body = @hash[key]) && body.join
end

#write(body, ttl = nil) ⇒ Object

Write the Rack response body immediately and return the SHA1 key.



61
62
63
64
65
66
# File 'lib/rack/cache/entity_store.rb', line 61

def write(body, ttl=nil)
  buf = []
  key, size = slurp(body) { |part| buf << part }
  @hash[key] = buf
  [key, size]
end