Class: ActiveSupport::Cache::Entry

Inherits:
Object
  • Object
show all
Defined in:
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, options = {}) ⇒ Entry

Create a new cache entry for the specified value. Options supported are :compress, :compress_threshold, and :expires_in.



540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
# File 'lib/active_support/cache.rb', line 540

def initialize(value, options = {})
  @compressed = false
  @expires_in = options[:expires_in]
  @expires_in = @expires_in.to_f if @expires_in
  @created_at = Time.now.to_f
  if value
    if should_compress?(value, options)
      @value = Zlib::Deflate.deflate(Marshal.dump(value))
      @compressed = true
    else
      @value = value
    end
  else
    @value = nil
  end
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



520
521
522
# File 'lib/active_support/cache.rb', line 520

def created_at
  @created_at
end

#expires_inObject (readonly)

Returns the value of attribute expires_in.



520
521
522
# File 'lib/active_support/cache.rb', line 520

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.



528
529
530
531
532
533
534
535
# File 'lib/active_support/cache.rb', line 528

def create (raw_value, created_at, options = {})
  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, !!options[:compressed])
  entry.instance_variable_set(:@expires_in, options[:expires_in])
  entry
end

Instance Method Details

#compressed?Boolean

Returns:

  • (Boolean)


573
574
575
# File 'lib/active_support/cache.rb', line 573

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.

Returns:

  • (Boolean)


579
580
581
582
583
584
585
# File 'lib/active_support/cache.rb', line 579

def expired?
  if @expires_in && @created_at + @expires_in <= Time.now.to_f
    true
  else
    false
  end
end

#expires_atObject

Seconds since the epoch when the cache entry will expire.



597
598
599
# File 'lib/active_support/cache.rb', line 597

def expires_at
  @expires_in ? @created_at + @expires_in : nil
end

#expires_at=(time) ⇒ Object

Set a new time to live on the entry so it expires at the given time.



588
589
590
591
592
593
594
# File 'lib/active_support/cache.rb', line 588

def expires_at=(time)
  if time
    @expires_in = time.to_f - @created_at
  else
    @expires_in = nil
  end
end

#raw_valueObject

Get the raw value. This value may be serialized and compressed.



558
559
560
# File 'lib/active_support/cache.rb', line 558

def raw_value
  @value
end

#sizeObject

Get the size of the cached value. This could be less than value.size if the data is compressed.



603
604
605
606
607
608
609
610
611
# File 'lib/active_support/cache.rb', line 603

def size
  if @value.nil?
    0
  elsif @value.respond_to?(:bytesize)
    @value.bytesize
  else
    Marshal.dump(@value).bytesize
  end
end

#valueObject

Get the value stored in the cache.



563
564
565
566
567
568
569
570
571
# File 'lib/active_support/cache.rb', line 563

def value
  if @value
    val = compressed? ? Marshal.load(Zlib::Inflate.inflate(@value)) : @value
    unless val.frozen?
      val.freeze rescue nil
    end
    val
  end
end