Sha256: f882cc4bc414eea633548b98a9369f4019e5615a4088957cf2a057e6642b6598

Contents?: true

Size: 1.73 KB

Versions: 21

Compression:

Stored size: 1.73 KB

Contents

class ReeDao::DaoCache
  include Ree::BeanDSL

  bean :dao_cache do
    link :deep_dup, from: :ree_object

    singleton
    after_init :setup
  end

  def setup
    @threads = {}
  end

  def add_thread_cache(thread)
    @threads[get_thread_object_id(thread)] ||= {}
  end

  def drop_thread_cache(thread)
    @threads.delete(get_thread_object_id(thread))
  end

  def get(table_name, primary_key)
    add_thread_cache(current_thread)
    add_table_name(table_name)

    @threads[current_thread_object_id][table_name][primary_key] 
  end

  def set(table_name, primary_key, data)
    add_thread_cache(current_thread)
    add_table_name(table_name)
    add_primary_key(table_name, primary_key)
   
    @threads[current_thread_object_id][table_name][primary_key] = deep_dup(data)
  end

  private

  def get_thread_object_id(thread)
    thread == Thread.main ? thread.object_id : get_parent_thread(thread)
  end

  def get_parent_thread(thread)
    return thread.object_id if thread == Thread.main

    get_parent_thread(thread.parent)
  end

  def current_thread
    Thread.current
  end

  def current_thread_object_id
    get_thread_object_id(current_thread)
  end

  def add_table_name(table_name)
    current_thr_obj_id = current_thread_object_id

    if !@threads[current_thr_obj_id]
      @threads[current_thr_obj_id] ||= {}
    end

    @threads[current_thr_obj_id][table_name] ||= {}
  end

  def add_primary_key(table_name, primary_key)
    current_thr_obj_id = current_thread_object_id

    if !@threads[current_thr_obj_id]
      @threads[current_thr_obj_id] ||= {}
    end

    if !@threads[current_thr_obj_id][table_name]
      @threads[current_thr_obj_id][table_name] ||= {}
    end

    @threads[current_thr_obj_id][table_name][primary_key] ||= {}
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
ree_lib-1.0.68 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb