Skip to main content

Getting Started with Momento Cache in Swift

If you need to get going quickly with Swift and Momento Cache, this page contains the basic API calls you'll need. Check the Swift SDK examples for complete, working code samples.

Install the Momento SDK

The latest version of the Momento Swift SDK is available on GitHub: momentohq/client-sdk-swift.

Get your Momento API key

You'll need a Momento API key. You can get one from the Momento Web Console. Once you have your token, store it in an environment variable so that the Momento client can consume it:

export MOMENTO_API_KEY=<your api key here>

Note: it is best practice to put the API key into something like AWS Secret Manager or GCP Secret Manager instead of an environment variable for enhanced security, but we are using one here for demo purposes.

Set up a CacheClient

This code creates the CacheClient that you will use to call the other methods.

do {
let credentialProvider = try CredentialProvider.fromEnvironmentVariable(envVariableName: "MOMENTO_API_KEY")
let cacheClient = CacheClient(
configuration: CacheClientConfigurations.iOS.latest(),
credentialProvider: credentialProvider,
defaultTtlSeconds: 10
)
} catch {
print("Unable to create cache client: \(error)")
exit(1)
}

Create a new cache in Momento Cache

Use this function to create a new cache in your account.

let result = await cacheClient.createCache(cacheName: cacheName)
switch result {
case .alreadyExists(_):
print("Cache already exists!")
case .success(_):
print("Successfully created the cache!")
case .error(let err):
print("Unable to create the cache: \(err)")
exit(1)
}

List the names of existing caches in your account

A simple list of the names of caches for the account.

let result = await cacheClient.listCaches()
switch result {
case .success(let success):
print("Successfully fetched list of caches: \(success.caches.map { $0.name })")
case .error(let err):
print("Unable to fetch list of caches: \(err)")
exit(1)
}

Delete a cache

Use this function to delete a cache from your account.

let result = await cacheClient.deleteCache(cacheName: cacheName)
switch result {
case .success(let success):
print("Successfully deleted the cache")
case .error(let err):
print("Unable to delete cache: \(err)")
exit(1)
}

Write an item to a cache

A simple example of doing a set operation. In the CacheClient.set() call, the TTL is optional. If you did pass it in, this would override the default TTL value set with the client connection object.

let result = await cacheClient.set(
cacheName: cacheName,
key: "key",
value: "value"
)
switch result {
case .success(_):
print("Successfully set item in the cache")
case .error(let err):
print("Unable to set item in the cache: \(err)")
exit(1)
}

Read an item from a cache

This is an example of a simple read operation to get an item from a cache.

let result = await cacheClient.get(
cacheName: cacheName,
key: "key"
)
switch result {
case .hit(let hit):
print("Cache hit: \(hit.valueString)")
case .miss(_):
print("Cache miss")
case .error(let err):
print("Unable to get item in the cache: \(err)")
exit(1)
}

Delete an item from a cache

This is an example of deleting an item from a cache.

let result = await cacheClient.delete(
cacheName: cacheName,
key: "key"
)
switch result {
case .success(_):
print("Successfully deleted item in the cache")
case .error(let err):
print("Unable to delete item in the cache: \(err)")
exit(1)
}

Running the code

You can find complete, working examples in the Swift SDK GitHub repo examples directory.

info

Beyond these basic API calls check out the API reference page for more information on the full assortment of Momento API calls.