Class: Merb::Cache::GzipStore

Inherits:
AbstractStrategyStore show all
Defined in:
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.

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

#compress(data) ⇒ Object



47
48
49
50
51
52
53
54
55
# File '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

#decompress(data) ⇒ Object



57
58
59
60
61
# File '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

#delete(key, parameters = {}) ⇒ Object



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

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

#delete_all!Object



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

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

#exists?(key, parameters = {}) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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



30
31
32
33
# File '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

#read(key, parameters = {}) ⇒ Object



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

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

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

Returns:

  • (Boolean)


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

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

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



18
19
20
21
22
# File '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

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



24
25
26
27
28
# File '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