-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
2,161 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ jobs: | |
k8s | ||
lock | ||
messagefanout | ||
momento | ||
ngrok | ||
openai | ||
postgres | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
target/ | ||
node_modules/ | ||
/.env | ||
/.env.test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Wing | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# momento | ||
|
||
## Prerequisites | ||
|
||
* [winglang](https://winglang.io) | ||
* [Momento](https://www.gomomento.com/) account and API key (see [Getting started with Momento](https://docs.momentohq.com/cache/getting-started)) | ||
|
||
## Installation | ||
|
||
```sh | ||
npm i @winglibs/momento | ||
``` | ||
|
||
## Usage | ||
|
||
```js | ||
bring cloud; | ||
bring momento; | ||
|
||
let token = new cloud.Secret("momento-key"); | ||
let cache = new momento.Cache(token: token); | ||
|
||
new cloud.Function(inflight () => { | ||
cache.set("key", "value"); | ||
log(cache.get("key")); | ||
}); | ||
``` | ||
|
||
To deploy an application with Momento resources using Terraform, you will need to set the MOMENTO_API_KEY environment variable to a valid super-user API key on the machine running `terraform apply`. | ||
|
||
You will also need a valid Momento token (super-user token or fine-grained access token) with permissions for performing any data operations on the cache(s) that can be used by the application. | ||
You can set the secret by running `wing secrets -t tf-aws main.w`. | ||
You are responsible for rotating the secret as needed. | ||
|
||
See the [Momento documentation](https://docs.momentohq.com/cache/develop/authentication/api-keys) for more information about creating API keys. | ||
|
||
## License | ||
|
||
This library is licensed under the [MIT License](./LICENSE). |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
bring cloud; | ||
bring expect; | ||
bring "./cache.w" as momento; | ||
bring util; | ||
|
||
let token = new cloud.Secret(name: "MOMENTO_API_KEY"); | ||
|
||
let c = new momento.Cache(token: token) as "WinglyCache"; | ||
|
||
test "can set and get a cache value" { | ||
c.set("key", "value", ttl: 10s); | ||
let value = c.get("key"); | ||
log(value ?? "nil"); | ||
expect.equal(value, "value"); | ||
util.sleep(11s); | ||
let value2 = c.get("key"); | ||
log(value2 ?? "nil"); | ||
expect.equal(value2, nil); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import types from "./cache.extern"; | ||
import { | ||
CacheGet, | ||
CacheClient, | ||
Configurations, | ||
CredentialProvider, | ||
CacheSet, | ||
} from "@gomomento/sdk"; | ||
|
||
let cacheClients: Record<string, CacheClient> = {}; | ||
|
||
async function createCacheClient(token: string) { | ||
if (!cacheClients[token]) { | ||
cacheClients[token] = await CacheClient.create({ | ||
configuration: Configurations.Lambda.latest(), | ||
credentialProvider: CredentialProvider.fromString(token), | ||
defaultTtlSeconds: 60, // won't be used | ||
}); | ||
} | ||
} | ||
|
||
export const _get: types["_get"] = async (token, cacheName, key) => { | ||
await createCacheClient(token); | ||
const getResponse = await cacheClients[token].get(cacheName, key); | ||
if (getResponse instanceof CacheGet.Hit) { | ||
return getResponse.valueString(); | ||
} else if (getResponse instanceof CacheGet.Miss) { | ||
return undefined; | ||
} else if (getResponse instanceof CacheGet.Error) { | ||
throw getResponse.innerException(); | ||
} else { | ||
throw new Error("Unexpected response type"); | ||
} | ||
}; | ||
|
||
export const _set: types["_set"] = async ( | ||
token, | ||
cacheName, | ||
key, | ||
value, | ||
ttl | ||
) => { | ||
await createCacheClient(token); | ||
const setResponse = await cacheClients[token].set(cacheName, key, value, { | ||
ttl, | ||
}); | ||
if (setResponse instanceof CacheSet.Success) { | ||
return; | ||
} else if (setResponse instanceof CacheSet.Error) { | ||
throw setResponse.innerException(); | ||
} else { | ||
throw new Error("Unexpected response type"); | ||
} | ||
}; |
Oops, something went wrong.