Class: ActiveSupport::Cache::Entry
- Defined in:
- activesupport/lib/active_support/cache.rb
Overview
Entry that is put into caches. It supports expiration time on entries and can compress values to save space in the cache.
Constant Summary collapse
- DEFAULT_COMPRESS_LIMIT =
16.kilobytes
Instance Attribute Summary collapse
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#expires_in ⇒ Object
readonly
Returns the value of attribute expires_in.
Class Method Summary collapse
-
.create(raw_value, created_at, options = {}) ⇒ Object
Create an entry with internal attributes set.
Instance Method Summary collapse
- #compressed? ⇒ Boolean
-
#expired? ⇒ Boolean
Check if the entry is expired.
-
#expires_at ⇒ Object
Seconds since the epoch when the entry will expire.
-
#expires_at=(time) ⇒ Object
Set a new time when the entry will expire.
-
#initialize(value, options = {}) ⇒ Entry
constructor
Create a new cache entry for the specified value.
-
#raw_value ⇒ Object
Get the raw value.
-
#size ⇒ Object
Returns the size of the cached value.
-
#value ⇒ Object
Get the value stored in the cache.
Constructor Details
#initialize(value, options = {}) ⇒ Entry
Create a new cache entry for the specified value. Options supported are :compress
, :compress_threshold
, and :expires_in
.
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 |
# File 'activesupport/lib/active_support/cache.rb', line 553 def initialize(value, = {}) @compressed = false @expires_in = [:expires_in] @expires_in = @expires_in.to_f if @expires_in @created_at = Time.now.to_f if value.nil? @value = nil else @value = Marshal.dump(value) if should_compress?(@value, ) @value = Zlib::Deflate.deflate(@value) @compressed = true end end end |
Instance Attribute Details
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at
533 534 535 |
# File 'activesupport/lib/active_support/cache.rb', line 533 def created_at @created_at end |
#expires_in ⇒ Object (readonly)
Returns the value of attribute expires_in
533 534 535 |
# File 'activesupport/lib/active_support/cache.rb', line 533 def expires_in @expires_in end |
Class Method Details
.create(raw_value, created_at, options = {}) ⇒ Object
Create an entry with internal attributes set. This method is intended to be used by implementations that store cache entries in a native format instead of as serialized Ruby objects.
541 542 543 544 545 546 547 548 |
# File 'activesupport/lib/active_support/cache.rb', line 541 def create(raw_value, created_at, = {}) entry = new(nil) entry.instance_variable_set(:@value, raw_value) entry.instance_variable_set(:@created_at, created_at.to_f) entry.instance_variable_set(:@compressed, [:compressed]) entry.instance_variable_set(:@expires_in, [:expires_in]) entry end |
Instance Method Details
#compressed? ⇒ Boolean
594 595 596 |
# File 'activesupport/lib/active_support/cache.rb', line 594 def compressed? @compressed end |
#expired? ⇒ Boolean
Check if the entry is expired. The expires_in
parameter can override the value set when the entry was created.
600 601 602 |
# File 'activesupport/lib/active_support/cache.rb', line 600 def expired? @expires_in && @created_at + @expires_in <= Time.now.to_f end |
#expires_at ⇒ Object
Seconds since the epoch when the entry will expire.
614 615 616 |
# File 'activesupport/lib/active_support/cache.rb', line 614 def expires_at @expires_in ? @created_at + @expires_in : nil end |
#expires_at=(time) ⇒ Object
Set a new time when the entry will expire.
605 606 607 608 609 610 611 |
# File 'activesupport/lib/active_support/cache.rb', line 605 def expires_at=(time) if time @expires_in = time.to_f - @created_at else @expires_in = nil end end |
#raw_value ⇒ Object
Get the raw value. This value may be serialized and compressed.
570 571 572 |
# File 'activesupport/lib/active_support/cache.rb', line 570 def raw_value @value end |
#size ⇒ Object
Returns the size of the cached value. This could be less than value.size if the data is compressed.
620 621 622 623 624 625 626 |
# File 'activesupport/lib/active_support/cache.rb', line 620 def size if @value.nil? 0 else @value.bytesize end end |
#value ⇒ Object
Get the value stored in the cache.
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 |
# File 'activesupport/lib/active_support/cache.rb', line 575 def value # If the original value was exactly false @value is still true because # it is marshalled and eventually compressed. Both operations yield # strings. if @value # In rails 3.1 and earlier values in entries did not marshaled without # options[:compress] and if it's Numeric. # But after commit a263f377978fc07515b42808ebc1f7894fafaa3a # all values in entries are marshalled. And after that code below expects # that all values in entries will be marshaled (and will be strings). # So here we need a check for old ones. begin Marshal.load(compressed? ? Zlib::Inflate.inflate(@value) : @value) rescue TypeError compressed? ? Zlib::Inflate.inflate(@value) : @value end end end |