Skip to content

Commit

Permalink
fix: add javascript patterns examples files (#1266)
Browse files Browse the repository at this point in the history
  • Loading branch information
bruuuuuuuce authored May 7, 2024
1 parent bf6c16e commit 991969f
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions examples/nodejs/cache/doc-example-files/patterns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
*
* This file contains examples of consuming the JavaScript APIs, for use as examples
* in the public dev docs. Each function name that begins with `example_` is available
* to the dev docs to inject into the code snippets widget for the specified API.
*
* These examples should all be JavaScript; we can add TypeScript-specific examples in
* a second file in the future if desired.
*
*/
import {CacheClient, Configurations, CredentialProvider} from '@gomomento/sdk';

const database: Map<string, string> = new Map();
database.set('test-key', 'test-value');

/* eslint-disable @typescript-eslint/no-unused-vars */
async function example_patterns_ReadAsideCaching(cacheClient: CacheClient): Promise<string> {
const cachedValue = (await cacheClient.get('test-cache', 'test-key')).value();
if (cachedValue !== undefined) {
console.log(`Cache hit for key 'test-key': ${cachedValue}`);
return cachedValue;
} else {
console.log("Key 'test-key' was not found in cache 'test-cache', checking in database...");
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const actualValue = database.get('test-key')!;
await cacheClient.set('test-cache', 'test-key', actualValue);
return actualValue;
}
}

/* eslint-disable @typescript-eslint/no-unused-vars */
async function example_patterns_WriteThroughCaching(cacheClient: CacheClient): Promise<string> {
const value = 'test-value';
database.set('test-key', value);
await cacheClient.set('test-cache', 'test-key', value);
return value;
}

async function main() {
const cacheClient = await CacheClient.create({
configuration: Configurations.Laptop.v1(),
credentialProvider: CredentialProvider.fromEnvironmentVariable({
environmentVariableName: 'MOMENTO_API_KEY',
}),
defaultTtlSeconds: 60,
});
await cacheClient.createCache('test-cache');

await example_patterns_ReadAsideCaching(cacheClient);
await example_patterns_WriteThroughCaching(cacheClient);
}

main().catch(e => {
throw e;
});

0 comments on commit 991969f

Please sign in to comment.