Class: CachedPage

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/cached_page.rb

Overview

Cache html pages

This class is responsible for caching/expiring pages. It is called each time a web page is rendered and the visitor is anonymous.

Cache

When a CachedPage page is created, it stores the content_data in the current site’s static cache location and remembers the context used to create this page in order to expire it later. If passed content_path it creates a symlink instead of a new file (used to cache documents, images).

Context

When the anonymous visitor visits a web page, it ‘opens’ many nodes in order to produce the final html. The nodes opened depend on the zafu template used. The list of opened nodes is stored as the cached page’s context.

Expire

Whenever a node changes, all the cached pages which use this node as context are destroyed with their static files.

Example

Anonymous visits the node ‘projects’. When rendering the html for this page, the following nodes are opened:

projects ---> 11
parent   ---> 1
notes    ---> 12, 13, 39, 23, 82, 15
hot      ---> 43, 23

The visited_nodes list is [11,1,12,13,39,23,82,15,43,23].

Later, joe edits one of the hot topics (id=43). As 43 is in the context for the ‘projects’ page, the latter is expired.

Cache rendered ‘erb’

This class is also used for caching/expiring of rendered zafu templates (erb code).

Cached zafu

When a zafu template is rendered to erb code, the ids of the sub-templates used for this rendering (through ‘include’ tags) are stored in the join table ‘cached_pages_nodes’. Whenever any of these sub-templates (including the master template) is updated, the rendered zafu is removed. This behavior is very close to CachedPage (caching of resulting html sent to clients).

Expire

Whenever a sub-template changes, all the rendered zafu templates in which this template was included are destroyed.

Example

Rendering ‘Node_index.html’ to erb includes the following sub-templates :

Project.html ---> layout.html
             ---> notes.html

The visited_nodes list is [Node_index.html, Project.html, layout.html, notes.html].

Whenever any of the nodes listed above changes, ‘Node_index.html’ rendered folder is destroyed.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#content_dataObject

Returns the value of attribute content_data.



57
58
59
# File 'app/models/cached_page.rb', line 57

def content_data
  @content_data
end

#content_pathObject

Returns the value of attribute content_path.



57
58
59
# File 'app/models/cached_page.rb', line 57

def content_path
  @content_path
end

#expire_with_idsObject

Returns the value of attribute expire_with_ids.



57
58
59
# File 'app/models/cached_page.rb', line 57

def expire_with_ids
  @expire_with_ids
end

Class Method Details

.expire_oldObject

Expire all pages whose expire date is in the past



66
67
68
# File 'app/models/cached_page.rb', line 66

def expire_old
  expire(CachedPage.find(:all, :conditions=>["expire_after < ?", Time.now]))
end

.expire_with(node, filter = nil) ⇒ Object

Remove cached pages related to the given node.



71
72
73
74
75
76
77
78
# File 'app/models/cached_page.rb', line 71

def expire_with(node, filter = nil)
  if filter
    list = CachedPage.find(:all, :conditions => filter[:conditions], :joins => "INNER JOIN cached_pages_nodes AS cpn ON cpn.cached_page_id = cached_pages.id AND cpn.node_id = #{node.id}")
  else
    list = node.cached_pages
  end
  expire(list)
end

Instance Method Details

#node_idsObject

Cached page’s creation context (list of node ids).



91
92
93
# File 'app/models/cached_page.rb', line 91

def node_ids
  Zena::Db.fetch_ids("SELECT node_id FROM cached_pages_nodes WHERE cached_page_id = '#{self[:id]}'", 'node_id')
end