Class: Ramaze::Cache::MemCache

### Home Classes Methods

### In Files

- lib/ramaze/cache/memcache.rb

### Parent

Object

### Methods

- ::new
- ::using
- #cache_clear
- #cache_delete
- #cache_fetch
- #cache_setup
- #cache_store

### Included Modules

- Cache::API
- Innate::Traited

### Files

- Gemfile
- Rakefile

### Class/Module Index

Quicksearch

- Ramaze
- Ramaze::App
- Ramaze::Bin
- Ramaze::Bin::Create
- Ramaze::Bin::Runner
- Ramaze::Cache
- Ramaze::Cache::LRU
- Ramaze::Cache::LocalMemCache
- Ramaze::Cache::MemCache
- Ramaze::Cache::Redis
- Ramaze::Cache::Sequel
- Ramaze::Controller
- Ramaze::CoreExtensions
- Ramaze::CoreExtensions::Object
- Ramaze::CoreExtensions::String
- Ramaze::Current
- Ramaze::DefaultController
- Ramaze::Files
- 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::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::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::Informer
- Ramaze::Logger::LogHub
- Ramaze::Logger::Logger
- Ramaze::Logger::RotatingInformer
- Ramaze::Logger::Syslog
- Ramaze::Logging
- Ramaze::Reloader
- Ramaze::Reloader::Hooks
- Ramaze::Reloader::WatchInotify
- Ramaze::Reloader::WatchStat
- Ramaze::Request
- Ramaze::Response
- Ramaze::SpecLogger
- 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
- Innate
- Innate::View
- Innate::View::Slim
- Nagoro
- Nagoro::Pipe
- Nagoro::Pipe::RenderPartial
- Bacon
- Bacon::PrettyOutput
- Erector
- Erector::Widget
- Rack
- Rack::RouteExceptions
- Tenjin
- Tenjin::Template
- BlankSlate
- Controller
- MainController
- Object

No matching classes.

# Ramaze::Cache::MemCache

Cache driver for the Memcache storage engine. Memcache is a key/value store that's extremely useful for caching data such as views or API responses. More inforamtion about Memcache can be found on it's website: <memcached.org/>.

Note that this cache driver requires the Dalli gem rather than Memcache Client. The reason for this is that the Memcache client hasn't been updated in over a year and Memcache has changed quite a bit. Dalli is also supposed to be faster and better coded. This cache driver will also try to load the kgio Gem if it's installed, if it's not it will just continue to operate but you won't get the nice speed boost.

This driver works similar to Ramaze::Cache::Sequel in that it allows you to specify instance specific options uisng the using() method:

    Ramaze::Cache.options.session = Ramaze::Cache::MemCache.using(
      :compression => false
    )

All options sent to the using() method will be sent to Dalli.

@example Using the default options

    Ramaze::Cache.options.view = Ramaze::Cache::MemCache
    Ramaze.setup_dependencies

    Ramaze::Cache.view.store(:my_view, 'Hello Ramaze')

@example Using custom options

    Ramaze::Cache.options.view = Ramaze::Cache::MemCache.using(
      :compression => false,
      :servers     => ['localhost:11211', 'localhost:112112']
    )

    Ramaze.setup_dependencies
    Ramaze::Cache.view.store(:my_view, 'Hello Ramaze')

@author Yorick Peterse @since 04-05-2011

### Constants

MAX_TTL

  The maximum Time To Live that can be used in Memcache

### Attributes

options[RW]
options[RW]

Hash containing all the default options merged with the user specified ones

### Public Class Methods

new(options = {}) click to toggle source

Creates a new instance of the cache class.

@author Michael Fellinger @since 04-05-2011 @param [Hash] options A hash with custom options, see

    Ramaze::Cache::MemCache.using for all available options.

    # File lib/ramaze/cache/memcache.rb, line 116
    def initialize(options = {})
      self.class.options ||= Ramaze::Cache::MemCache.trait[:default].merge(
        options
      )

      @options = options.merge(self.class.options)
    end

