Skip to content

Commit 66548f3

Browse files
committed
fix silly mistake and add cache removal
1 parent e8542fc commit 66548f3

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

mod.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"geode": "4.3.1",
2+
"geode": "4.4.0",
33
"gd": {
44
"win": "2.2074",
55
"android": "2.2074",
@@ -30,7 +30,7 @@
3030
},
3131
"auth-server": {
3232
"name": "Auth Server",
33-
"description": "Authentication Server (-1 = None, 0 = DashAuth, 1 = GDAuth, 2 = Custom Auth (Ignores the test auth))",
33+
"description": "Authentication Server (-1 = None, 0 = DashAuth, 1 = Argon, 2 = Custom Auth (Ignores the test auth))",
3434
"type": "int",
3535
"default": -1,
3636
"min": -1,

server/src/Components/TimedMap.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ export class TimedMap<K, V> {
3838
delete(key: K): boolean {
3939
return this.map.delete(key);
4040
}
41+
deleteByPattern(pattern: string): void {
42+
for (const key of this.map.keys()) {
43+
const keyStr = String(key);
44+
if (keyStr.startsWith(pattern)) {
45+
this.map.delete(key);
46+
}
47+
}
48+
}
4149
find(fn: (value: TimedEntry<V>, key: K) => boolean): [K, TimedEntry<V>] | null {
4250
for (const [key, value] of this.map.entries()) {
4351
if (fn(value, key)) {

server/src/cache.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { TimedMap } from './Components/TimedMap';
33

44
type ShouldCacheRoute = (req: Request) => boolean;
55

6-
class CacheManager {
6+
export class CacheManager {
77
private static cache = new TimedMap<string, any>(60000, 1000);
88
static get<T>(key: string): T | null {
99
return this.cache.get(key);
@@ -14,6 +14,9 @@ class CacheManager {
1414
static delete(key: string): boolean {
1515
return this.cache.delete(key);
1616
}
17+
static deletePattern(key: string): void {
18+
this.cache.deleteByPattern(key);
19+
}
1720
}
1821

1922
export const cacheMiddleware = (ttl: number, cacheRoute: ShouldCacheRoute = () => true) => {

server/src/controllers/objects.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { verifyToken, banCheck } from './user';
88
import axios from 'axios';
99
import moment from 'moment'
1010
import { UserData } from '@/Components/User';
11-
import { cacheMiddleware } from '../cache';
11+
import { cacheMiddleware, CacheManager } from '../cache';
1212

1313
const allowedTags = ["Font", "Decoration", "Gameplay", "Art", "Structure", "Custom", "Icon", "Meme", "Technical", "Particles", "Triggers", "SFX", "Effects", "Auto Build", "Recreation"];
1414

@@ -286,7 +286,7 @@ oRouter.post('/objects/:id/overwrite',
286286
}
287287
);
288288

289-
oRouter.get('/objects/:id', param("id").isInt({min: 0, max: 2147483647}).notEmpty(), cacheMiddleware(600, (req) => {
289+
oRouter.get('/objects/:id', param("id").isInt({min: 0, max: 2147483647}).notEmpty(), cacheMiddleware(120, (req) => {
290290
const category = parseInt(req.query.category as string) || 0;
291291
return !req.query["no-cache"] || category != 4;
292292
}), (req: Request, res: Response) => {
@@ -358,6 +358,7 @@ oRouter.post('/objects/:id/rate',
358358
`;
359359
await pool.query(query, [objectID, accountID, stars]);
360360
res.status(200).json({ message: `Sent rating of ${stars} stars!`});
361+
CacheManager.delete(`GET:/objects/${objectID}`);
361362
} catch (e) {
362363
console.error(e);
363364
res.status(500).json({ error: 'Internal server error' });
@@ -398,6 +399,7 @@ oRouter.post('/objects/:id/comment',
398399
if (!objExists.rows[0].exists) return res.status(404).json({error: "Object not found."});
399400
await pool.query("INSERT INTO comments (object_id, account_id, content) VALUES ($1, $2, $3)", [objectID, accountID, data]);
400401
res.status(200).json({ message: `Sent!`});
402+
CacheManager.delete(`GET:/objects/${objectID}/comments`);
401403
} catch (e) {
402404
console.error(e);
403405
res.status(500).json({ error: 'Internal server error' });
@@ -442,6 +444,7 @@ oRouter.post('/objects/:id/comments/:commentid/pin',
442444
}
443445
await pool.query("UPDATE comments SET pinned = FALSE WHERE object_id = $1", [objectID]);
444446
await pool.query("UPDATE comments SET pinned = TRUE WHERE id = $1", [commentID]);
447+
CacheManager.delete(`GET:/objects/${objectID}/comments`);
445448
res.status(200).json({ message: `Pinned!`});
446449
} catch (e) {
447450
console.error(e);
@@ -493,6 +496,7 @@ oRouter.post('/objects/:id/comments/:commentid/vote',
493496
await pool.query("UPDATE users SET c_likes = ARRAY_APPEND(c_likes, $1) WHERE account_id = $2", [commentID, accountID]);
494497
await pool.query("UPDATE comments SET likes = likes + 2 WHERE id = $1", [commentID]);
495498
}
499+
CacheManager.delete(`GET:/objects/${objectID}/comments`);
496500
res.status(200).json({ message: "Voted!" });
497501
} else {
498502
if (like == 0) { // dislike
@@ -502,6 +506,7 @@ oRouter.post('/objects/:id/comments/:commentid/vote',
502506
await pool.query("UPDATE users SET c_likes = ARRAY_APPEND(c_likes, $1) WHERE account_id = $2", [commentID, accountID]);
503507
await pool.query("UPDATE comments SET likes = likes + 1 WHERE id = $1", [commentID]);
504508
}
509+
CacheManager.delete(`GET:/objects/${objectID}/comments`);
505510
res.status(200).json({ message: "Voted!" });
506511
}
507512
} catch (e) {
@@ -549,6 +554,7 @@ oRouter.post('/objects/:id/comments/:commentid/delete',
549554
if (!commentExists.rows[0].exists) return res.status(404).json({error: "Comment not found."});
550555
}
551556
await pool.query("DELETE FROM comments WHERE id = $1", [commentID]);
557+
CacheManager.delete(`GET:/objects/${objectID}/comments`);
552558
res.status(200).json({ message: `Deleted!`});
553559
} catch (e) {
554560
console.error(e);
@@ -722,6 +728,8 @@ oRouter.post('/objects/:id/delete',
722728
}
723729
await pool.query("DELETE FROM objects WHERE id = $1", [objectID]);
724730
res.status(200).json({ message: "Deleted object!" });
731+
CacheManager.delete(`GET:/objects/${objectID}`);
732+
CacheManager.deletePattern(`GET:/objects`);
725733
} catch (e) {
726734
console.error(e);
727735
res.status(500).json({ error: 'Internal server error' });
@@ -788,6 +796,7 @@ oRouter.post('/objects/:id/update',
788796
if (!objExists.rows[0].exists) return res.status(404).json({error: "Object not found."});
789797
await pool.query(query, [name, description, tags, objectID]);
790798
}
799+
CacheManager.delete(`GET:/objects/${objectID}`);
791800
return res.status(200).json({message: "Updated object!"});
792801
} catch (e) {
793802
console.error(e)
@@ -942,6 +951,8 @@ oRouter.post('/objects/:id/feature',
942951
await pool.query("UPDATE objects SET featured = 1 WHERE id = $1", [objectID]);
943952
res.status(200).json({ message: "Featured!" });
944953
}
954+
CacheManager.delete(`GET:/objects/${objectID}`);
955+
CacheManager.deletePattern(`GET:/objects`);
945956
} catch (e) {
946957
console.error(e);
947958
res.status(500).json({ error: 'Internal server error' });
@@ -959,7 +970,7 @@ oRouter.post('/objects/:id/feature',
959970
oRouter.get('/user/:id',
960971
param('id').isInt({min: 0, max: 2147483647}).notEmpty(),
961972
query('page').isInt({min: 0, max: 2147483647}).optional(),
962-
cacheMiddleware(300, (req) => {
973+
cacheMiddleware(60, (req) => {
963974
return !req.query["no-cache"];
964975
}),
965976
async (req: Request, res: Response) => {
@@ -1306,9 +1317,6 @@ oRouter.post('/user/@me/favorites',
13061317
body('token').notEmpty().isString(),
13071318
query('page').isInt({min: 1, max: 2147483647}).optional(),
13081319
query('limit').isInt({min: 1, max: 9}).optional(),
1309-
cacheMiddleware(300, (req) => {
1310-
return !req.query["no-cache"];
1311-
}),
13121320
async (req: Request, res: Response) => {
13131321
const result = validationResult(req);
13141322
if (!result.isEmpty()) return res.status(400).json({ errors: result.array() })
@@ -1379,7 +1387,7 @@ oRouter.get('/objects',
13791387
query('category').isInt({min: 0, max: 2147483647}).optional(),
13801388
query('limit').isInt({min: 1, max: 9}).optional(),
13811389
body('tags').optional().isString(),
1382-
cacheMiddleware(300, (req) => {
1390+
cacheMiddleware(60, (req) => {
13831391
const category = parseInt(req.query.category as string) || 0;
13841392
return !req.query["no-cache"] || category == 4;
13851393
}),
@@ -1517,6 +1525,9 @@ oRouter.post('/objects/search',
15171525
query('page').isInt({min: 1, max: 2147483647}).optional(),
15181526
query('limit').isInt({min: 1, max: 9}).optional(),
15191527
body('tags').optional().isString(),
1528+
cacheMiddleware(10, (req) => {
1529+
return !req.query["no-cache"]
1530+
}),
15201531
async (req: Request, res: Response) => {
15211532
const result = validationResult(req);
15221533
if (!result.isEmpty()) return res.status(400).json({ errors: result.array() })

0 commit comments

Comments
 (0)