Class: Momento::SimpleCacheClient

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

Examples:

token = ...your Momento JWT...
client = Momento::SimpleCacheClient.new(
  auth_token: token,
  # cached items will be deleted after 100 seconds
  default_ttl: 100
)

response = client.create_cache("my_cache")
if response.success?
  puts "my_cache was created"
elsif response.already_exists?
  puts "my_cache already exists"
elsif response.error?
  raise response.error
end

# set will only return success or error,
# we only need to check for error
response = client.set("my_cache", "key", "value")
raise response.error if response.error?

response = client.get("my_cache", "key")
if response.hit?
  puts "We got #{response.value_string}"
elsif response.miss?
  puts "It's not in the cache"
elsif response.error?
  raise response.error
end

See Also:

Constant Summary collapse

VERSION =

This gem's version.

Momento::VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth_token:, default_ttl:) ⇒ SimpleCacheClient

Returns a new instance of SimpleCacheClient.

Parameters:

  • auth_token (String)

    the JWT for your Momento account

  • default_ttl (Numeric)

    time-to-live, in seconds

Raises:

  • (ArgumentError)

    if the default_ttl or auth_token is invalid


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_ttlNumeric

Returns how long items should remain in the cache, in seconds.

Returns:

  • (Numeric)

    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

#cachesEnumerator::Lazy<String>

Note:

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.

Examples:

cache_names = client.caches
cache_names.each { |name| puts name }

Returns:

  • (Enumerator::Lazy<String>)

    the cache names

Raises:


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.

Examples:

response = client.create_cache("my_cache")
if response.success?
  puts "my_cache was created"
elsif response.already_exists?
  puts "my_cache already exists"
elsif response.error?
  raise response.error
end

Parameters:

  • cache_name (String)

    the name of the cache to create.

Returns:

Raises:

  • (TypeError)

    when the cache_name is not a String

See Also:


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.

Examples:

response = client.delete("my_cache", "key")
raise response.error if response.error?

Parameters:

  • cache_name (String)
  • key (String)

    must only contain ASCII characters

Returns:

Raises:

  • (TypeError)

    when the cache_name or key is not a String

See Also:


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.

Examples:

response = client.delete_cache("my_cache")
raise response.error if response.error?

Parameters:

  • cache_name (String)

    the name of the cache to delete.

Returns:

Raises:

  • (TypeError)

    when the cache_name is not a String

See Also:


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.

Examples:

response = client.get("my_cache", "key")
if response.hit?
  puts "We got #{response.value_string}"
elsif response.miss?
  puts "It's not in the cache"
elsif response.error?
  raise response.error
end

Parameters:

  • cache_name (String)
  • key (String)

    must only contain ASCII characters

Returns:

Raises:

  • (TypeError)

    when the cache_name or key is not a String

See Also:


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

Note:

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.

Parameters:

  • next_token (String, nil) (defaults to: "")

    the token of the page to request

Returns:

See Also:


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.

Examples:

response = client.set("my_cache", "key", "value")
raise response.error if response.error?

Parameters:

  • cache_name (String)
  • key (String)

    must only contain ASCII characters

  • value (String)

    the value to cache

  • ttl (Numeric) (defaults to: default_ttl)

    time-to-live, in seconds.

Returns:

Raises:

  • (ArgumentError)

    if the ttl is invalid

  • (TypeError)

    when the cache_name, key, or value is not a String

See Also:


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