Code Monkey home page Code Monkey logo

catche's Introduction

Important

This gem is no longer maintained. I strongly suggest using russian-doll-caching with the gem cache_digests.

Catche Build Status

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.

Installation

Add this to your Gemfile and run bundle.

gem "catche"

Troubleshooting

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.

Controller caching

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.

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.

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

Simple caching

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

Associative caching

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

Multiple associations

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

Advanced configuration

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

View caching

Catche supports view caching using the Rails methods cache.

Resource caching

<% catche @project do %>
  <%= @project.title %>
<% end %>

Collection caching

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 %>

How does it work?

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.

Manually expiring a cache

@task.expire_resource!
@task.expire_collection!
@task.expire_resource_and_collection!

Supported cache stores

Catche currently supports:

  • MemoryStore
  • Memcached
  • Dalli

Want support for more? Just fork and open up a pull request.

Roadmap

No features planned for now. In need of a feature? Please open up an issue, or pull request.

License

This project is released under the MIT license.

catche's People

Contributors

arjeno avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.