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 client library for Swift.
To get started with Momento you will need a Momento Auth Token. You can get one from the Momento Console.
- Website: https://www.gomomento.com/
- Momento Documentation: https://docs.momentohq.com/
- Getting Started: https://docs.momentohq.com/getting-started
- Momento SDK Documentation for Swift: https://docs.momentohq.com/sdks/swift
- Discuss: Momento Discord
To get started with Momento you will need a Momento API key. You can get one from the Momento Console.
- Website: https://www.gomomento.com/
- Momento Documentation: https://docs.momentohq.com/
- Getting Started: https://docs.momentohq.com/getting-started
- Discuss: Momento Discord
The Momento Swift SDK is available here on github: momentohq/client-sdk-swift.
Edit your Package.swift
file to include the SDK in your project:
// swift-tools-version: 5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "momento-example",
platforms: [.macOS(.v10_15), .iOS(.v13)],
products: [
.executable(
name: "momento-example",
targets: ["momento-example"]
),
],
dependencies: [
.package(url: "https://github.com/momentohq/client-sdk-swift", exact: "0.4.0")
],
targets: [
.executableTarget(
name: "momento-example",
dependencies: [
.product(name: "Momento", package: "client-sdk-swift"),
],
path: "Sources"
),
]
)
Check out our topics example directory for a complete example of using the Momento Swift SDK to implement a publish and subscribe system and our cache example directory for an example of using the cache client.
Here is a quickstart you can use for your own project:
import Momento
import Foundation
func main() async {
print("Running Momento Cache example!")
let cacheName = "example-cache"
var creds: CredentialProviderProtocol
do {
creds = try CredentialProvider.fromEnvironmentVariable(envVariableName: "MOMENTO_API_KEY")
} catch {
print("Error establishing credential provider: \(error)")
exit(1)
}
let cacheClient = CacheClient(
configuration: CacheClientConfigurations.iOS.latest(),
credentialProvider: creds,
defaultTtlSeconds: 10
)
let getResult = await cacheClient.get(
cacheName: cacheName,
key: "key"
)
switch getResult {
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)
}
}
await main()
General documentation on Momento and the Momento SDKs is available on the Momento Docs website. Specific usage examples for the Swift SDK can be found in the cache and topics cheat sheets!
Check out full working code in the Examples directory of this repository!
We are using swift-log to create internal Loggers for producing Momento-related logs.
The default logging backend provided by swift-log (StreamLogHandler
) simply prints to stdout at a default logging level of .info
.
To change the logging level and/or redirect logs to stderr, you would call LoggingSystem.bootstrap(...)
once at the beginning of your program like so:
LoggingSystem.bootstrap { _ in
var handler = StreamLogHandler.standardError(label: "momento-logger")
handler.logLevel = .debug
return handler
}
You can also use the LoggingSystem.bootstrap
call to configure your preferred swift-log compatible logging backend or to use your custom logging backend implementation.
If you are interested in contributing to the SDK, please see the CONTRIBUTING docs.
For more info, visit our website at https://gomomento.com!