File tree Expand file tree Collapse file tree 3 files changed +58
-2
lines changed Expand file tree Collapse file tree 3 files changed +58
-2
lines changed Original file line number Diff line number Diff line change @@ -29,8 +29,8 @@ describe('CacheService', () => {
29
29
expect ( is_disconnected ) . toBe ( true ) ;
30
30
} ) ;
31
31
32
- it ( 'should_describe_the_cache' , async ( ) => {
32
+ it ( 'should_describe_the_cache' , async ( ) => {
33
33
const status = await cacheService . get_status ( ) ;
34
34
expect ( status ) . toBe ( 'wait' ) ;
35
- } )
35
+ } ) ;
36
36
} ) ;
Original file line number Diff line number Diff line change @@ -88,5 +88,33 @@ export class CacheService {
88
88
async get_status ( ) : Promise < RedisStatus > {
89
89
return await this . client . status ;
90
90
}
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
+ }
91
119
}
92
120
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments