Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds set options for apollo #2

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ import {

type KeyvMomentoOptions<Value> = NonNullable<unknown> & Keyv.Options<Value>;

/**
* KeyValueCacheSetOptions exported to match with apollo graphql set options
* defined here:
* https://github.com/apollographql/apollo-utils/blob/main/packages/keyValueCache/src/KeyValueCache.ts
*/
interface KeyValueCacheSetOptions {
/**
* Specified in **seconds**, the time-to-live (TTL) value limits the lifespan
* of the data being stored in the cache.
*/
ttl?: number | null;
}

class KeyvMomento<Value = any> extends EventEmitter implements Store<Value> {
namespace?: string;
ttlSupport = true;
Expand Down Expand Up @@ -59,20 +72,29 @@ class KeyvMomento<Value = any> extends EventEmitter implements Store<Value> {
});
}

async set(key: string, value: Value, ttl?: number) {
const options: {ttl?: number} = {};

if (ttl !== undefined) {
// eslint-disable-next-line no-multi-assign
options.ttl = Math.floor(ttl / 1000); // Moving to seconds
async set(
key: string,
value: Value,
options?: number | KeyValueCacheSetOptions
) {
const momentoSetOptions: {ttl?: number} = {};

if (options !== undefined) {
if (typeof options === 'number') {
momentoSetOptions.ttl = Math.floor(options / 1000); // Moving to seconds
} else {
if (options.ttl) {
momentoSetOptions.ttl = options.ttl;
}
}
}

const rsp = await this.client.set(
this.cacheName,
key,
// @ts-expect-error - Value needs to be number, string or buffer
value,
options
momentoSetOptions
);
if (rsp instanceof CacheSet.Error) {
this.emit('error', rsp.message());
Expand Down
6 changes: 3 additions & 3 deletions test/integration-setup.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {v4} from 'uuid';
import {CacheClientProps} from '@gomomento/sdk/dist/src/cache-client-props';
import {
CreateCache,
CacheClient,
Configurations,
CreateCache,
CredentialProvider,
DeleteCache,
MomentoErrorCode,
CacheClient,
CredentialProvider,
} from '@gomomento/sdk';
import KeyvMomento from '../src';

Expand Down
11 changes: 10 additions & 1 deletion test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,17 @@ describe('simple get and set', () => {

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const value = await client.get(testKey);

expect(value).toBeUndefined();

// Test with ttl passed as option that uses seconds instead
const testKey2 = v4();
await client.set(testKey2, 'expiring_soon', {ttl: 1});

await snooze(3000);

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const value2 = await client.get(testKey2);
expect(value2).toBeUndefined();
});

it('keyv get / expired', async () => {
Expand Down