Skip to content

momentohq/client-sdk-ruby

Repository files navigation

logo

project status project stability

Momento Ruby Client Library

Momento Cache is a fast, simple, pay-as-you-go caching solution without any of the operational overhead required by traditional caching solutions. This repo contains the source code for the Momento Ruby client library.

To get started with Momento you will need a Momento Auth Token. You can get one from the Momento Console.

Momento Ruby SDK

To get started with Momento you will need a Momento API key. You can get one from the Momento Console.

Packages

The Momento Ruby SDK is available on RubyGems

Install the gem and add to an application's Gemfile by executing:

bundle add momento

If bundler is not being used to manage dependencies, install the gem by executing:

gem install momento

You will need Ruby 2.7 or newer.

An IDE with good Ruby support, such as RubyMine, is recommended.

Note: M1 or M2 Macs: If you're using an M1 or M2 Mac, you may have trouble installing the grpc gem; see this issue for more information.

A work around is to run bundle config build.grpc --with-ldflags="-Wl,-undefined,dynamic_lookup" before doing bundle install.

Usage

You can find this example code and more in the examples directory of this repository.

# An example of the basic functionality of
# Momento::CacheClient.

require 'momento'

# Cached items will be deleted after 12.5 seconds.
TTL_SECONDS = 12.5

# The name of the cache to create *and delete*
CACHE_NAME = ENV.fetch('MOMENTO_CACHE_NAME', 'ruby-examples')

# Create a credential provider that loads a Momento API Key from an environment variable.
credential_provider = Momento::CredentialProvider.from_env_var('MOMENTO_API_KEY')

# This is a reasonable configuration for dev work on a laptop.
configuration = Momento::Cache::Configurations::Laptop.latest
# This configuration might be better for a production where you want more aggressive timeouts
# configuration = Momento::Cache::Configurations::InRegion.latest
# To set a custom timeout, you can use the with_timeout method.
# configuration = configuration.with_timeout(10_000)
# To increase the number of TCP connections for a client where you expect a high volume of traffic,
# you can use the with_num_connections method.
# configuration = configuration.with_num_connections(4)

# Instantiate a Momento client.
client = Momento::CacheClient.new(
  configuration: configuration,
  credential_provider: credential_provider,
  default_ttl: TTL_SECONDS
)

# Create a cache to play with.
response = client.create_cache(CACHE_NAME)
if response.success?
  puts "Created the cache."
elsif response.already_exists?
  puts "Cache already exists."
elsif response.error?
  raise "Couldn't create a cache: #{response.error}"
end

# List our caches.
response = client.list_caches
if response.success?
  puts "Caches: #{response.cache_names&.join(", ")}"
elsif response.error?
  raise "Couldn't list the caches: #{response.error}"
end

# Put an item in the cache.
response = client.set(CACHE_NAME, "key", "You cached something!")
if response.success?
  puts "Set an item in the cache."
elsif response.error?
  raise "Couldn't set an item in the cache: #{response.error}"
end

# And get it back.
response = client.get(CACHE_NAME, "key")
if response.hit?
  puts "Cache returned: #{response.value_string}"
elsif response.miss?
  puts "The item wasn't found in the cache."
elsif response.error?
  raise "Couldn't get an item from the cache: #{response.error}"
end

# Now delete it.
response = client.delete(CACHE_NAME, "key")
if response.success?
  puts "Key/value deleted."
elsif response.error?
  raise "Couldn't delete an item from the cache: #{response.error}"
end

# And delete our test cache.
response = client.delete_cache(CACHE_NAME)
if response.success?
  puts "Deleted the cache."
elsif response.error?
  raise "Couldn't create a cache: #{response.error}"
end

Getting Started and Documentation

General documentation on Momento and the Momento SDKs is available on the Momento Docs website. Specific usage examples for the Ruby SDK are coming soon!

Examples

Check out full working code in the example directory of this repository!

Error Handling

Momento::CacheClient follows the philosophy that when working with a service, exceptions are bugs. Minor outages are a fact of life; they are normal rather than exceptional.

When there is a problem, Momento::CacheClient methods return an error response, the same as any other response. This makes errors more visible, allows your IDE to be more helpful in ensuring that you've handled the responses you care about.

Check if a response is an error with response.error?, get the error with response.error, and it can be raised as an exception with raise response.error. Generally, printing response.error tell you what you need to know, but you might want more details. Here's a contrived example.

# This is an invalid cache name. They must be ASCII-only.
cache_name = 'çåché nåme'
response = client.create_cache(cache_name)
if response.success?
  puts "Created the cache"
elsif response.error?
  error = response.error
  puts "Creating the cache failed: #{error}"
  case error
  when Momento::Error::LimitExceededError
    puts "We'll have to slow down"
  when Momento::Error::PermissionError
    puts "We'll have to fix our auth token"
  when Momento::Error::InvalidArgumentError
    puts "We can't make a cache named #{cache_name}"
  end
end

Momento::CacheClient will raise exceptions for programmer mistakes such as passing the wrong type, typically an ArgumentError or TypeError. The exceptions are documented for each method.

See Momento::Response for more about working with with error responses, and Momento::Error for more about using errors.

Issues

Please report any bugs, issues, requests, and feedback via this repo's issue tracker.

Developing

If you are interested in contributing to the SDK, please see the CONTRIBUTING docs.


For more info, visit our website at https://gomomento.com!