Permalink
Browse files

Undefined ivars return nil by default

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...
1 parent 36360e9 commit 1d2991536fc9a54b019b7eeba215f3af1414f134 @ayrton committed Oct 12, 2012
Showing with 10 additions and 10 deletions.
  1. +10 −10 activesupport/lib/active_support/cache.rb
@@ -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)
@@ -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
@@ -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
@@ -622,7 +622,7 @@ def should_compress?(value, options)
end
def compressed?
- defined?(@c) ? @c : false
+ @c ? @c : false
end
def compress(value)
@@ -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)

0 comments on commit 1d29915

Please sign in to comment.