using(options = {}) click to toggle source

This method will create a subclass of Ramaze::Cache::MemCache with all the custom options set. All options set in this method will be sent to Dalli as well.

Using this method allows you to use different memcache settings for various parts of Ramaze. For example, you might want to use servers A and B for storing the sessions but server C for only views. Most of the way this method works was inspired by Ramaze::Cache::Sequel which was contributed by Lars Olsson.

@example

    Ramaze::Cache.options.session = Ramaze::Cache::MemCache.using(
      :compression => false,
      :username    => 'ramaze',
      :password    => 'ramaze123',
      :servers     => ['othermachine.com:12345']
    )

@author Yorick Peterse @since 04-05-2011 @param [Hash] options A hash containing all configuration options to

    use for Dalli. For more information on all the available options you
    can read the README in their repository. This repository can be found
    here: https://github.com/mperham/dalli

    # File lib/ramaze/cache/memcache.rb, line 102
    def using(options = {})
      merged = Ramaze::Cache::MemCache.trait[:default].merge(options)
      Class.new(self) { @options = merged }
    end

### Public Instance Methods

cache_clear() click to toggle source

Removes all items from the cache.

@author Yorick Peterse @since 04-05-2011

    # File lib/ramaze/cache/memcache.rb, line 155
    def cache_clear
      @client.flush
    end

cache_delete(*keys) click to toggle source

Removes the specified keys from the cache.

@author Yorick Peterse @since 04-05-2011 @param [Array] keys The keys to remove from the cache.

    # File lib/ramaze/cache/memcache.rb, line 166
    def cache_delete(*keys)
      keys.each do |key|
        @client.delete(key)
      end
    end

cache_fetch(key, default = nil) click to toggle source

Fetches the specified key from the cache. It the value was nil the default value will be returned instead.

@author Yorick Peterse @since 04-05-2011 @param [String] key The name of the key to retrieve. @param [Mixed] default The default value. @return [Mixed]

    # File lib/ramaze/cache/memcache.rb, line 182
    def cache_fetch(key, default = nil)
      value = @client.get(key)

      if value.nil?
        return default
      else
        return value
      end
    end

cache_setup(hostname, username, appname, cachename) click to toggle source

Prepares the cache by creating the namespace and an instance of a Dalli client.

@author Yorick Peterse @since 04-05-2011 @param [String] hostname The hostname of the machine running the

    application.

@param [String] username The name of the user executing the process @param [String] appname Unique identifier for the application. @param [String] cachename The namespace to use for this cache instance.

    # File lib/ramaze/cache/memcache.rb, line 136
    def cache_setup(hostname, username, appname, cachename)
      # Validate the maximum TTL
      if options[:expires_in] > MAX_TTL
        raise(ArgumentError, "The maximum TTL of Memcache is 30 days")
      end

      options[:namespace] = [
        hostname, username, appname, cachename
      ].compact.join('-')

      @client = ::Dalli::Client.new(options[:servers], options)
    end

cache_store(key, value, ttl = nil, options = {}) click to toggle source

Sets the given key to the specified value. Optionally you can specify a hash with options specific to the key. Once a key has been stored it's value will be returned.

@author Yorick Peterse @since 04-05-2011 @param [String] key The name of the key to store. @param [Mixed] value The value to store in Memcache. @param [Fixnum] ttl The Time To Live to use for the current key. @param [Hash] options A hash containing options specific for the

    specified key.

@return [Mixed]

    # File lib/ramaze/cache/memcache.rb, line 206
    def cache_store(key, value, ttl = nil, options = {})
      ttl = options.delete(:ttl) || @options[:expires_in]

      if ttl > MAX_TTL
        raise(ArgumentError, "The maximum TTL of Memcache is 30 days")
      end

      @client.set(key, value, ttl, options)

      return value
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.