Class: ActiveSupport::Cache::MemoryStore

### Home Classes Methods

### In Files

- lib/active_support/cache/memory_store.rb

### Parent

ActiveSupport::Cache::Store

### Methods

- ::new
- #cleanup
- #clear
- #decrement
- #delete_matched
- #increment
- #prune
- #pruning?

### Class/Module Index

Quicksearch

- ActiveSupport
- ActiveSupport::Autoload
- ActiveSupport::BacktraceCleaner
- ActiveSupport::Base64
- ActiveSupport::BasicObject
- ActiveSupport::Benchmarkable
- ActiveSupport::BufferedLogger
- ActiveSupport::BufferedLogger::Severity
- ActiveSupport::Cache
- ActiveSupport::Cache::Entry
- ActiveSupport::Cache::FileStore
- ActiveSupport::Cache::MemCacheStore
- ActiveSupport::Cache::MemoryStore
- ActiveSupport::Cache::NullStore
- ActiveSupport::Cache::Store
- ActiveSupport::Cache::Strategy
- ActiveSupport::Cache::Strategy::LocalCache
- ActiveSupport::Cache::Strategy::LocalCache::LocalStore
- ActiveSupport::Callbacks
- ActiveSupport::Callbacks::ClassMethods
- ActiveSupport::Concern
- ActiveSupport::Configurable
- ActiveSupport::Configurable::ClassMethods
- ActiveSupport::Configurable::Configuration
- ActiveSupport::Dependencies
- ActiveSupport::Dependencies::ClassCache
- ActiveSupport::Dependencies::WatchStack
- ActiveSupport::Deprecation
- ActiveSupport::DescendantsTracker
- ActiveSupport::Duration
- ActiveSupport::FileUpdateChecker
- ActiveSupport::FileWatcher
- ActiveSupport::FileWatcher::Backend
- ActiveSupport::Gzip
- ActiveSupport::Gzip::Stream
- ActiveSupport::HashWithIndifferentAccess
- ActiveSupport::HashWithIndifferentAccess
- ActiveSupport::Inflector
- ActiveSupport::Inflector::Inflections
- ActiveSupport::InheritableOptions
- ActiveSupport::JSON
- ActiveSupport::JSON::Encoding
- ActiveSupport::JSON::Encoding::CircularReferenceError
- ActiveSupport::JSON::Encoding::Encoder
- ActiveSupport::JSON::Variable
- ActiveSupport::LogSubscriber
- ActiveSupport::LogSubscriber::TestHelper
- ActiveSupport::LogSubscriber::TestHelper::MockLogger
- ActiveSupport::Memoizable
- ActiveSupport::Memoizable::InstanceMethods
- ActiveSupport::MessageEncryptor
- ActiveSupport::MessageEncryptor::InvalidMessage
- ActiveSupport::MessageVerifier
- ActiveSupport::MessageVerifier::InvalidSignature
- ActiveSupport::Multibyte
- ActiveSupport::Multibyte::Chars
- ActiveSupport::Multibyte::EncodingError
- ActiveSupport::Multibyte::Unicode
- ActiveSupport::Multibyte::Unicode::Codepoint
- ActiveSupport::Multibyte::Unicode::UnicodeDatabase
- ActiveSupport::Notifications
- ActiveSupport::Notifications::Event
- ActiveSupport::Notifications::Fanout
- ActiveSupport::Notifications::Instrumenter
- ActiveSupport::OrderedHash
- ActiveSupport::OrderedOptions
- ActiveSupport::Railtie
- ActiveSupport::Rescuable
- ActiveSupport::Rescuable::ClassMethods
- ActiveSupport::SafeBuffer
- ActiveSupport::SafeBuffer::SafeConcatError
- ActiveSupport::StringInquirer
- ActiveSupport::TaggedLogging
- ActiveSupport::TestCase
- ActiveSupport::Testing
- ActiveSupport::Testing::Assertions
- ActiveSupport::Testing::Declarative
- ActiveSupport::Testing::Isolation
- ActiveSupport::Testing::Isolation::Forking
- ActiveSupport::Testing::Isolation::MiniTest
- ActiveSupport::Testing::Isolation::Subprocess
- ActiveSupport::Testing::Isolation::TestUnit
- ActiveSupport::Testing::Pending
- ActiveSupport::Testing::Performance
- ActiveSupport::Testing::Performance::Benchmarker
- ActiveSupport::Testing::Performance::ForClassicTestUnit
- ActiveSupport::Testing::Performance::ForMiniTest
- ActiveSupport::Testing::Performance::Metrics
- ActiveSupport::Testing::Performance::Metrics::Amount
- ActiveSupport::Testing::Performance::Metrics::Base
- ActiveSupport::Testing::Performance::Metrics::CpuTime
- ActiveSupport::Testing::Performance::Metrics::DigitalInformationUnit
- ActiveSupport::Testing::Performance::Metrics::GcRuns
- ActiveSupport::Testing::Performance::Metrics::GcTime
- ActiveSupport::Testing::Performance::Metrics::Memory
- ActiveSupport::Testing::Performance::Metrics::Objects
- ActiveSupport::Testing::Performance::Metrics::ProcessTime
- ActiveSupport::Testing::Performance::Metrics::Time
- ActiveSupport::Testing::Performance::Metrics::UserTime
- ActiveSupport::Testing::Performance::Metrics::WallTime
- ActiveSupport::Testing::Performance::Performer
- ActiveSupport::Testing::Performance::Profiler
- ActiveSupport::Testing::ProxyTestResult
- ActiveSupport::Testing::RemoteError
- ActiveSupport::Testing::SetupAndTeardown
- ActiveSupport::Testing::SetupAndTeardown::ClassMethods
- ActiveSupport::Testing::SetupAndTeardown::ForClassicTestUnit
- ActiveSupport::Testing::SetupAndTeardown::ForMiniTest
- ActiveSupport::TimeWithZone
- ActiveSupport::TimeZone
- ActiveSupport::XmlMini
- ActiveSupport::XmlMini_LibXMLSAX
- ActiveSupport::XmlMini_LibXMLSAX::HashBuilder
- ActiveSupport::XmlMini_NokogiriSAX
- ActiveSupport::XmlMini_NokogiriSAX::HashBuilder
- Test
- Test::Unit
- Test::Unit::Collector
- Test::Unit::Collector::ObjectSpace
- Object
- Object::HashWithIndifferentAccess
- Object::MissingSourceFile
- ERB
- ERB::Util
- I18n
- I18n::Railtie
- LoadError
- LoadError
- Logger
- Logger::SimpleFormatter
- Array
- Base64
- Benchmark
- BigDecimal
- Class
- Date
- DateTime
- Enumerable
- FalseClass
- File
- Float
- Hash
- Integer
- Kernel
- Module
- NameError
- NilClass
- Numeric
- Process
- QualifiedConstUtils
- Range
- Regexp
- String
- Symbol
- Time
- TrueClass
- URI

