Skip to content
Browse files

Cache unserialized attributes

  • Loading branch information...
1 parent cc81b04 commit 4b7b8d9e19b662d8a1135fec73b422202f97472a @jeremy jeremy committed
View
14 activerecord/lib/active_record/attribute_methods/read.rb
@@ -26,8 +26,7 @@ def cache_attributes(*attribute_names)
# Returns the attributes which are cached. By default time related columns
# with datatype <tt>:datetime, :timestamp, :time, :date</tt> are cached.
def cached_attributes
- @cached_attributes ||=
- columns.select{|c| attribute_types_cached_by_default.include?(c.type)}.map{|col| col.name}.to_set
+ @cached_attributes ||= columns.select { |c| cacheable_column?(c) }.map { |col| col.name }.to_set
end
# Returns +true+ if the provided attribute is being cached.
@@ -37,7 +36,7 @@ def cache_attribute?(attr_name)
protected
def define_method_attribute(attr_name)
- if self.serialized_attributes[attr_name]
+ if serialized_attributes.include?(attr_name)
define_read_method_for_serialized_attribute(attr_name)
else
define_read_method(attr_name.to_sym, attr_name, columns_hash[attr_name])
@@ -49,9 +48,14 @@ def define_method_attribute(attr_name)
end
private
+ def cacheable_column?(column)
+ serialized_attributes.include?(column.name) || attribute_types_cached_by_default.include?(column.type)
+ end
+
# Define read method for serialized attribute.
def define_read_method_for_serialized_attribute(attr_name)
- generated_attribute_methods.module_eval("def #{attr_name}; unserialize_attribute('#{attr_name}'); end", __FILE__, __LINE__)
+ access_code = "@attributes_cache['#{attr_name}'] ||= unserialize_attribute('#{attr_name}')"
+ generated_attribute_methods.module_eval("def #{attr_name}; #{access_code}; end", __FILE__, __LINE__)
end
# Define an attribute reader method. Cope with nil column.
@@ -92,7 +96,7 @@ def read_attribute(attr_name)
# Returns true if the attribute is of a text column and marked for serialization.
def unserializable_attribute?(attr_name, column)
- column.text? && self.class.serialized_attributes[attr_name]
+ column.text? && self.class.serialized_attributes.include?(attr_name)
end
# Returns the unserialized object of the attribute.
View
11 activerecord/lib/active_record/base.rb
@@ -415,6 +415,11 @@ class Base
class_inheritable_accessor :default_scoping, :instance_writer => false
self.default_scoping = []
+ # Returns a hash of all the attributes that have been specified for serialization as
+ # keys and their class restriction as values.
+ class_attribute :serialized_attributes
+ self.serialized_attributes = {}
+
class << self # Class methods
delegate :find, :first, :last, :all, :destroy, :destroy_all, :exists?, :delete, :delete_all, :update, :update_all, :to => :scoped
delegate :find_each, :find_in_batches, :to => :scoped
@@ -526,12 +531,6 @@ def serialize(attr_name, class_name = Object)
serialized_attributes[attr_name.to_s] = class_name
end
- # Returns a hash of all the attributes that have been specified for serialization as
- # keys and their class restriction as values.
- def serialized_attributes
- read_inheritable_attribute(:attr_serialized) or write_inheritable_attribute(:attr_serialized, {})
- end
-
# Guesses the table name (in forced lower-case) based on the name of the class in the
# inheritance hierarchy descending directly from ActiveRecord::Base. So if the hierarchy
# looks like: Reply < Message < ActiveRecord::Base, then Message is used
View
21 activerecord/test/cases/attribute_methods_test.rb
@@ -419,12 +419,8 @@ def test_declaring_attributes_as_cached_adds_them_to_the_attributes_cached_by_de
Topic.instance_variable_set "@cached_attributes", nil
end
- def test_time_related_columns_are_actually_cached
- column_types = %w(datetime timestamp time date).map(&:to_sym)
- column_names = Topic.columns.select{|c| column_types.include?(c.type) }.map(&:name)
-
- assert_equal column_names.sort, Topic.cached_attributes.sort
- assert_equal time_related_columns_on_topic.sort, Topic.cached_attributes.sort
+ def test_cacheable_columns_are_actually_cached
+ assert_equal cached_columns.sort, Topic.cached_attributes.sort
end
def test_accessing_cached_attributes_caches_the_converted_values_and_nothing_else
@@ -435,8 +431,7 @@ def test_accessing_cached_attributes_caches_the_converted_values_and_nothing_els
assert cache.empty?
all_columns = Topic.columns.map(&:name)
- cached_columns = time_related_columns_on_topic
- uncached_columns = all_columns - cached_columns
+ uncached_columns = all_columns - cached_columns
all_columns.each do |attr_name|
attribute_gets_cached = Topic.cache_attribute?(attr_name)
@@ -594,8 +589,16 @@ def title; "private!"; end
private
+ def cached_columns
+ @cached_columns ||= (time_related_columns_on_topic + serialized_columns_on_topic).map(&:name)
+ end
+
def time_related_columns_on_topic
- Topic.columns.select{|c| [:time, :date, :datetime, :timestamp].include?(c.type)}.map(&:name)
+ Topic.columns.select { |c| [:time, :date, :datetime, :timestamp].include?(c.type) }
+ end
+
+ def serialized_columns_on_topic
+ Topic.columns.select { |c| Topic.serialized_attributes.include?(c.name) }
end
def in_time_zone(zone)

0 comments on commit 4b7b8d9

Please sign in to comment.
Something went wrong with that request. Please try again.