Momento Ruby Client Library
:warning: Alpha SDK :warning:
This is an official Momento SDK, but the API is in an alpha stage and may be subject to backward-incompatible changes. For more info, click on the alpha badge above.
Ruby client SDK for Momento Serverless Cache: a fast, simple, pay-as-you-go caching solution without any of the operational overhead required by traditional caching solutions!
Getting Started :running:
Requirements
- A Momento Auth Token is required, you can generate one using the Momento CLI.
- Ruby 2.7 or newer.
An IDE with good Ruby support, such as RubyMine, is recommended.
Examples
You can find this example code and more in the examples directory of this repository.
Installation
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
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
# An example of the basic functionality of
# Momento::SimpleCacheClient.
require 'momento'
# Get your Momento token from an environment variable.
TOKEN = ENV.fetch('MOMENTO_AUTH_TOKEN')
# 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')
# Instantiate a Momento client.
client = Momento::SimpleCacheClient.new(
auth_token: TOKEN,
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.
puts "Caches: #{client.caches.to_a.join(", ")}"
# 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
Error Handling
Momento::SimpleCacheClient 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::SimpleCacheClient 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::SimpleCacheClient 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.
Tuning
Coming soon.
Issues
Please report any bugs, issues, requests, and feedback via this repo's issue tracker.
Contributing
See our instructions for CONTRIBUTING.
For more info, visit our website at https://gomomento.com!