class Rack::Cache::EntityStore::Redis

Constants

MINIMUM_COMPRESSION_BYTESIZE

Public Instance Methods

exist?(key) click to toggle source
# File lib/rack/cache/redis_entitystore.rb, line 15
def exist?(key)
  cache.exists key
end
purge(key) click to toggle source
# File lib/rack/cache/redis_entitystore.rb, line 38
def purge(key)
  cache.del key
  nil
end
read(key) click to toggle source
# File lib/rack/cache/redis_entitystore.rb, line 19
def read(key)
  raw = cache.get(key)

  return if raw.nil?

  decompress(raw).force_encoding('utf-8')
end
write(body, ttl=0) click to toggle source
# File lib/rack/cache/redis_entitystore.rb, line 27
def write(body, ttl=0)
  buf = StringIO.new
  key, size = slurp(body) {|part| buf.write(part) }
  ttl = ttl.to_i.zero? ? default_ttl : ttl
  value = compress(buf.string)

  return unless cache.setex(key, ttl, value)

  [key, size]
end

Protected Instance Methods

compress(data) click to toggle source
# File lib/rack/cache/redis_entitystore.rb, line 45
def compress(data)
  return data unless compress? data

  deflater.deflate(data)
end
decompress(data) click to toggle source
# File lib/rack/cache/redis_entitystore.rb, line 51
def decompress(data)
  return data unless compress?

  inflater.inflate(data) rescue data
end

Private Instance Methods

compress?(data = nil) click to toggle source
# File lib/rack/cache/redis_entitystore.rb, line 85
def compress?(data = nil)
  return options[:compress] if data.nil?

  options[:compress] && data.bytesize >= MINIMUM_COMPRESSION_BYTESIZE
end
deflater() click to toggle source
# File lib/rack/cache/redis_entitystore.rb, line 59
def deflater
  case options[:compress]
  when :deflate
    Zlib::Deflate
  when :gzip, true
    Zlib::GzipCompression
  when false
    nil
  else
    options[:compress]
  end
end
inflater() click to toggle source
# File lib/rack/cache/redis_entitystore.rb, line 72
def inflater
  case options[:compress]
  when :deflate
    Zlib::Inflate
  when :gzip, true
    Zlib::GzipCompression
  when false
    nil
  else
    options[:compress]
  end
end