Class: Thread

Inherits:
Object
  • Object
show all
Defined in:
lib/ferret/utils/thread_local.rb

Instance Method Summary collapse

Instance Method Details

#clear_localObject



25
26
27
# File 'lib/ferret/utils/thread_local.rb', line 25

def clear_local
  (@ferret_cache ||= {}).clear
end

#get_local(key) ⇒ Object

Get the local value for the thread



16
17
18
# File 'lib/ferret/utils/thread_local.rb', line 16

def get_local(key)
  return (@ferret_cache ||= {})[key.object_id]
end

#local_sizeObject

Returns the number of local variables stored. Useful for testing.



21
22
23
# File 'lib/ferret/utils/thread_local.rb', line 21

def local_size
  return (@ferret_cache ||= {}).size
end

#make_deleterObject



3
4
5
# File 'lib/ferret/utils/thread_local.rb', line 3

def make_deleter
  lambda{|id| @ferret_cache.delete(id)}
end

#set_local(key, value) ⇒ Object

Set the local value for the thread



8
9
10
11
12
13
# File 'lib/ferret/utils/thread_local.rb', line 8

def set_local(key, value)
  @del ||= make_deleter
  @ferret_cache ||= {}
  ObjectSpace.define_finalizer(key, @del)
  @ferret_cache[key.object_id] = value
end