Class: ArCache::Table

Inherits:
Object
  • Object
show all
Includes:
Marshal
Defined in:
lib/ar_cache/table.rb

Constant Summary collapse

OPTIONS =
%i[disabled select_disabled unique_indexes].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Marshal

#delete, #read, #write

Constructor Details

#initialize(table_name) ⇒ Table

Returns a new instance of Table.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ar_cache/table.rb', line 25

def initialize(table_name)
  @name = table_name
  @cache_key_prefix = "arcache:#{@name}:version"
  @primary_key = connection.primary_key(@name)
  columns = connection.columns(@name)
  options = ArCache::Configuration.get_table_options(@name)
  @unique_indexes = normalize_unique_indexes(options.delete(:unique_indexes), columns).freeze
  options.each { |k, v| instance_variable_set("@#{k}", v) }
  @disabled = true if @primary_key.nil? # ArCache is depend on primary key implementation.
  @column_names = columns.map(&:name).freeze
  @column_indexes = @unique_indexes.flatten.uniq.freeze
  coder = ArCache::Configuration.coder
  @md5 = Digest::MD5.hexdigest("#{coder}-#{@disabled}-#{columns.to_json}")

  ArCache::Record.store(self)

  self.class.all << self
end

Instance Attribute Details

#allObject (readonly)

Returns the value of attribute all.



9
10
11
# File 'lib/ar_cache/table.rb', line 9

def all
  @all
end

#cache_key_prefixObject (readonly)

Returns the value of attribute cache_key_prefix.



11
12
13
# File 'lib/ar_cache/table.rb', line 11

def cache_key_prefix
  @cache_key_prefix
end

#column_indexesObject (readonly)

Returns the value of attribute column_indexes.



11
12
13
# File 'lib/ar_cache/table.rb', line 11

def column_indexes
  @column_indexes
end

#column_namesObject (readonly)

Returns the value of attribute column_names.



11
12
13
# File 'lib/ar_cache/table.rb', line 11

def column_names
  @column_names
end

#md5Object (readonly)

Returns the value of attribute md5.



11
12
13
# File 'lib/ar_cache/table.rb', line 11

def md5
  @md5
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/ar_cache/table.rb', line 11

def name
  @name
end

#primary_keyObject (readonly)

Returns the value of attribute primary_key.



11
12
13
# File 'lib/ar_cache/table.rb', line 11

def primary_key
  @primary_key
end

#unique_indexesObject (readonly)

Returns the value of attribute unique_indexes.



11
12
13
# File 'lib/ar_cache/table.rb', line 11

def unique_indexes
  @unique_indexes
end

Class Method Details

.new(table_name) ⇒ Object



19
20
21
22
23
# File 'lib/ar_cache/table.rb', line 19

def self.new(table_name)
  @lock.synchronize do
    @all.find { |table| table.name == table_name } || super
  end
end

Instance Method Details

#cache_key(where_values_hash, index, multi_values_key = nil, key_value = nil) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/ar_cache/table.rb', line 74

def cache_key(where_values_hash, index, multi_values_key = nil, key_value = nil)
  where_value = index.map do |column|
    value = column == multi_values_key ? key_value : where_values_hash[column]
    "#{column}=#{value}"
  end.sort.join('&')

  "#{cache_key_prefix}:#{version}:#{where_value}"
end

#disabled?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/ar_cache/table.rb', line 44

def disabled?
  @disabled
end

#primary_cache_key(id) ⇒ Object



70
71
72
# File 'lib/ar_cache/table.rb', line 70

def primary_cache_key(id)
  "#{cache_key_prefix}:#{version}:#{primary_key}=#{id}"
end

#select_disabled?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/ar_cache/table.rb', line 48

def select_disabled?
  @select_disabled
end

#update_versionObject



62
63
64
65
66
67
68
# File 'lib/ar_cache/table.rb', line 62

def update_version
  return -1 if disabled?

  version = ArCache::Record.update_version(self)
  ArCache::Store.write(cache_key_prefix, version)
  version
end

#versionObject



52
53
54
55
56
57
58
59
60
# File 'lib/ar_cache/table.rb', line 52

def version
  version = ArCache::Store.read(cache_key_prefix)
  unless version
    version = ArCache::Record.version(self)
    ArCache::Store.write(cache_key_prefix, version)
  end

  version
end