Skip to content

Commit 16f50e7

Browse files
author
Kushagra Singh Bisen
committed
adds : method to fetch events betwen timestamps. fixes #4
1 parent 9c726af commit 16f50e7

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

src/service/CacheService.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ describe('CacheService', () => {
2929
expect(is_disconnected).toBe(true);
3030
});
3131

32-
it('should_describe_the_cache', async() => {
32+
it('should_describe_the_cache', async () => {
3333
const status = await cacheService.get_status();
3434
expect(status).toBe('wait');
35-
})
35+
});
3636
});

src/service/CacheService.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,33 @@ export class CacheService {
8888
async get_status(): Promise<RedisStatus> {
8989
return await this.client.status;
9090
}
91+
/**
92+
* Get all key-value pairs from the Redis cache.
93+
* This method is not recommended for large databases, as it will load all key-value pairs into memory and be slow.
94+
* However, it is useful for notification caching, where the database is expected to be small to get the missing notifications.
95+
* @return {Promise<{ [key: string]: string }>} - A promise that resolves to an object containing all key-value pairs in the cache.
96+
* @memberof CacheService
97+
*/
98+
async read_whole_database(): Promise<{ [key: string]: string }> {
99+
let cursor = '0';
100+
const key_values: { [key: string]: string } = {};
101+
do {
102+
const [newCursor, keys] = await this.client.scan(cursor);
103+
cursor = newCursor;
104+
105+
if (keys.length > 0) {
106+
const values = await this.client.mget(...keys);
107+
keys.forEach((key: any, index: any) => {
108+
const value = values[index];
109+
if (value !== null) {
110+
key_values[key] = value;
111+
} else {
112+
console.warn(`Key '${key}' does not exist.`);
113+
}
114+
});
115+
}
116+
} while (cursor !== '0');
117+
return key_values
118+
}
91119
}
92120

src/utils/FetchUtil.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Readable } from "stream"
2+
import { CacheService } from "../service/CacheService";
3+
const cache_service = new CacheService();
4+
export async function getMembers(opts?: {
5+
from?: Date,
6+
until?: Date,
7+
}): Promise<Readable> {
8+
opts = opts ?? {};
9+
const from = opts.from ?? new Date(0);
10+
const until = opts.until ?? new Date();
11+
const from_epoch = from.getTime();
12+
const until_epoch = until.getTime();
13+
const member_stream = new Readable({
14+
objectMode: true,
15+
read() {
16+
17+
}
18+
});
19+
const database = await cache_service.read_whole_database();
20+
for (const key in database) {
21+
const value = database[key];
22+
const timestamp = parseInt(key);
23+
if (timestamp >= from_epoch && timestamp <= until_epoch) {
24+
member_stream.push(value);
25+
}
26+
}
27+
return member_stream;
28+
}

0 commit comments

Comments
 (0)