This gem is no longer maintained. I strongly suggest using russian-doll-caching with the gem cache_digests.
- https://github.com/rails/cache_digests
- http://railscasts.com/episodes/387-cache-digests
- http://37signals.com/svn/posts/3112-how-basecamp-next-got-to-be-so-damn-fast-without-using-much-client-side-ui
Catche is a caching library for Ruby on Rails. It automates resource and collection caching/expiration. It basically tags cached outputs and expires those tags based on configuration.
Add this to your Gemfile and run bundle
.
gem "catche"
This gem is still in beta (v0.x), this means that certain structures, especially storing data, may change. If you're experiencing problems please try clearing the cache using Rails.cache.clear
. If that doesn't work please open up a new issue.
Catche supports both action and page caching using the Rails methods caches_action
and caches_page
.
Catche's catches_action
uses Rails' caches_action
and therefore supports all options this method supports.
class ProjectsController < ApplicationController
catches_action Project, :index, :show
end
Catche's catches_page
uses Rails' caches_page
and therefore supports all options this method supports.
class ProjectsController < ApplicationController
catches_page Project, :index, :show
end
class ProjectsController < ApplicationController
catches_action Project, :index, :show # or catches_page
end
This will result in the following expirations, depending on your routes configuration:
@project.update_attributes({ :title => 'Update!' }) # or @project.destroy
# => Expires: /projects
# => Expires: /projects/1
@project.create
# => Expires: /projects
Catche supports associative caching.
class Task < ActiveRecord::Base
catche :through => :project
end
class TasksController < ApplicationController
catches_action Task, :index, :show # or catches_page
end
This will result in the following expirations:
@task.update_attributes({ :title => 'Update!' }) # or @task.destroy
# => Expires: /tasks
# => Expires: /projects/1/tasks
# => Expires: /projects/1/tasks/1
@project.tasks.create
# => Expires: /tasks
# => Expires: /projects/1/tasks
You can use as many associations as you would like. Associations are not nested.
class Task < ActiveRecord::Base
catche :through => [:user, :project]
end
This will result in the following expirations:
@task.update_attributes({ :title => 'Update!' }) # or @task.destroy
# => Expires: /tasks
# => Expires: /projects/1/tasks
# => Expires: /projects/1/tasks/1
# => Expires: /users/1/tasks
# => Expires: /users/1/tasks/1
@project.tasks.create
# => Expires: /tasks
# => Expires: /projects/1/tasks
# => Expires: /users/1/tasks
class TasksController < ApplicationController
catche(
Task, # Configured cached model
:index, :show, # Actions
{
:resource_name => :task, # Name of your resource, defaults to your model name
:type => :action, # Type of caching, :action or :page
}
)
end
class Task < ActiveRecord::Base
catche(
:through => [:user, :project], # Associations
:tag_identifier => :id, # Unique identifier for the resource
:class => Task, # Class to use as tag scope
:collection_tag => 'tasks', # Name of the tag scope for this model,
)
end
Catche supports view caching using the Rails methods cache
.
<% catche @project do %>
<%= @project.title %>
<% end %>
Because a collection may be an array, you will need to pass along the configured model you wish to use;
<% catche @projects, :model => Project do %>
<% @projects.each do |project| %>
<%= project.title %>
<% end %>
<% end %>
Catche intercepts a cached value and tags this value using the unique identifier for the given/loaded resource or collection. Once a resource expires it will expire the tagged cached values, such as the resource itself and the collection it belongs to.
Catche::Tag::Collect.resource(@task) # { :set => ["tasks_1"], :expire => ["tasks_1"] }
Catche::Tag::Collect.collection(@task) # { :set => ["projects_1_tasks"], :expire => ["tasks", "projects_1_tasks"] }
The tags will point to different cached values, for example pointing to a cached key or a cached filepath.
@task.expire_resource!
@task.expire_collection!
@task.expire_resource_and_collection!
Catche currently supports:
- MemoryStore
- Memcached
- Dalli
Want support for more? Just fork and open up a pull request.
No features planned for now. In need of a feature? Please open up an issue, or pull request.
This project is released under the MIT license.