@@ -26,9 +26,11 @@ import {
2626} from '../utils/event'
2727import { IEventRepository , INip05VerificationRepository , IUserRepository } from '../@types/repositories'
2828import { IEventStrategy , IMessageHandler } from '../@types/message-handlers'
29+ import { CacheAdmissionState } from '../constants/caching'
2930import { createCommandResult } from '../utils/messages'
3031import { createLogger } from '../factories/logger-factory'
3132import { Factory } from '../@types/base'
33+ import { ICacheAdapter } from '../@types/adapters'
3234import { IncomingEventMessage } from '../@types/messages'
3335import { IRateLimiter } from '../@types/utils'
3436import { IWebSocketAdapter } from '../@types/adapters'
@@ -46,6 +48,7 @@ export class EventMessageHandler implements IMessageHandler {
4648 private readonly settings : ( ) => Settings ,
4749 private readonly slidingWindowRateLimiter : Factory < IRateLimiter > ,
4850 private readonly nip05VerificationRepository : INip05VerificationRepository ,
51+ private readonly cache : ICacheAdapter ,
4952 ) { }
5053
5154 public async handleMessage ( message : IncomingEventMessage ) : Promise < void > {
@@ -112,8 +115,7 @@ export class EventMessageHandler implements IMessageHandler {
112115 try {
113116 await strategy . execute ( event )
114117 this . processNip05Metadata ( event )
115- } catch ( error ) {
116- console . error ( 'error handling message' , message , error )
118+ } catch ( _error ) {
117119 this . webSocket . emit ( WebSocketAdapterEvent . Message , createCommandResult ( event . id , false , 'error: unable to process event' ) )
118120 }
119121 }
@@ -341,17 +343,44 @@ export class EventMessageHandler implements IMessageHandler {
341343 return
342344 }
343345
344- // const hasKey = await this.cache.hasKey(`${event.pubkey}:is-admitted`)
345- // TODO: use cache
346+ const cacheKey = `${ event . pubkey } :is-admitted`
347+
348+ try {
349+ const cachedValue = await this . cache . getKey ( cacheKey )
350+ if ( cachedValue === CacheAdmissionState . ADMITTED ) {
351+ debug ( 'cache hit for %s admission: admitted' , event . pubkey )
352+ return
353+ }
354+ if ( cachedValue === CacheAdmissionState . BLOCKED_NOT_ADMITTED ) {
355+ debug ( 'cache hit for %s admission: blocked' , event . pubkey )
356+ return 'blocked: pubkey not admitted'
357+ }
358+ if ( cachedValue === CacheAdmissionState . BLOCKED_INSUFFICIENT_BALANCE ) {
359+ debug ( 'cache hit for %s admission: insufficient balance' , event . pubkey )
360+ return 'blocked: insufficient balance'
361+ }
362+ } catch ( error ) {
363+ debug ( 'cache error for %s: %o' , event . pubkey , error )
364+ }
365+
346366 const user = await this . userRepository . findByPubkey ( event . pubkey )
347367 if ( ! user || ! user . isAdmitted ) {
368+ this . cacheSet ( cacheKey , CacheAdmissionState . BLOCKED_NOT_ADMITTED , 60 )
348369 return 'blocked: pubkey not admitted'
349370 }
350371
351372 const minBalance = currentSettings . limits ?. event ?. pubkey ?. minBalance ?? 0n
352373 if ( minBalance > 0n && user . balance < minBalance ) {
374+ this . cacheSet ( cacheKey , CacheAdmissionState . BLOCKED_INSUFFICIENT_BALANCE , 60 )
353375 return 'blocked: insufficient balance'
354376 }
377+
378+ this . cacheSet ( cacheKey , CacheAdmissionState . ADMITTED , 300 )
379+ }
380+
381+ private cacheSet ( key : string , value : string , ttl : number ) : void {
382+ this . cache . setKey ( key , value , ttl )
383+ . catch ( ( error ) => debug ( 'unable to cache %s: %o' , key , error ) )
355384 }
356385
357386 protected addExpirationMetadata ( event : Event ) : Event | ExpiringEvent {
0 commit comments