Skip to content
This repository has been archived by the owner on Nov 21, 2017. It is now read-only.
/ tagged-cache Public archive

ActiveSupport cache adapter with tagged dependencies

Notifications You must be signed in to change notification settings

antage/tagged-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tagged-cache

DESCRIPTION:

ActiveSupport cache adapter with tagged dependencies.

SYNOPSIS:

RUBY EXAMPLE:

require 'active_support'

cache = ActiveSupport::Cache.lookup_store(:tagged_store,
  tag_store: [:memory_store, namespace: "tags"],
  entity_store: [:memory_store, namespace: "entities"]
)

# tags management
cache.read_tag("tag1") # returns Fixnum
cache.read_tags("tag1", "tag2") # returns Hash { "tag1" => tag1_version, "tag2" => tag2_version }
cache.touch_tag("tag1") # increment tag1's version

# reading and writing cache
cache.write("cache key", value, depends: ["tag1", "tag2"])
cache.fetch("cache key", depends: ["tag1", "tag2]) { value }

# or

cache.tagged_fetch("cache key") do |entry|
  entry.depends "tag1"
  entry << "tag2"
  entry.concat ["tag3", "tag4"]
  entry.concat "tag5", "tag6"
  value
end

# any object with method 'cache_tag' can be used as tag name
class A
  attr_accessor :id

  def cache_tag
    "class/A/#{id}"
  end
end

cache.tagged_fetch("some cache path") do |entry|
  obj1 = A.new
  obj1.id = 1

  obj2 = A.new
  obj2.id = 2

  entry.depends obj1
  entry.depends obj2

  [obj1, obj2]
end

RAILS EXAMPLE:

# config/environments/production.rb:
Rails3::Application.configure do
...
  config.cache_store = :tagged_store, { tag_store: [:mem_cache_store, %w(localhost), { namespace: "tags" }], entity_store: [:mem_cache_store, %w(localhost), { namespace: "entities" }] }
...
end

# app/controller/samples_controller.rb:
class SamplesController < ApplicationController
  # specify :depends using lambda
  caches_action :show, depends: ->(c) {
    c.instance_eval do
      # @sample must respond to #cache_tag method
      [@sample]
    end
  }

  # specify :depends using method name
  caches_action :index, depends: :index_tags

  def index
    @samples = Sample.all
  end

  def show
    @sample = Sample.find(params[:id])
  end

  private

  def index_tags
    # each element of @samples must respond to #cache_tag method
    @samples
  end
end

REQUIREMENTS:

  • activesupport >= 3.0.0

INSTALL:

  • sudo gem install tagged-cache

LICENSE:

(The MIT License)

Copyright © 2010-2014 Anton Ageev

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

ActiveSupport cache adapter with tagged dependencies

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages