Class: EMRPC::ReferenceSavior

Inherits:
Object
  • Object
show all
Defined in:
lib/emrpc/archive/reference_savior.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ReferenceSavior

:timeout - specifies a time interval for keeping a reference to a value



8
9
10
11
12
13
14
15
# File 'lib/emrpc/archive/reference_savior.rb', line 8

def initialize(options)
  @timeout        = options[:timeout] || 60
  @timer          = options[:timer] || Timers::EVENTED
  @timeout_thread = @timer.call(@timeout, method(:swap_pages))
  # We keep two previous pages to ensure, that the value lives for at least +timeout+ seconds.
  @page1 = new_page
  @page2 = new_page
end

Instance Attribute Details

#timeoutObject

Returns the value of attribute timeout.



6
7
8
# File 'lib/emrpc/archive/reference_savior.rb', line 6

def timeout
  @timeout
end

Instance Method Details

#get(oid) ⇒ Object

Raises:



23
24
25
26
27
28
29
# File 'lib/emrpc/archive/reference_savior.rb', line 23

def get(oid)
  oid = oid.to_i
  val = @page1[oid] || @page2[oid]
  raise StaleReference.new("Object id #{oid} was not found in a ReferenceSavior cache. It could have been garbage collected after #{@timeout} sec.") unless val
  ping(oid, val)
  val
end

#new_pageObject



43
44
45
# File 'lib/emrpc/archive/reference_savior.rb', line 43

def new_page
  Hash.new
end

#ping(oid, val) ⇒ Object



31
32
33
# File 'lib/emrpc/archive/reference_savior.rb', line 31

def ping(oid, val)
  @page1[oid] = val
end

#set(val) ⇒ Object



17
18
19
20
21
# File 'lib/emrpc/archive/reference_savior.rb', line 17

def set(val)
  oid = val.object_id
  ping(oid, val)
  MethodProxy.new(Wrapper.new(oid))
end

#swap_pagesObject

and is replaced by a brand new cache page.



38
39
40
41
# File 'lib/emrpc/archive/reference_savior.rb', line 38

def swap_pages
  @page2 = @page1
  @page1 = new_page
end