class HTTPDisk::CacheKey
Attributes
env[R]
ignore_params[R]
Public Class Methods
new(env, ignore_params: [])
click to toggle source
# File lib/httpdisk/cache_key.rb, line 9 def initialize(env, ignore_params: []) @env, @ignore_params = env, ignore_params # sanity checks raise InvalidUrl, "http/https required #{env.url.inspect}" if env.url.scheme !~ /^https?$/ raise InvalidUrl, "hostname required #{env.url.inspect}" if !env.url.host end
Public Instance Methods
digest()
click to toggle source
md5(key) (memoized)
# File lib/httpdisk/cache_key.rb, line 27 def digest @digest ||= Digest::MD5.hexdigest(key) end
diskpath()
click to toggle source
Relative path for this cache key based on hostdir & digest.
# File lib/httpdisk/cache_key.rb, line 32 def diskpath @diskpath ||= File.join(hostdir, digest[0, 3], digest[3..]) end
key()
click to toggle source
Cache
key (memoized)
# File lib/httpdisk/cache_key.rb, line 22 def key @key ||= calculate_key end
to_s()
click to toggle source
# File lib/httpdisk/cache_key.rb, line 36 def to_s key end
url()
click to toggle source
# File lib/httpdisk/cache_key.rb, line 17 def url env.url end
Protected Instance Methods
bodykey()
click to toggle source
Calculate cache key segment for body
# File lib/httpdisk/cache_key.rb, line 69 def bodykey body = env.request_body.to_s if env.request_headers['Content-Type'] == 'application/x-www-form-urlencoded' querykey(body) elsif body.length < 50 body else Digest::MD5.hexdigest(body) end end
calculate_key()
click to toggle source
Calculate cache key for url
# File lib/httpdisk/cache_key.rb, line 43 def calculate_key key = [] key << env.method.upcase key << ' ' key << url.scheme key << '://' key << url.host.downcase if !default_port? key << ':' key << url.port end if url.path != '/' key << url.path end if (q = url.query) && q != '' key << '?' key << querykey(q) end if env.request_body key << ' ' key << bodykey end key.join end
default_port?()
click to toggle source
# File lib/httpdisk/cache_key.rb, line 94 def default_port? url.default_port == url.port end
hostdir()
click to toggle source
Calculate nice directory name from url.host
# File lib/httpdisk/cache_key.rb, line 99 def hostdir hostdir = url.host.downcase hostdir = hostdir.gsub(/^www\./, '') hostdir = hostdir.gsub(/[^a-z0-9._-]/, '') hostdir = hostdir.squeeze('.') hostdir = 'any' if hostdir.empty? hostdir end
querykey(q)
click to toggle source
Calculate canonical key for a query
# File lib/httpdisk/cache_key.rb, line 81 def querykey(q) parts = q.split('&').sort if !ignore_params.empty? parts = parts.map do |part| key, value = part.split('=', 2) next if ignore_params.include?(key) "#{key}=#{value}" end.compact end parts.join('&') end