Class: Momento::SimpleCacheClient
- Inherits:
-
Object
- Object
- Momento::SimpleCacheClient
- Defined in:
- lib/momento/simple_cache_client.rb
Overview
A simple client for Momento.
SimpleCacheClient does not use exceptions to report errors. Instead it returns an error response. Please see README.
Constant Summary collapse
Instance Attribute Summary collapse
-
#default_ttl ⇒ Numeric
How long items should remain in the cache, in seconds.
Instance Method Summary collapse
-
#caches ⇒ Enumerator::Lazy<String>
Lists the names of all your caches, as a lazy Enumerator.
-
#create_cache(cache_name) ⇒ Momento::CreateCacheResponse
Create a new Momento cache.
-
#delete(cache_name, key) ⇒ Momento::DeleteResponse
Delete a key in a cache.
-
#delete_cache(cache_name) ⇒ Momento::DeleteCacheResponse
Delete an existing Momento cache.
-
#get(cache_name, key) ⇒ Momento::GetResponse
Get a value in a cache.
-
#initialize(auth_token:, default_ttl:) ⇒ SimpleCacheClient
constructor
A new instance of SimpleCacheClient.
-
#list_caches(next_token: "") ⇒ Momento::ListCachesResponse
List a page of your caches.
-
#set(cache_name, key, value, ttl: default_ttl) ⇒ Momento::SetResponse
Set a value in a cache.
Constructor Details
#initialize(auth_token:, default_ttl:) ⇒ SimpleCacheClient
Returns a new instance of SimpleCacheClient.
61 62 63 64 65 |
# File 'lib/momento/simple_cache_client.rb', line 61 def initialize(auth_token:, default_ttl:) @auth_token = auth_token @default_ttl = Momento::Ttl.to_ttl(default_ttl) load_endpoints_from_token end |
Instance Attribute Details
#default_ttl ⇒ Numeric
Returns how long items should remain in the cache, in seconds.
56 57 58 |
# File 'lib/momento/simple_cache_client.rb', line 56 def default_ttl @default_ttl end |
Instance Method Details
#caches ⇒ Enumerator::Lazy<String>
Unlike other methods, this will raise if there is a problem with the client or service.
Lists the names of all your caches, as a lazy Enumerator.
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/momento/simple_cache_client.rb', line 247 def caches Enumerator.new do |yielder| next_token = "" loop do response = list_caches(next_token: next_token) raise response.error if response.is_a? Momento::Response::Error response.cache_names.each do |name| yielder << name end break if response.next_token == '' next_token = response.next_token end end.lazy end |
#create_cache(cache_name) ⇒ Momento::CreateCacheResponse
Create a new Momento cache.
177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/momento/simple_cache_client.rb', line 177 def create_cache(cache_name) builder = CreateCacheResponseBuilder.new( context: { cache_name: cache_name } ) return builder.from_block do validate_cache_name(cache_name) control_stub.create_cache( ControlClient::CreateCacheRequest.new(cache_name: cache_name) ) end end |
#delete(cache_name, key) ⇒ Momento::DeleteResponse
Delete a key in a cache.
If the key does not exist, delete will still succeed.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/momento/simple_cache_client.rb', line 147 def delete(cache_name, key) builder = DeleteResponseBuilder.new( context: { cache_name: cache_name, key: key } ) return builder.from_block do validate_cache_name(cache_name) cache_stub.delete( CacheClient::DeleteRequest.new(cache_key: to_bytes(key)), metadata: { cache: cache_name } ) end end |
#delete_cache(cache_name) ⇒ Momento::DeleteCacheResponse
Delete an existing Momento cache.
201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/momento/simple_cache_client.rb', line 201 def delete_cache(cache_name) builder = DeleteCacheResponseBuilder.new( context: { cache_name: cache_name } ) return builder.from_block do validate_cache_name(cache_name) control_stub.delete_cache( ControlClient::DeleteCacheRequest.new(cache_name: cache_name) ) end end |
#get(cache_name, key) ⇒ Momento::GetResponse
Get a value in a cache.
The value can be retrieved as either bytes or a string.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/momento/simple_cache_client.rb', line 85 def get(cache_name, key) builder = GetResponseBuilder.new( context: { cache_name: cache_name, key: key } ) return builder.from_block do validate_cache_name(cache_name) cache_stub.get( CacheClient::GetRequest.new(cache_key: to_bytes(key)), metadata: { cache: cache_name } ) end end |
#list_caches(next_token: "") ⇒ Momento::ListCachesResponse
Consider using `caches` instead.
List a page of your caches.
This is a low-level method. You probably want to use #caches instead.
The next_token indicates which page to fetch. If nil or “” it will fetch the first page. Default is to fetch the first page.
227 228 229 230 231 232 233 234 235 236 |
# File 'lib/momento/simple_cache_client.rb', line 227 def list_caches(next_token: "") builder = ListCachesResponseBuilder.new( context: { next_token: next_token } ) return builder.from_block do control_stub.list_caches( ControlClient::ListCachesRequest.new(next_token: next_token) ) end end |
#set(cache_name, key, value, ttl: default_ttl) ⇒ Momento::SetResponse
Set a value in a cache.
If ttl is not set, it will use the default_ttl.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/momento/simple_cache_client.rb', line 115 def set(cache_name, key, value, ttl: default_ttl) ttl = Momento::Ttl.to_ttl(ttl) builder = SetResponseBuilder.new( context: { cache_name: cache_name, key: key, value: value, ttl: ttl } ) return builder.from_block do validate_cache_name(cache_name) req = CacheClient::SetRequest.new( cache_key: to_bytes(key), cache_body: to_bytes(value), ttl_milliseconds: ttl.milliseconds ) cache_stub.set(req, metadata: { cache: cache_name }) end end |