Class: Merb::Cache::PageStore

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

Overview

Store well suited for page caching.

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

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



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

def delete(dispatch, parameters = {})
  if writable?(dispatch, parameters)
    @stores.map {|s| s.delete(normalize(dispatch), {})}.any?
  end
end

#delete_all!Object



49
50
51
# File 'merb-cache/lib/merb-cache/stores/strategy/page_store.rb', line 49

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

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



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

def exists?(dispatch, parameters = {})
  if writable?(dispatch, parameters)
    @stores.capture_first {|s| s.exists?(normalize(dispatch), {})}
  end
end

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



31
32
33
34
35
# File 'merb-cache/lib/merb-cache/stores/strategy/page_store.rb', line 31

def fetch(dispatch, parameters = {}, conditions = {}, &blk)
  if writable?(dispatch, parameters, conditions)
    read(dispatch, parameters) || @stores.capture_first {|s| s.fetch(normalize(dispatch), data || dispatch.body, {}, conditions, &blk)}
  end
end

#normalize(dispatch) ⇒ Object



53
54
55
56
57
58
# File 'merb-cache/lib/merb-cache/stores/strategy/page_store.rb', line 53

def normalize(dispatch)
  key = dispatch.request.uri.split('?').first
  key << "index" if key =~ /\/$/
  key << ".#{dispatch.content_type}" unless key =~ /\.\w{2,6}/
  key
end

#query_string_present?(dispatch) ⇒ Boolean



60
61
62
# File 'merb-cache/lib/merb-cache/stores/strategy/page_store.rb', line 60

def query_string_present?(dispatch)
  dispatch.request.env["REQUEST_URI"] == dispatch.request.uri
end

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



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

def read(dispatch, parameters = {})
  nil
end

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



4
5
6
7
8
9
10
11
12
13
# File 'merb-cache/lib/merb-cache/stores/strategy/page_store.rb', line 4

def writable?(dispatch, parameters = {}, conditions = {})
  if Merb::Controller === dispatch && dispatch.request.method == :get &&
      !dispatch.request.uri.nil? && !dispatch.request.uri.empty? &&
      !conditions.has_key?(:if) && !conditions.has_key?(:unless) &&
      query_string_present?(dispatch)
    @stores.any? {|s| s.writable?(normalize(dispatch), parameters, conditions)}
  else
    false
  end
end

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



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

def write(dispatch, data = nil, parameters = {}, conditions = {})
  if writable?(dispatch, parameters, conditions)
    @stores.capture_first {|s| s.write(normalize(dispatch), data || dispatch.body, {}, conditions)}
  end
end

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



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

def write_all(dispatch, data = nil, parameters = {}, conditions = {})
  if writable?(dispatch, parameters, conditions)
    @stores.map {|s| s.write_all(normalize(dispatch), data || dispatch.body, {}, conditions)}.all?
  end
end