@@ -23,7 +23,7 @@ import { Op, QueryTypes, sql } from "@sequelize/core";
2323
2424import promClient from "prom-client" ;
2525import { db , Limit , Name , Offset , PaginatedResult , SqTransaction } from "../../database/index.js" ;
26-
26+ import { cachedFindAndCountAll , cachedCount , invalidateCountCache } from "../../utils/cache.js" ;
2727import { sanitiseLimit , sanitiseOffset } from "../../utils/index.js" ;
2828import { NAME_COST } from "../../utils/vars.js" ;
2929
@@ -38,7 +38,7 @@ export async function getNames(
3838 limit ?: Limit ,
3939 offset ?: Offset
4040) : Promise < PaginatedResult < Name > > {
41- return Name . findAndCountAll ( {
41+ return cachedFindAndCountAll ( Name , {
4242 order : [ [ "name" , "ASC" ] ] ,
4343 limit : sanitiseLimit ( limit ) ,
4444 offset : sanitiseOffset ( offset )
@@ -50,7 +50,7 @@ export async function getNamesByAddress(
5050 limit ?: Limit ,
5151 offset ?: Offset
5252) : Promise < PaginatedResult < Name > > {
53- return Name . findAndCountAll ( {
53+ return cachedFindAndCountAll ( Name , {
5454 order : [ [ "name" , "ASC" ] ] ,
5555 where : { owner : address } ,
5656 limit : sanitiseLimit ( limit ) ,
@@ -72,7 +72,7 @@ export async function getDetailedUnpaid(): Promise<DetailedUnpaidResponseRow[]>
7272}
7373
7474export async function getNameCountByAddress ( address : string ) : Promise < number > {
75- return Name . count ( { where : { owner : address } } ) ;
75+ return cachedCount ( Name , { where : { owner : address } } ) ;
7676}
7777
7878export async function getName ( name : string ) : Promise < Name | null > {
@@ -83,7 +83,7 @@ export async function getUnpaidNames(
8383 limit ?: Limit ,
8484 offset ?: Offset
8585) : Promise < PaginatedResult < Name > > {
86- return Name . findAndCountAll ( {
86+ return cachedFindAndCountAll ( Name , {
8787 order : [ [ "id" , "DESC" ] ] ,
8888 where : { unpaid : { [ Op . gt ] : 0 } } ,
8989 limit : sanitiseLimit ( limit ) ,
@@ -115,12 +115,17 @@ export async function createName(
115115 unpaid : NAME_COST
116116 } , { transaction : dbTx } ) ;
117117
118- promNamesPurchasedCounter . inc ( ) ;
118+ dbTx . afterCommit ( async ( ) => {
119+ promNamesPurchasedCounter . inc ( ) ;
120+
121+ // Invalidate name count caches
122+ await invalidateCountCache ( Name . name ) ;
119123
120- wsManager . broadcastEvent ( {
121- type : "event" ,
122- event : "name" ,
123- name : nameToJson ( dbName )
124+ wsManager . broadcastEvent ( {
125+ type : "event" ,
126+ event : "name" ,
127+ name : nameToJson ( dbName )
128+ } ) ;
124129 } ) ;
125130
126131 return dbName ;
0 commit comments