Skip to content

Commit

Permalink
Merge pull request #2 from Arjeno/caches-page
Browse files Browse the repository at this point in the history
Caches page
  • Loading branch information
Arjeno committed Jun 23, 2012
2 parents 2736d10 + 4bec98d commit 22d49d8
Show file tree
Hide file tree
Showing 30 changed files with 571 additions and 104 deletions.
33 changes: 27 additions & 6 deletions README.md
Expand Up @@ -11,13 +11,33 @@ gem "catche"

## Controller caching

Controller caching is based on `caches_action` using the method `catche`.
Catche supports both action and page caching using the Rails methods `caches_action` and `caches_page`.

### Action caching

Catche's `catches_action` uses Rails' `caches_action` and therefore supports all options this method supports.

```ruby
class ProjectsController < ApplicationController
catches_action Project, :index, :show
end
```

### Page caching

Catche's `catches_page` uses Rails' `caches_page` and therefore supports all options this method supports.

```ruby
class ProjectsController < ApplicationController
catches_page Project, :index, :show
end
```

### Simple caching

```ruby
class ProjectsController < ApplicationController
catche Project, :index, :show
catches_action Project, :index, :show # or catches_page
end
```

Expand Down Expand Up @@ -48,7 +68,7 @@ end

```ruby
class TasksController < ApplicationController
catche Task, :index, :show
catches_action Task, :index, :show # or catches_page
end
```

Expand Down Expand Up @@ -104,10 +124,11 @@ This will result in the following expirations:
```ruby
class TasksController < ApplicationController
catche(
Task, # Configured cached model
:index, :show, # Actions
Task, # Configured cached model
:index, :show, # Actions
{
:resource_name => :task, # Name of your resource, defaults to your model name
:resource_name => :task, # Name of your resource, defaults to your model name
:type => :action, # Type of caching, :action or :page
}
)
end
Expand Down
61 changes: 60 additions & 1 deletion lib/catche/controller.rb
@@ -1 +1,60 @@
require 'catche/controller/base'
module Catche
module Controller

extend ActiveSupport::Concern
extend ActiveSupport::Autoload

eager_autoload do
autoload :Action
autoload :Page
end

include Action, Page

included do

class_attribute :catche_model,
:catche_resource_name,
:catche_constructed_tags

end

module ClassMethods

# Caches a controller action.
#
# catche Project, :index, :resource_name => :project
def catche(model, *args)
options = args.extract_options!

self.catche_model = model
self.catche_resource_name = options[:resource_name] || self.catche_model.name.downcase.to_sym

case options[:type]
when :page then caches_page *args, options
else caches_action *args, options
end
end

def catche?
self.catche_model.present?
end

end

def catche_tags
return @catche_tags if ! @catche_tags.nil?

if resource = Catche::ResourceLoader.fetch_one(self, self.class.catche_resource_name)
tags = Catche::Tag::Collect.resource(resource)
@catche_tags = tags[:set]
else
tags = Catche::Tag::Collect.collection(self, self.class.catche_model)
@catche_tags = tags[:set]
end
end

end
end

ActionController::Base.send :include, Catche::Controller
30 changes: 30 additions & 0 deletions lib/catche/controller/action.rb
@@ -0,0 +1,30 @@
module Catche
module Controller
module Action

extend ActiveSupport::Concern

module ClassMethods

# Caches an action in Rails.cache
# See ActionController `caches_action` for more information
#
# catches_action Project, :index
def catches_action(model, *args)
catche model, *args, :type => :action
end

end

def _save_fragment(name, options={})
if self.class.catche?
key = fragment_cache_key(name)
Catche::Tag.tag_view! key, *catche_tags
end

super
end

end
end
end
57 changes: 0 additions & 57 deletions lib/catche/controller/base.rb

This file was deleted.

35 changes: 35 additions & 0 deletions lib/catche/controller/page.rb
@@ -0,0 +1,35 @@
module Catche
module Controller
module Page

extend ActiveSupport::Concern

module ClassMethods

# Caches an action by file
# See ActionController `caches_page` for more information
#
# catches_action Project, :index
def catches_page(model, *args)
catche model, *args, :type => :page
end

def cache_page(content, path, extension = nil, gzip = Zlib::BEST_COMPRESSION)
if self.catche?
Catche::Tag.tag_page! page_cache_path(path, extension), *catche_constructed_tags
end

super
end

end

def cache_page(content = nil, options = nil, gzip = Zlib::BEST_COMPRESSION)
self.class.catche_constructed_tags = catche_tags
super
end


end
end
end
17 changes: 17 additions & 0 deletions lib/catche/expire.rb
@@ -0,0 +1,17 @@
require 'catche/expire/view'
require 'catche/expire/page'

module Catche
module Expire

class << self

def expire!(data)
Catche::Expire::View.expire! *data[:views]
Catche::Expire::Page.expire! *data[:pages]
end

end

end
end
22 changes: 22 additions & 0 deletions lib/catche/expire/page.rb
@@ -0,0 +1,22 @@
module Catche
module Expire
module Page

class << self

# Expires cache by deleting the associated files
# Uses the same flow as defined in `expire_page` in ActionController
#
# Catche::Expire::Page.expire!('/public/projects.html')
def expire!(*paths)
paths.each do |path|
File.delete(path) if File.exist?(path)
File.delete(path + '.gz') if File.exist?(path + '.gz')
end
end

end

end
end
end
20 changes: 20 additions & 0 deletions lib/catche/expire/view.rb
@@ -0,0 +1,20 @@
module Catche
module Expire
module View

class << self

# Expires cache by deleting the associated keys
#
# Catche::Expire::View.expire!('projects')
def expire!(*keys)
keys.each do |key|
Catche.adapter.delete key
end
end

end

end
end
end

0 comments on commit 22d49d8

Please sign in to comment.