Skip to content

Commit

Permalink
feat: bring momento (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chriscbr authored Jul 23, 2024
1 parent 6e8b008 commit da20cfe
Show file tree
Hide file tree
Showing 15 changed files with 2,161 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/canary.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions .github/workflows/momento-pull.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions .github/workflows/momento-release.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .github/workflows/pull-request-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ jobs:
k8s
lock
messagefanout
momento
ngrok
openai
postgres
Expand Down
6 changes: 6 additions & 0 deletions .mergify.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ publishing them for you.
| [k8s](./k8s) | [@winglibs/k8s](https://www.npmjs.com/package/@winglibs/k8s) | k8s |
| [lock](./lock) | [@winglibs/lock](https://www.npmjs.com/package/@winglibs/lock) | * |
| [messagefanout](./messagefanout) | [@winglibs/messagefanout](https://www.npmjs.com/package/@winglibs/messagefanout) | sim, tf-aws |
| [momento](./momento) | [@winglibs/momento](https://www.npmjs.com/package/@winglibs/momento) | sim, tf-aws, tf-azure, tf-gcp |
| [ngrok](./ngrok) | [@winglibs/ngrok](https://www.npmjs.com/package/@winglibs/ngrok) | * |
| [openai](./openai) | [@winglibs/openai](https://www.npmjs.com/package/@winglibs/openai) | * |
| [postgres](./postgres) | [@winglibs/postgres](https://www.npmjs.com/package/@winglibs/postgres) | sim, tf-aws |
Expand Down
4 changes: 4 additions & 0 deletions momento/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target/
node_modules/
/.env
/.env.test
21 changes: 21 additions & 0 deletions momento/LICENSE
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.
39 changes: 39 additions & 0 deletions momento/README.md
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).
4 changes: 4 additions & 0 deletions momento/cache.extern.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions momento/cache.test.w
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);
}
54 changes: 54 additions & 0 deletions momento/cache.ts
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");
}
};
Loading

0 comments on commit da20cfe

Please sign in to comment.