Module: Adorn::Cache

Defined in:
lib/adorn/cache.rb

Class Method Summary collapse

Class Method Details

.[](key) ⇒ Hash

Return value at given key from the Adorn::Cache.

Parameters:

  • (Symbol, String)

Returns:

  • (Hash)


37
38
39
40
41
42
43
44
45
46
47
# File 'lib/adorn/cache.rb', line 37

def [](key)
  case key.class.to_s
  when 'String'
    Thread.current[:adorn_cache][key]
  when 'Symbol'
    Thread.current[:adorn_cache][key.to_s]
  else
    raise 'Unable to read key'
  end

end

.append!(key, value) ⇒ Hash

Instantiate or read from Adorn::Cache. Provide an interface to append to Adorn::Cache during request

Parameters:

  • (Symbol, *)

Returns:

  • (Hash)


25
26
27
28
29
30
31
# File 'lib/adorn/cache.rb', line 25

def append!(key, value)
  Thread.current[:adorn_cache] ||= Hash.new

  semaphore.synchronize do
    Thread.current[:adorn_cache].merge!({ "#{key}" => value })
  end rescue nil #rescue ThreadError
end

.clear!Hash

Remove all values from the Adorn::Cache.

Returns:

  • (Hash)


52
53
54
55
56
# File 'lib/adorn/cache.rb', line 52

def clear!
  semaphore.synchronize do
    Thread.current[:adorn_cache].clear
  end rescue nil #rescue ThreadError
end

.semaphoreMutex

Used to create a critical area during writes to Adorn::Cache

Returns:

  • (Mutex)


16
17
18
# File 'lib/adorn/cache.rb', line 16

def semaphore
  Thread.current[:semaphore] ||= Mutex.new
end