Class: Opod::MemoryCache

Inherits:
Object
  • Object
show all
Defined in:
lib/opod/memory.rb

Overview

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

Direct Known Subclasses

DrbCache

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MemoryCache

Returns a new instance of MemoryCache.



13
14
15
16
17
18
19
# File 'lib/opod/memory.rb', line 13

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

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



11
12
13
# File 'lib/opod/memory.rb', line 11

def hash
  @hash
end

Instance Method Details

#allObject Also known as: values

Return all objects in the cache.



76
77
78
# File 'lib/opod/memory.rb', line 76

def all
  @hash.values
end

#delete(key, options = nil) ⇒ Object Also known as: remove

Delete an object from the cache.



46
47
48
# File 'lib/opod/memory.rb', line 46

def delete(key, options = nil)
  @hash.delete(key)
end

#delete_if(&block) ⇒ Object



51
52
53
# File 'lib/opod/memory.rb', line 51

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

#gc!Object

Perform session garbage collection. Typically this method is called from a cron like mechanism.



58
59
60
# File 'lib/opod/memory.rb', line 58

def gc!
  delete_if { |key, s| s.expired? }
end

#get(key, options = nil) ⇒ Object Also known as: read, []

Get an object from the cache.



29
30
31
# File 'lib/opod/memory.rb', line 29

def get(key, options = nil)
  @hash[key]
end

#keysObject

Return all keys in the cache.



70
71
72
# File 'lib/opod/memory.rb', line 70

def keys
  @hash.keys
end

#mappingObject

Return the mapping.



64
65
66
# File 'lib/opod/memory.rb', line 64

def mapping
  @hash
end

#set(key, value = nil, options = nil) ⇒ Object Also known as: put, write, []=

Put an object in the cache.



37
38
39
# File 'lib/opod/memory.rb', line 37

def set(key, value = nil, options = nil)
  @hash[key] = value
end

#update(hash) ⇒ Object

Was orig. in Cache.



23
24
25
# File 'lib/opod/memory.rb', line 23

def update(hash)
  hash.each { |key, value| self[key] = value }
end