Class: Momento::CollectionTtl

Inherits:
Object
  • Object
show all
Defined in:
lib/momento/collection_ttl.rb

Overview

Represents the desired behavior for managing the TTL on collection objects.

For cache operations that modify a collection (dictionaries, lists, or sets), there are a few things to consider. The first time the collection is created, we need to set a TTL on it. For subsequent operations that modify the collection you may choose to update the TTL in order to prolong the life of the cached collection object, or you may choose to leave the TTL unmodified in order to ensure that the collection expires at the original TTL.

The default behaviour is to refresh the TTL (to prolong the life of the collection) each time it is written using the client’s default item TTL.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ttl_seconds = nil, refresh_ttl: true) ⇒ Momento::CollectionTtl

Create a CollectionTtl with optional ttl seconds and refresh.

Parameters:

  • ttl_seconds (Integer | nil) (defaults to: nil)

    the time to live of the collection. Uses the client default TTL if nil.

  • refresh_ttl (Boolean) (defaults to: true)

    whether to refresh the collection’s ttl when performing a cache operation.



20
21
22
23
24
# File 'lib/momento/collection_ttl.rb', line 20

def initialize(ttl_seconds = nil, refresh_ttl: true)
  validate_ttl_seconds(ttl_seconds) unless ttl_seconds.nil?
  @ttl_seconds = ttl_seconds
  @refresh_ttl = refresh_ttl
end

Instance Attribute Details

#refresh_ttlObject (readonly)

Returns the value of attribute refresh_ttl.



14
15
16
# File 'lib/momento/collection_ttl.rb', line 14

def refresh_ttl
  @refresh_ttl
end

#ttl_secondsObject (readonly)

Returns the value of attribute ttl_seconds.



14
15
16
# File 'lib/momento/collection_ttl.rb', line 14

def ttl_seconds
  @ttl_seconds
end

Class Method Details

.from_cache_ttlMomento::CollectionTtl

Creates a CollectionTtl that refreshes and uses the default client TTL.



32
33
34
# File 'lib/momento/collection_ttl.rb', line 32

def self.from_cache_ttl
  new
end

.of(ttl_seconds) ⇒ Momento::CollectionTtl

Creates a CollectionTtl with the given TTL.

Parameters:

  • ttl_seconds (Integer | nil)

    the time to live of the collection. Uses the client default TTL if nil.

Returns:



39
40
41
# File 'lib/momento/collection_ttl.rb', line 39

def self.of(ttl_seconds)
  new(ttl_seconds)
end

.refresh_ttl_if_provided(ttl_seconds = nil) ⇒ Momento::CollectionTtl

Creates a CollectionTtl that sets refresh to true if ttl_seconds is provided and false otherwise

Parameters:

  • ttl_seconds (Integer | nil) (defaults to: nil)

    the time to live of the collection. If not nil, refresh is set to true.

Returns:



46
47
48
# File 'lib/momento/collection_ttl.rb', line 46

def self.refresh_ttl_if_provided(ttl_seconds = nil)
  new(ttl_seconds, refresh_ttl: !ttl_seconds.nil?)
end

Instance Method Details

#to_sObject



69
70
71
# File 'lib/momento/collection_ttl.rb', line 69

def to_s
  "ttl: #{@ttl_seconds || 'null'}, refreshTtl: #{@refresh_ttl ? 'true' : 'false'}"
end

#ttl_millisecondsObject



26
27
28
# File 'lib/momento/collection_ttl.rb', line 26

def ttl_milliseconds
  @ttl_seconds.nil? ? nil : @ttl_seconds * 1000
end

#with_no_refresh_ttl_on_updatesMomento::CollectionTtl

Copy constructor that uses the parent TTL and does not refresh.



65
66
67
# File 'lib/momento/collection_ttl.rb', line 65

def with_no_refresh_ttl_on_updates
  self.class.new(@ttl_seconds, refresh_ttl: false)
end

#with_refresh_ttl_on_updatesMomento::CollectionTtl

Copy constructor that uses the parent TTL and refreshes.



59
60
61
# File 'lib/momento/collection_ttl.rb', line 59

def with_refresh_ttl_on_updates
  self.class.new(@ttl_seconds)
end

#with_ttl_if_absent(ttl_seconds) ⇒ Momento::CollectionTtl

Copy constructor that uses the given TTL only if the parent CollectionTtl doesn’t have one.

Parameters:

  • ttl_seconds (Integer | nil)

    the time to live of the collection. Will be ignored if the parent has a TTL.

Returns:



53
54
55
# File 'lib/momento/collection_ttl.rb', line 53

def with_ttl_if_absent(ttl_seconds)
  self.class.new(@ttl_seconds || ttl_seconds, refresh_ttl: @refresh_ttl)
end