Class: ActiveSupport::Cache::Entry
- Defined in:
- activesupport/lib/active_support/cache.rb
Overview
This class is used to represent cache entries. Cache entries have a value and an optional expiration time. The expiration time is used to support the :race_condition_ttl option on the cache.
Since cache entries in most instances will be serialized, the internals of this class are highly optimized using short instance variable names that are lazily defined.
Constant Summary collapse
- DEFAULT_COMPRESS_LIMIT =
16.kilobytes
Instance Method Summary collapse
-
#dup_value! ⇒ Object
Duplicate the value in a class.
-
#expired? ⇒ Boolean
Check if the entry is expired.
- #expires_at ⇒ Object
- #expires_at=(value) ⇒ Object
-
#initialize(value, options = {}) ⇒ Entry
constructor
Create a new cache entry for the specified value.
-
#size ⇒ Object
Returns the size of the cached value.
- #value ⇒ Object
Constructor Details
#initialize(value, options = {}) ⇒ Entry
Create a new cache entry for the specified value. Options supported are :compress
, :compress_threshold
, and :expires_in
.
606 607 608 609 610 611 612 613 614 615 616 617 |
# File 'activesupport/lib/active_support/cache.rb', line 606 def initialize(value, = {}) if should_compress?(value, ) @value = compress(value) @compressed = true else @value = value end @created_at = Time.now.to_f @expires_in = [:expires_in] @expires_in = @expires_in.to_f if @expires_in end |
Instance Method Details
#dup_value! ⇒ Object
Duplicate the value in a class. This is used by cache implementations that don’t natively serialize entries to protect against accidental cache modifications.
662 663 664 665 666 667 668 669 670 671 672 |
# File 'activesupport/lib/active_support/cache.rb', line 662 def dup_value! convert_version_4beta1_entry! if defined?(@v) if @value && !compressed? && !(@value.is_a?(Numeric) || @value == true || @value == false) if @value.is_a?(String) @value = @value.dup else @value = Marshal.load(Marshal.dump(@value)) end end end |
#expired? ⇒ Boolean
Check if the entry is expired. The expires_in
parameter can override the value set when the entry was created.
626 627 628 629 |
# File 'activesupport/lib/active_support/cache.rb', line 626 def expired? convert_version_4beta1_entry! if defined?(@value) @expires_in && @created_at + @expires_in <= Time.now.to_f end |
#expires_at ⇒ Object
631 632 633 |
# File 'activesupport/lib/active_support/cache.rb', line 631 def expires_at @expires_in ? @created_at + @expires_in : nil end |
#expires_at=(value) ⇒ Object
635 636 637 638 639 640 641 |
# File 'activesupport/lib/active_support/cache.rb', line 635 def expires_at=(value) if value @expires_in = value.to_f - @created_at else @expires_in = nil end end |
#size ⇒ Object
Returns the size of the cached value. This could be less than value.size
if the data is compressed.
645 646 647 648 649 650 651 652 653 654 655 656 657 658 |
# File 'activesupport/lib/active_support/cache.rb', line 645 def size if defined?(@s) @s else case value when NilClass 0 when String @value.bytesize else @s = Marshal.dump(@value).bytesize end end end |
#value ⇒ Object
619 620 621 622 |
# File 'activesupport/lib/active_support/cache.rb', line 619 def value convert_version_4beta1_entry! if defined?(@v) compressed? ? uncompress(@value) : @value end |