File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -38,6 +38,14 @@ class Cache {
3838 async hasUniqueLaunch ( mod : string , version : string , ip : string ) {
3939 return this . cache . has ( `unique_launch:${ mod } :${ version } :${ ip } ` ) ;
4040 }
41+
42+ async setPageView ( mod : string , source : string , ip : string ) {
43+ return this . cache . set ( `page_view:${ mod } :${ source } :${ ip } ` , 1 , "10m" ) ;
44+ }
45+
46+ async hasPageView ( mod : string , source : string , ip : string ) {
47+ return this . cache . has ( `page_view:${ mod } :${ source } :${ ip } ` ) ;
48+ }
4149}
4250
4351export default async function ( fastify : FastifyInstance ) {
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import { z } from "zod";
55
66export const plugin : FastifyPluginAsyncZod = async fastify => {
77 const db = fastify . mongo . db ! ;
8+ const cache = fastify . cache ;
89
910 fastify . get (
1011 "/:mod_id" ,
@@ -41,15 +42,21 @@ export const plugin: FastifyPluginAsyncZod = async fastify => {
4142 return `Banner ${ mod_id } not found.` ;
4243 }
4344
44- await db . collection < PageViews > ( Collections . PageViews ) . updateOne (
45- { mod_id } ,
46- {
47- $inc : {
48- [ `${ source } .${ dayjs ( ) . format ( "YYYY.M.D" ) } ` ] : 1
49- }
50- } ,
51- { upsert : true }
52- ) ;
45+ const hasPageView = await cache . hasPageView ( mod_id , source , request . ip ) ;
46+
47+ if ( ! hasPageView ) {
48+ await db . collection < PageViews > ( Collections . PageViews ) . updateOne (
49+ { mod_id } ,
50+ {
51+ $inc : {
52+ [ `${ source } .${ dayjs ( ) . format ( "YYYY.M.D" ) } ` ] : 1
53+ }
54+ } ,
55+ { upsert : true }
56+ ) ;
57+
58+ await cache . setPageView ( mod_id , source , request . ip ) ;
59+ }
5360
5461 reply . redirect (
5562 `https://blakesmods.com/img/banner/${ mod_id } _title.png` ,
You can’t perform that action at this time.
0 commit comments