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.88 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.87 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.86 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.85 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.84 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.83 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.82 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.81 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.80 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.79 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.78 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.77 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.76 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.75 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.74 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.73 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.72 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.71 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.70 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.69 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb