### Home Classes Methods

### In Files

- lib/ramaze/helper/cache.rb

### Namespace

- MODULE Ramaze::Helper::Cache::SingletonMethods

### Methods

- ::included
- #cache_value
- #cache_wrap

### Class/Module Index

Quicksearch

- Ramaze
- Ramaze::App
- Ramaze::AppGraph
- Ramaze::Bin
- Ramaze::Bin::Console
- Ramaze::Bin::Create
- Ramaze::Bin::Helper
- Ramaze::Bin::Restart
- Ramaze::Bin::Runner
- Ramaze::Bin::Start
- Ramaze::Bin::Status
- Ramaze::Bin::Stop
- Ramaze::Cache
- Ramaze::Cache::LRU
- Ramaze::Cache::LocalMemCache
- Ramaze::Cache::MemCache
- Ramaze::Cache::Redis
- Ramaze::Cache::Sequel
- Ramaze::Controller
- Ramaze::CoreExtensions
- Ramaze::CoreExtensions::Array
- Ramaze::CoreExtensions::Binding
- Ramaze::CoreExtensions::Kernel
- Ramaze::CoreExtensions::Numeric
- Ramaze::CoreExtensions::Object
- Ramaze::CoreExtensions::Proc
- Ramaze::CoreExtensions::String
- Ramaze::Current
- Ramaze::DefaultController
- Ramaze::Dictionary
- Ramaze::Fiber
- Ramaze::Files
- Ramaze::GemSetup
- Ramaze::Gestalt
- Ramaze::Helper
- Ramaze::Helper::Auth
- Ramaze::Helper::Bench
- Ramaze::Helper::BlueForm
- Ramaze::Helper::BlueForm::Form
- Ramaze::Helper::CSRF
- Ramaze::Helper::Cache
- Ramaze::Helper::Cache::SingletonMethods
- Ramaze::Helper::Disqus
- Ramaze::Helper::Email
- Ramaze::Helper::Erector
- Ramaze::Helper::Flash
- Ramaze::Helper::Formatting
- Ramaze::Helper::Gestalt
- Ramaze::Helper::Gravatar
- Ramaze::Helper::Identity
- Ramaze::Helper::Layout
- Ramaze::Helper::Layout::SingletonMethods
- Ramaze::Helper::Link
- Ramaze::Helper::Localize
- Ramaze::Helper::Localize::Dictionary
- Ramaze::Helper::Localize::Parser
- Ramaze::Helper::Markaby
- Ramaze::Helper::Maruku
- Ramaze::Helper::Paginate
- Ramaze::Helper::Paginate::Paginator
- Ramaze::Helper::Paginate::Paginator::ArrayPager
- Ramaze::Helper::Paginate::Paginator::DataMapperPager
- Ramaze::Helper::Remarkably
- Ramaze::Helper::RequestAccessor
- Ramaze::Helper::SendFile
- Ramaze::Helper::SimpleCaptcha
- Ramaze::Helper::Stack
- Ramaze::Helper::Thread
- Ramaze::Helper::Ultraviolet
- Ramaze::Helper::Upload
- Ramaze::Helper::Upload::ClassMethods
- Ramaze::Helper::Upload::UploadedFile
- Ramaze::Helper::UserHelper
- Ramaze::Helper::UserHelper::Wrapper
- Ramaze::Helper::XHTML
- Ramaze::LRUHash
- Ramaze::Logger
- Ramaze::Logger::Analogger
- Ramaze::Logger::Growl
- Ramaze::Logger::Informer
- Ramaze::Logger::Knotify
- Ramaze::Logger::LogHub
- Ramaze::Logger::Logger
- Ramaze::Logger::RotatingInformer
- Ramaze::Logger::Syslog
- Ramaze::Logger::Xosd
- Ramaze::Logging
- Ramaze::MiddlewareCompiler
- Ramaze::Plugin
- Ramaze::Reloader
- Ramaze::Reloader::Hooks
- Ramaze::Reloader::WatchInotify
- Ramaze::Reloader::WatchStat
- Ramaze::Reloader::Watcher
- Ramaze::Request
- Ramaze::Response
- Ramaze::SpecLogger
- Ramaze::Struct
- Ramaze::View
- Ramaze::View::Erector
- Ramaze::View::Erubis
- Ramaze::View::Ezamar
- Ramaze::View::Gestalt
- Ramaze::View::Haml
- Ramaze::View::Liquid
- Ramaze::View::Liquid::Tag
- Ramaze::View::Lokar
- Ramaze::View::Mustache
- Ramaze::View::Mustache::RamazeContext
- Ramaze::View::Nagoro
- Ramaze::View::Remarkably
- Ramaze::View::Sass
- Ramaze::View::Slippers
- Ramaze::View::Tagz
- Ramaze::View::Tagz::Methods
- Ramaze::View::Tenjin
- Nagoro
- Nagoro::Pipe
- Nagoro::Pipe::RenderPartial
- Bacon
- Bacon::PrettyOutput
- Erector
- Erector::Widget
- Rack
- Rack::RouteExceptions
- Tenjin
- Tenjin::Template
- BlankSlate
- Controller
- Fiber
- FiberError
- MainController
- Object
- OrderedSet
- Thread

No matching classes.

# Ramaze::Helper::Cache

Caching of simple objects and whole action responses.

### Public Class Methods

included(into) click to toggle source

Setup needed traits, add the singleton methods and add the caches used by this helper.

@param [Class] into Class that this Module is included into @author manveru

    # File lib/ramaze/helper/cache.rb, line 16
    def self.included(into)
      into.extend(SingletonMethods)
      into.add_action_wrapper(6.0, :cache_wrap)
      into.trait[:cache_action] ||= Set.new
      Ramaze::Cache.add(:action, :cache_helper_value)
    end

### Public Instance Methods

cache_value(key = nil, options = {}) click to toggle source

This method is used to access Ramaze::Cache.cache_helper_value. It provides an easy way to cache long-running computations, gathering external resources like RSS feeds or DB queries that are the same for every user of an application. This method changes behaviour if a block is passed, which can be used to do lazy computation of the cached value conveniently when using a custom TTL or longer expressions that don't fit on one line with ||=.

@example to get the cache object directly

    count = cache_value[:count] ||= Article.count

@example with block

    count = cache_value(:count){ Article.count }
    count = cache_value(:count, :ttl => 60){ Article.count }

@return [Object] The cache wrapper assigned for :cache_helper_value @see Innate::Cache @author manveru

    # File lib/ramaze/helper/cache.rb, line 86
    def cache_value(key = nil, options = {})
      cache = Ramaze::Cache.cache_helper_value

      if key and block_given?
        if found = cache[key]
          found
        else
          cache.store(key, yield, options)
        end
      else
        cache
      end
    end

cache_wrap(action) click to toggle source

@param [Action] action The currently wrapped action @yield The next block in wrap_action_call @return [String] the response body @see Innate::Node#wrap_action_call @author manveru

    # File lib/ramaze/helper/cache.rb, line 30
    def cache_wrap(action)
      cache = Innate::Cache.action

      ancestral_trait[:cache_action].each do |cache_action|
        temp  = cache_action.dup
        block = temp.delete(:key)
        ttl   = temp.delete(:ttl)

        if temp.all?{|key, value| action[key] == value }
          cache_key = action.full_path
          cache_key << "_#{action.instance.instance_eval(&block).to_s}" if block

          if cached = cache[cache_key]
            action.options[:content_type] = cached[:type]
          else
            cached = {
              :body => catch(:respond) { yield },
              :type => response['Content-Type']
            }

            if ttl
              cache.store(cache_key, cached, :ttl => ttl)
            else
              cache.store(cache_key, cached)
            end
          end

          return cached[:body]
        end
      end

      yield
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.