Class: Merb::Cache::GzipStore

Inherits:
AbstractStrategyStore show all
Defined in:
merb-cache/lib/merb-cache/stores/strategy/gzip_store.rb

Overview

Store that compresses cached data using GZip. Usually wraps other stores and good for caching of large pages.

Pinched from Tobias Luetke's "cacheable" rails plugin.

Instance Attribute Summary

Attributes inherited from AbstractStrategyStore

#stores

Instance Method Summary (collapse)

Methods inherited from AbstractStrategyStore

#clone, contextualize, #initialize

Methods inherited from AbstractStore

#delete_all, #initialize

Constructor Details

This class inherits a constructor from Merb::Cache::AbstractStrategyStore

Instance Method Details

- (Object) compress(data)



47
48
49
50
51
52
53
54
55
# File 'merb-cache/lib/merb-cache/stores/strategy/gzip_store.rb', line 47

def compress(data)
  return if data.nil?

  output = StringIO.new
  gz = Zlib::GzipWriter.new(output)
  gz.write(Marshal.dump(data))
  gz.close
  output.string
end

- (Object) decompress(data)



57
58
59
60
61
# File 'merb-cache/lib/merb-cache/stores/strategy/gzip_store.rb', line 57

def decompress(data)
  return if data.nil?

  Marshal.load(Zlib::GzipReader.new(StringIO.new(data)).read)
end

- (Object) delete(key, parameters = {})



39
40
41
# File 'merb-cache/lib/merb-cache/stores/strategy/gzip_store.rb', line 39

def delete(key, parameters = {})
  @stores.map {|c| c.delete(key, parameters)}.any?
end

- (Object) delete_all!



43
44
45
# File 'merb-cache/lib/merb-cache/stores/strategy/gzip_store.rb', line 43

def delete_all!
  @stores.map {|c| c.delete_all! }.all?
end

- (Boolean) exists?(key, parameters = {})



35
36
37
# File 'merb-cache/lib/merb-cache/stores/strategy/gzip_store.rb', line 35

def exists?(key, parameters = {})
  @stores.capture_first {|c| c.exists?(key, parameters)}
end

- (Object) fetch(key, parameters = {}, conditions = {}, &blk)



30
31
32
33
# File 'merb-cache/lib/merb-cache/stores/strategy/gzip_store.rb', line 30

def fetch(key, parameters = {}, conditions = {}, &blk)
  wrapper_blk = lambda { compress(blk.call) }
  read(key, parameters) || decompress(@stores.capture_first {|s| s.fetch(key, parameters, conditions, &wrapper_blk)})
end

- (Object) read(key, parameters = {})



14
15
16
# File 'merb-cache/lib/merb-cache/stores/strategy/gzip_store.rb', line 14

def read(key, parameters = {})
  decompress(@stores.capture_first {|c| c.read(key, parameters)})
end

- (Boolean) writable?(key, parameters = {}, conditions = {})



10
11
12
# File 'merb-cache/lib/merb-cache/stores/strategy/gzip_store.rb', line 10

def writable?(key, parameters = {}, conditions = {})
  @stores.any? {|c| c.writable?(key, parameters, conditions)}
end

- (Object) write(key, data = nil, parameters = {}, conditions = {})



18
19
20
21
22
# File 'merb-cache/lib/merb-cache/stores/strategy/gzip_store.rb', line 18

def write(key, data = nil, parameters = {}, conditions = {})
  if writable?(key, parameters, conditions)
    @stores.capture_first {|c| c.write(key, compress(data), parameters, conditions)}
  end
end

- (Object) write_all(key, data = nil, parameters = {}, conditions = {})



24
25
26
27
28
# File 'merb-cache/lib/merb-cache/stores/strategy/gzip_store.rb', line 24

def write_all(key, data = nil, parameters = {}, conditions = {})
  if writable?(key, parameters, conditions)
    @stores.map {|c| c.write_all(key, compress(data), parameters, conditions)}.all?
  end
end