forked from momentohq/client-sdk-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
98 lines (88 loc) · 2.79 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import {
CacheGet,
ListCaches,
CreateCache,
CacheSet,
CacheDelete,
LogLevel,
LogFormat,
SimpleCacheClient,
EnvMomentoTokenProvider,
Configurations,
LoggerOptions,
} from '@gomomento/sdk';
const cacheName = 'cache';
const cacheKey = 'key';
const cacheValue = 'value';
const credentialsProvider = new EnvMomentoTokenProvider('MOMENTO_AUTH_TOKEN');
const loggerOptions: LoggerOptions = {
level: LogLevel.INFO,
format: LogFormat.JSON,
};
const defaultTtl = 60;
const momento = new SimpleCacheClient({
configuration: Configurations.Laptop.latest(loggerOptions),
credentialProvider: credentialsProvider,
defaultTtlSeconds: defaultTtl,
});
const main = async () => {
const createCacheResponse = await momento.createCache(cacheName);
if (createCacheResponse instanceof CreateCache.AlreadyExists) {
console.log('cache already exists');
} else if (createCacheResponse instanceof CreateCache.Error) {
throw createCacheResponse.innerException();
}
console.log('Listing caches:');
let token: string | undefined;
do {
const listResponse = await momento.listCaches(token);
if (listResponse instanceof ListCaches.Error) {
console.log(`Error listing caches: ${listResponse.message()}`);
break;
} else if (listResponse instanceof ListCaches.Success) {
listResponse.getCaches().forEach(cacheInfo => {
console.log(`${cacheInfo.getName()}`);
});
token = listResponse.getNextToken();
}
} while (token !== undefined);
const exampleTtlSeconds = 10;
console.log(
`Storing key=${cacheKey}, value=${cacheValue}, ttl=${exampleTtlSeconds}`
);
const setResponse = await momento.set(
cacheName,
cacheKey,
cacheValue,
exampleTtlSeconds
);
if (setResponse instanceof CacheSet.Success) {
console.log(
'Key stored successfully with value ' + setResponse.valueString()
);
} else if (setResponse instanceof CacheSet.Error) {
console.log('Error setting key: ' + setResponse.message());
}
const getResponse = await momento.get(cacheName, cacheKey);
if (getResponse instanceof CacheGet.Hit) {
console.log(`cache hit: ${String(getResponse.valueString())}`);
} else if (getResponse instanceof CacheGet.Miss) {
console.log('cache miss');
} else if (getResponse instanceof CacheGet.Error) {
console.log(`Error: ${getResponse.message()}`);
}
const deleteResponse = await momento.delete(cacheName, cacheKey);
if (deleteResponse instanceof CacheDelete.Error) {
console.log(`Error deleting cache key: ${deleteResponse.message()}`);
} else if (deleteResponse instanceof CacheDelete.Success) {
console.log('Deleted key from cache');
}
};
main()
.then(() => {
console.log('success!!');
})
.catch((e: Error) => {
console.error(`failed to get from cache ${e.message}`);
throw e;
});