Class: Momento::CollectionTtl
- Inherits:
-
Object
- Object
- Momento::CollectionTtl
- 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
-
#refresh_ttl ⇒ Object
readonly
Returns the value of attribute refresh_ttl.
-
#ttl_seconds ⇒ Object
readonly
Returns the value of attribute ttl_seconds.
Class Method Summary collapse
-
.from_cache_ttl ⇒ Momento::CollectionTtl
Creates a CollectionTtl that refreshes and uses the default client TTL.
-
.of(ttl_seconds) ⇒ Momento::CollectionTtl
Creates a CollectionTtl with the given TTL.
-
.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.
Instance Method Summary collapse
-
#initialize(ttl_seconds = nil, refresh_ttl: true) ⇒ Momento::CollectionTtl
constructor
Create a CollectionTtl with optional ttl seconds and refresh.
- #to_s ⇒ Object
- #ttl_milliseconds ⇒ Object
-
#with_no_refresh_ttl_on_updates ⇒ Momento::CollectionTtl
Copy constructor that uses the parent TTL and does not refresh.
-
#with_refresh_ttl_on_updates ⇒ Momento::CollectionTtl
Copy constructor that uses the parent TTL and refreshes.
-
#with_ttl_if_absent(ttl_seconds) ⇒ Momento::CollectionTtl
Copy constructor that uses the given TTL only if the parent CollectionTtl doesn’t have one.
Constructor Details
#initialize(ttl_seconds = nil, refresh_ttl: true) ⇒ Momento::CollectionTtl
Create a CollectionTtl with optional ttl seconds and refresh.
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_ttl ⇒ Object (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_seconds ⇒ Object (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_ttl ⇒ Momento::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.
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
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_s ⇒ Object
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_milliseconds ⇒ Object
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_updates ⇒ Momento::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_updates ⇒ Momento::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.
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 |