@@ -2,13 +2,9 @@ import NodeCache from 'node-cache';
2
2
import { Network } from '../constants' ;
3
3
import { IDexHelper } from '../dex-helper' ;
4
4
import { Logger } from '../types' ;
5
- import _ from 'lodash' ;
6
- import jsonDiff from 'json-diff' ;
7
- import hash from 'object-hash' ;
8
5
9
6
type KeyValuePubSubMsg = {
10
7
expiresAt : number ;
11
- hash : string ;
12
8
data : Record < string , unknown > ;
13
9
} ;
14
10
@@ -19,7 +15,6 @@ export class ExpKeyValuePubSub {
19
15
network : Network ;
20
16
localCache : NodeCache = new NodeCache ( ) ;
21
17
22
- // TODO-rfq-ps: temporary logger
23
18
logger : Logger ;
24
19
25
20
constructor (
@@ -45,20 +40,15 @@ export class ExpKeyValuePubSub {
45
40
}
46
41
47
42
publish ( data : Record < string , unknown > , ttl : number ) {
48
- const hashedData = hash ( data ) ;
49
- this . logger . info ( `Publishing to ${ this . channel } with hash ${ hashedData } ` ) ;
50
-
51
43
const expiresAt = Math . round ( Date . now ( ) / 1000 ) + ttl ;
52
44
this . dexHelper . cache . publish (
53
45
this . channel ,
54
- JSON . stringify ( { expiresAt, data, hash : hashedData } ) ,
46
+ JSON . stringify ( { expiresAt, data } ) ,
55
47
) ;
56
48
}
57
49
58
50
handleSubscription ( msg : KeyValuePubSubMsg ) {
59
- const { expiresAt, data, hash } = msg ;
60
-
61
- this . logger . info ( `Received message from ${ this . channel } with hash ${ hash } ` ) ;
51
+ const { expiresAt, data } = msg ;
62
52
63
53
const now = Math . round ( Date . now ( ) / 1000 ) ;
64
54
// calculating ttl as message might come with the delay
@@ -82,31 +72,16 @@ export class ExpKeyValuePubSub {
82
72
async getAndCache < T > ( key : string ) : Promise < T | null > {
83
73
const localValue = this . localCache . get < T > ( key ) ;
84
74
85
- // if (localValue) {
86
- // return localValue;
87
- // }
75
+ if ( localValue ) {
76
+ return localValue ;
77
+ }
88
78
89
79
const [ value , ttl ] = await Promise . all ( [
90
80
this . dexHelper . cache . get ( this . dexKey , this . network , key ) ,
91
81
this . dexHelper . cache . ttl ( this . dexKey , this . network , key ) ,
92
82
] ) ;
93
83
94
- // TODO-rfq-ps: compare local and cache value
95
- const isEqual = _ . isEqual (
96
- localValue ?? null ,
97
- value ? JSON . parse ( value ) : null ,
98
- ) ;
99
- if ( ! isEqual ) {
100
- this . logger . info (
101
- `Values are not equal for the key ${ key } , local: ${ JSON . stringify (
102
- localValue ,
103
- ) } , cache: ${ value } , diff: ${ jsonDiff . diffString ( localValue , value ) } `,
104
- ) ;
105
- }
106
-
107
84
if ( value && ttl > 0 ) {
108
- // setting ttl same as in cache
109
- // TODO-ps: check if ttl is not null
110
85
const parsedValue = JSON . parse ( value ) ;
111
86
this . localCache . set ( key , parsedValue , ttl ) ;
112
87
return parsedValue ;
@@ -126,14 +101,12 @@ export class NonExpSetPubSub {
126
101
network : Network ;
127
102
set = new Set < string > ( ) ;
128
103
129
- // TODO-rfq-ps: temporary logger
130
104
logger : Logger ;
131
105
132
106
constructor (
133
107
private dexHelper : IDexHelper ,
134
108
private dexKey : string ,
135
109
channel : string ,
136
- private blackListCacheKey : string ,
137
110
) {
138
111
this . network = this . dexHelper . config . data . network ;
139
112
this . channel = `${ this . network } _${ this . dexKey } _${ channel } ` ;
@@ -142,7 +115,6 @@ export class NonExpSetPubSub {
142
115
}
143
116
144
117
async initializeAndSubscribe ( initialSet : string [ ] ) {
145
- // as there's no lazy load, we need to initialize the set
146
118
for ( const member of initialSet ) {
147
119
this . set . add ( member ) ;
148
120
}
@@ -160,38 +132,16 @@ export class NonExpSetPubSub {
160
132
}
161
133
162
134
publish ( msg : SetPubSubMsg ) {
163
- this . logger . info ( `Publishing to ${ this . channel } ` ) ;
164
-
165
- // should not be a problem, as we also subscribe to the channel on the same instance
166
- // // as there's no lazy load, also store locally
167
- // for (const key of set) {
168
- // this.set.add(key);
169
- // }
170
135
this . dexHelper . cache . publish ( this . channel , JSON . stringify ( msg ) ) ;
171
136
}
172
137
173
138
handleSubscription ( set : SetPubSubMsg ) {
174
- this . logger . info ( `Received message from ${ this . channel } ` ) ;
175
139
for ( const key of set ) {
176
140
this . set . add ( key ) ;
177
141
}
178
142
}
179
143
180
144
async has ( key : string ) {
181
- const localValue = this . set . has ( key ) ;
182
-
183
- const value = await this . dexHelper . cache . sismember (
184
- this . blackListCacheKey ,
185
- key ,
186
- ) ;
187
-
188
- // TODO-rfq-ps: compare local and cache value
189
- const isEqual = localValue === value ;
190
-
191
- if ( ! isEqual ) {
192
- this . logger . error ( 'Values are not equal' , { localValue, value } ) ;
193
- }
194
-
195
- return value ;
145
+ return this . set . has ( key ) ;
196
146
}
197
147
}
0 commit comments