Sha256: 262357e053afa387c2adcbcb0fed1989e88a6bc758b4abc1ed77d7a70c784634

Contents?: true

Size: 1.71 KB

Versions: 18

Compression:

Stored size: 1.71 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.parent == Thread.main ? thread.object_id : get_parent_thread(thread)
  end

  def get_parent_thread(thread)
    return thread.object_id if thread.parent == Thread.main || 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)
    if !@threads[current_thread_object_id]
      @threads[current_thread_object_id] ||= {}
    end

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

  def add_primary_key(table_name, primary_key)
    if !@threads[current_thread_object_id]
      @threads[current_thread_object_id] ||= {}
    end

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

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

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
ree_lib-1.0.67 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.66 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.65 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.64 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.63 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.62 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.61 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.60 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.59 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.58 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.57 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.56 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.55 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.54 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.53 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.52 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.51 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb
ree_lib-1.0.50 lib/ree_lib/packages/ree_dao/package/ree_dao/beans/dao_cache.rb