Skip to content

Commit

Permalink
adds : method to fetch events betwen timestamps. fixes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
argahsuknesib committed Mar 7, 2024
1 parent 9c726af commit 16f50e7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/service/CacheService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ describe('CacheService', () => {
expect(is_disconnected).toBe(true);
});

it('should_describe_the_cache', async() => {
it('should_describe_the_cache', async () => {
const status = await cacheService.get_status();
expect(status).toBe('wait');
})
});
});
28 changes: 28 additions & 0 deletions src/service/CacheService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,33 @@ export class CacheService {
async get_status(): Promise<RedisStatus> {
return await this.client.status;
}
/**
* Get all key-value pairs from the Redis cache.
* This method is not recommended for large databases, as it will load all key-value pairs into memory and be slow.
* However, it is useful for notification caching, where the database is expected to be small to get the missing notifications.
* @return {Promise<{ [key: string]: string }>} - A promise that resolves to an object containing all key-value pairs in the cache.
* @memberof CacheService
*/
async read_whole_database(): Promise<{ [key: string]: string }> {
let cursor = '0';
const key_values: { [key: string]: string } = {};
do {
const [newCursor, keys] = await this.client.scan(cursor);
cursor = newCursor;

if (keys.length > 0) {
const values = await this.client.mget(...keys);
keys.forEach((key: any, index: any) => {
const value = values[index];
if (value !== null) {
key_values[key] = value;
} else {
console.warn(`Key '${key}' does not exist.`);
}
});
}
} while (cursor !== '0');
return key_values
}
}

28 changes: 28 additions & 0 deletions src/utils/FetchUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Readable } from "stream"
import { CacheService } from "../service/CacheService";
const cache_service = new CacheService();
export async function getMembers(opts?: {
from?: Date,
until?: Date,
}): Promise<Readable> {
opts = opts ?? {};
const from = opts.from ?? new Date(0);
const until = opts.until ?? new Date();
const from_epoch = from.getTime();
const until_epoch = until.getTime();
const member_stream = new Readable({
objectMode: true,
read() {

}
});
const database = await cache_service.read_whole_database();
for (const key in database) {
const value = database[key];
const timestamp = parseInt(key);
if (timestamp >= from_epoch && timestamp <= until_epoch) {
member_stream.push(value);
}
}
return member_stream;
}

0 comments on commit 16f50e7

Please sign in to comment.