Class: ArCache::WhereClause
- Inherits:
-
Object
- Object
- ArCache::WhereClause
- Includes:
- Raw
- Defined in:
- lib/ar_cache/where_clause.rb
Defined Under Namespace
Modules: Raw
Instance Attribute Summary collapse
-
#invalid_keys ⇒ Object
readonly
Returns the value of attribute invalid_keys.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#predicates ⇒ Object
readonly
Returns the value of attribute predicates.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
- #add_invalid_keys(key) ⇒ Object
- #add_missed_values(key) ⇒ Object
- #cache_hash ⇒ Object
- #cacheable? ⇒ Boolean
- #delete_invalid_keys ⇒ Object
- #hit_unique_index? ⇒ Boolean
-
#initialize(klass, predicates) ⇒ WhereClause
constructor
A new instance of WhereClause.
- #missed_hash ⇒ Object
- #primary_cache_keys ⇒ Object
- #primary_key_index? ⇒ Boolean
- #single? ⇒ Boolean
Methods included from Raw
Constructor Details
#initialize(klass, predicates) ⇒ WhereClause
Returns a new instance of WhereClause.
7 8 9 10 11 12 |
# File 'lib/ar_cache/where_clause.rb', line 7 def initialize(klass, predicates) @klass = klass @table = klass.ar_cache_table @predicates = predicates @missed_values = [] end |
Instance Attribute Details
#invalid_keys ⇒ Object (readonly)
Returns the value of attribute invalid_keys.
5 6 7 |
# File 'lib/ar_cache/where_clause.rb', line 5 def invalid_keys @invalid_keys end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
5 6 7 |
# File 'lib/ar_cache/where_clause.rb', line 5 def klass @klass end |
#predicates ⇒ Object (readonly)
Returns the value of attribute predicates.
5 6 7 |
# File 'lib/ar_cache/where_clause.rb', line 5 def predicates @predicates end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
5 6 7 |
# File 'lib/ar_cache/where_clause.rb', line 5 def table @table end |
Instance Method Details
#add_invalid_keys(key) ⇒ Object
87 88 89 90 91 92 |
# File 'lib/ar_cache/where_clause.rb', line 87 def add_invalid_keys(key) @invalid_keys ||= [] @invalid_keys << key @invalid_keys << cache_hash[key] unless primary_key_index? @invalid_keys end |
#add_missed_values(key) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/ar_cache/where_clause.rb', line 79 def add_missed_values(key) if primary_key_index? @missed_values << cache_hash[key] else @missed_values << @original_cache_hash[cache_hash[key]] end end |
#cache_hash ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ar_cache/where_clause.rb', line 49 def cache_hash return @cache_hash if defined?(@cache_hash) @cache_hash = {} multi_values_key = @multi_values_key || @index.first Array.wrap(where_values_hash[multi_values_key]).each do |v| @cache_hash[table.cache_key(where_values_hash, @index, multi_values_key, v)] = v end return @cache_hash if primary_key_index? @original_cache_hash = @cache_hash @cache_hash = ArCache::Store.read_multi(@cache_hash.keys) @original_cache_hash.each { |k, v| @missed_values << v unless @cache_hash.key?(k) } @cache_hash = @cache_hash.invert @cache_hash end |
#cacheable? ⇒ Boolean
14 15 16 17 18 |
# File 'lib/ar_cache/where_clause.rb', line 14 def cacheable? return @cacheable if defined?(@cacheable) @cacheable = predicates.any? && where_values_hash.length == predicates.length && hit_unique_index? end |
#delete_invalid_keys ⇒ Object
94 95 96 |
# File 'lib/ar_cache/where_clause.rb', line 94 def delete_invalid_keys ArCache::Store.delete_multi(@invalid_keys) if @invalid_keys end |
#hit_unique_index? ⇒ Boolean
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/ar_cache/where_clause.rb', line 20 def hit_unique_index? table.unique_indexes.each do |index| @index = index @multi_values_key = nil count = 0 bool = index.all? do |column| where_values_hash.key?(column).tap do if where_values_hash[column].is_a?(Array) @multi_values_key = column count += 1 end end end return true if bool && count < 2 end false end |
#missed_hash ⇒ Object
75 76 77 |
# File 'lib/ar_cache/where_clause.rb', line 75 def missed_hash @missed_hash ||= @missed_values.empty? ? {} : { (@multi_values_key || @index.first) => @missed_values } end |
#primary_cache_keys ⇒ Object
69 70 71 72 73 |
# File 'lib/ar_cache/where_clause.rb', line 69 def primary_cache_keys raise 'Does not detect primary key index' unless primary_key_index? @primary_cache_keys ||= Array(where_values_hash[table.primary_key]).map { |v| table.primary_cache_key(v) } end |
#primary_key_index? ⇒ Boolean
45 46 47 |
# File 'lib/ar_cache/where_clause.rb', line 45 def primary_key_index? (@multi_values_key || @index.first) == table.primary_key end |
#single? ⇒ Boolean
41 42 43 |
# File 'lib/ar_cache/where_clause.rb', line 41 def single? @multi_values_key.nil? end |