Sha256: d1c763ad26af45697b509d97285268008cab1a5168cab1c096f4dda763099884

Contents?: true

Size: 1.25 KB

Versions: 4

Compression:

Stored size: 1.25 KB

Contents

require 'facet/synchash'

require 'glue/cache'

module Glue

# A cache backed in memory. 
#--
# This implementation is also the base for the Drb Cache.
#++

class MemoryCache < Cache
  attr :hash

  def initialize(options = {})
    if options[:sync]
      @hash = SyncHash
    else
      @hash = {}
    end
  end

  # Get an object from the cache.
  
  def get(key, options = nil)
    @hash[key]
  end
  alias_method :read, :get
  alias_method :[], :get

  # Put an object in the cache.
  
  def set(key, value = nil, options = nil)
    @hash[key] = value
  end
  alias_method :put, :set
  alias_method :write, :set
  alias_method :[]=, :set
  
  # Delete an object from the cache.
  
  def delete(key, options = nil) 
    @hash.delete(key)
  end
  alias_method :remove, :delete

  def delete_if(&block)
    @hash.delete_if(&block)
  end

  # Perform session garbage collection. Typically this method
  # is called from a cron like mechanism.
  
  def gc!
    delete_if { |key, s| s.expired? }
  end

  # Return the mapping.
  
  def mapping
    @hash
  end

  # Return all keys in the cache.
  
  def keys
    @hash.keys
  end

  # Return all objects in the cache.
  
  def all
    @hash.values
  end
  alias_method :values, :all
  
end
  
end

# * George Moschovitis <gm@navel.gr>

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
glue-0.28.0 lib/glue/cache/memory.rb
glue-0.29.0 lib/glue/cache/memory.rb
glue-0.30.0 lib/glue/cache/memory.rb
glue-0.31.0 lib/glue/cache/memory.rb