No matching classes.

# ActiveSupport::Cache::MemoryStore

A cache store implementation which stores everything into memory in the same process. If you’re running multiple Ruby on Rails server processes (which is the case if you’re using mongrel_cluster or Phusion Passenger), then this means that Rails server process instances won’t be able to share cache data with each other and this may not be the most appropriate cache in that scenario.

This cache has a bounded size specified by the :size options to the initializer (default is 32Mb). When the cache exceeds the allotted size, a cleanup will occur which tries to prune the cache down to three quarters of the maximum size by removing the least recently used entries.

MemoryStore is thread-safe.

### Public Class Methods

new(options = nil) click to toggle source

    # File lib/active_support/cache/memory_store.rb, line 19
    def initialize(options = nil)
      options ||= {}
      super(options)
      @data = {}
      @key_access = {}
      @max_size = options[:size] || 32.megabytes
      @max_prune_time = options[:max_prune_time] || 2
      @cache_size = 0
      @monitor = Monitor.new
      @pruning = false
    end

### Public Instance Methods

cleanup(options = nil) click to toggle source

    # File lib/active_support/cache/memory_store.rb, line 39
    def cleanup(options = nil)
      options = merged_options(options)
      instrument(:cleanup, :size => @data.size) do
        keys = synchronize{ @data.keys }
        keys.each do |key|
          entry = @data[key]
          delete_entry(key, options) if entry && entry.expired?
        end
      end
    end

clear(options = nil) click to toggle source

    # File lib/active_support/cache/memory_store.rb, line 31
    def clear(options = nil)
      synchronize do
        @data.clear
        @key_access.clear
        @cache_size = 0
      end
    end

decrement(name, amount = 1, options = nil) click to toggle source

Decrement an integer value in the cache.

    # File lib/active_support/cache/memory_store.rb, line 90
    def decrement(name, amount = 1, options = nil)
      synchronize do
        options = merged_options(options)
        if num = read(name, options)
          num = num.to_i - amount
          write(name, num, options)
          num
        else
          nil
        end
      end
    end

delete_matched(matcher, options = nil) click to toggle source

    # File lib/active_support/cache/memory_store.rb, line 103
    def delete_matched(matcher, options = nil)
      options = merged_options(options)
      instrument(:delete_matched, matcher.inspect) do
        matcher = key_matcher(matcher, options)
        keys = synchronize { @data.keys }
        keys.each do |key|
          delete_entry(key, options) if key.match(matcher)
        end
      end
    end

increment(name, amount = 1, options = nil) click to toggle source

Increment an integer value in the cache.

    # File lib/active_support/cache/memory_store.rb, line 76
    def increment(name, amount = 1, options = nil)
      synchronize do
        options = merged_options(options)
        if num = read(name, options)
          num = num.to_i + amount
          write(name, num, options)
          num
        else
          nil
        end
      end
    end

prune(target_size, max_time = nil) click to toggle source

To ensure entries fit within the specified memory prune the cache by removing the least recently accessed entries.

    # File lib/active_support/cache/memory_store.rb, line 52
    def prune(target_size, max_time = nil)
      return if pruning?
      @pruning = true
      begin
        start_time = Time.now
        cleanup
        instrument(:prune, target_size, :from => @cache_size) do
          keys = synchronize{ @key_access.keys.sort{|a,b| @key_access[a].to_f <=> @key_access[b].to_f} }
          keys.each do |key|
            delete_entry(key, options)
            return if @cache_size <= target_size || (max_time && Time.now - start_time > max_time)
          end
        end
      ensure
        @pruning = false
      end
    end

pruning?() click to toggle source

Returns true if the cache is currently being pruned.

    # File lib/active_support/cache/memory_store.rb, line 71
    def pruning?
      @pruning
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.