Skip to content

Commit

Permalink
feat: adds set options for apollo (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
eaddingtonwhite authored Jul 10, 2023
1 parent 2700b12 commit c01c9ce
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
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

0 comments on commit c01c9ce

Please sign in to comment.