Skip to content

Commit

Permalink
ioredis
Browse files Browse the repository at this point in the history
  • Loading branch information
glasser committed May 4, 2021
1 parent 5eef9aa commit 85c0fb5
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fetch, Request } from './__mocks__/apollo-server-env';
import { fetch, Request } from './mock-apollo-server-env';

import FakeTimers from '@sinonjs/fake-timers';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fetch, URL, Request } from './__mocks__/apollo-server-env';
import { fetch, URL, Request } from './mock-apollo-server-env';

import {
ApolloError,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
jest.mock('ioredis');
jest.mock('ioredis', () => require('./mockIoredis'));

import { RedisCache } from '../index';
import { runKeyValueCacheTests } from 'apollo-server-caching';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
jest.mock('ioredis');
jest.mock('ioredis', () => require('./mockIoredis'));

import { RedisClusterCache } from '../index';
import { runKeyValueCacheTests } from 'apollo-server-caching';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,30 @@
class Redis {
private keyValue = {};
private keyValue = new Map<string, {value: string, ttl: number | undefined}>();
private timeouts = new Set<NodeJS.Timer>();

async del(key: string) {
const keysDeleted = this.keyValue.hasOwnProperty(key) ? 1 : 0;
delete this.keyValue[key];
const keysDeleted = this.keyValue.has(key) ? 1 : 0;
this.keyValue.delete(key);
return keysDeleted;
}

async get(key: string) {
if (this.keyValue[key]) {
return this.keyValue[key].value;
}
return this.keyValue.get(key)?.value;
}

async mget(...keys: string[]) {
return keys.map((key) => {
if (this.keyValue[key]) {
return this.keyValue[key].value;
}
});
return keys.map((key) => this.keyValue.get(key)?.value);
}

async set(key, value, type, ttl) {
this.keyValue[key] = {
async set(key: string, value: string, _: string, ttl: number | undefined) {
this.keyValue.set(key, {
value,
ttl,
};
});
if (ttl) {
const timeout = setTimeout(() => {
this.timeouts.delete(timeout);
delete this.keyValue[key];
this.del(key);
}, ttl * 1000);
this.timeouts.add(timeout);
}
Expand Down

0 comments on commit 85c0fb5

Please sign in to comment.