Navigation Menu

Skip to content

Commit

Permalink
Undefined ivars return nil by default
Browse files Browse the repository at this point in the history
When called and undefined ivar, it does not create the ivar and
set it to nil, so there should not be a performance penalty
  • Loading branch information
ayrton committed Oct 12, 2012
1 parent 36360e9 commit 1d29915
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions activesupport/lib/active_support/cache.rb
Expand Up @@ -558,23 +558,23 @@ def initialize(value, options = {})
end

def value
convert_version_3_entry! if defined?(@value)
convert_version_3_entry! if @value
compressed? ? uncompress(@v) : @v
end

# Check if the entry is expired. The +expires_in+ parameter can override
# the value set when the entry was created.
def expired?
convert_version_3_entry! if defined?(@value)
if defined?(@x)
convert_version_3_entry! if @value
if @x
@x && @x < Time.now.to_i
else
false
end
end

def expires_at
Time.at(@x) if defined?(@x)
Time.at(@x) if @x
end

def expires_at=(value)
Expand All @@ -584,7 +584,7 @@ def expires_at=(value)
# Returns the size of the cached value. This could be less than
# <tt>value.size</tt> if the data is compressed.
def size
if defined?(@s)
if @s
@s
else
case value
Expand All @@ -601,7 +601,7 @@ def size
# Duplicate the value in a class. This is used by cache implementations that don't natively
# serialize entries to protect against accidental cache modifications.
def dup_value!
convert_version_3_entry! if defined?(@value)
convert_version_3_entry! if @value
if @v && !compressed? && !(@v.is_a?(Numeric) || @v == true || @v == false)
if @v.is_a?(String)
@v = @v.dup
Expand All @@ -622,7 +622,7 @@ def should_compress?(value, options)
end

def compressed?
defined?(@c) ? @c : false
@c ? @c : false
end

def compress(value)
Expand All @@ -636,15 +636,15 @@ def uncompress(value)
# The internals of this method changed between Rails 3.x and 4.0. This method provides the glue
# to ensure that cache entries created under the old version still work with the new class definition.
def convert_version_3_entry!
if defined?(@value)
if @value
@v = @value
remove_instance_variable(:@value)
end
if defined?(@compressed)
if @compressed
@c = @compressed
remove_instance_variable(:@compressed)
end
if defined?(@expires_in) && defined?(@created_at) && @expires_in && @created_at
if @expires_in && @created_at
@x = (@created_at + @expires_in).to_i
remove_instance_variable(:@created_at)
remove_instance_variable(:@expires_in)
Expand Down

0 comments on commit 1d29915

Please sign in to comment.