cache shortcuts for 600 seconds
This commit is contained in:
@ -2,15 +2,16 @@
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
||||
import cors from 'cors'
|
||||
import express from 'express'
|
||||
import { env } from '../env'
|
||||
import { getClaimsByToken, getTokenByRequest } from '../utils/auth'
|
||||
import { corsConfig } from '../utils/corsConfig'
|
||||
import { logger } from '../utils/logger'
|
||||
import {
|
||||
cacheShortcuts,
|
||||
getShortcuts,
|
||||
getShortcutsCache,
|
||||
resetShortcuts,
|
||||
setShortcuts,
|
||||
} from '../services/user_personalization'
|
||||
import { getClaimsByToken, getTokenByRequest } from '../utils/auth'
|
||||
import { corsConfig } from '../utils/corsConfig'
|
||||
import { logger } from '../utils/logger'
|
||||
|
||||
export function shortcutsRouter() {
|
||||
const router = express.Router()
|
||||
@ -32,9 +33,19 @@ export function shortcutsRouter() {
|
||||
}
|
||||
|
||||
try {
|
||||
const shortcuts = await getShortcuts(claims.uid)
|
||||
const userId = claims.uid
|
||||
const cachedShortcuts = await getShortcutsCache(userId)
|
||||
if (cachedShortcuts) {
|
||||
return res.send({
|
||||
shortcuts: cachedShortcuts,
|
||||
})
|
||||
}
|
||||
|
||||
const shortcuts = await getShortcuts(userId)
|
||||
await cacheShortcuts(userId, shortcuts)
|
||||
|
||||
return res.send({
|
||||
shortcuts: shortcuts ?? [],
|
||||
shortcuts,
|
||||
})
|
||||
} catch (e) {
|
||||
logger.info('error getting shortcuts', e)
|
||||
@ -61,9 +72,12 @@ export function shortcutsRouter() {
|
||||
}
|
||||
|
||||
try {
|
||||
const shortcuts = await setShortcuts(claims.uid, req.body.shortcuts)
|
||||
const userId = claims.uid
|
||||
const shortcuts = await setShortcuts(userId, req.body.shortcuts)
|
||||
await cacheShortcuts(userId, shortcuts)
|
||||
|
||||
return res.send({
|
||||
shortcuts: shortcuts ?? [],
|
||||
shortcuts,
|
||||
})
|
||||
} catch (e) {
|
||||
logger.info('error settings shortcuts', e)
|
||||
@ -89,11 +103,14 @@ export function shortcutsRouter() {
|
||||
}
|
||||
|
||||
try {
|
||||
const success = await resetShortcuts(claims.uid)
|
||||
const userId = claims.uid
|
||||
const success = await resetShortcuts(userId)
|
||||
if (success) {
|
||||
const shortcuts = await getShortcuts(claims.uid)
|
||||
const shortcuts = await getShortcuts(userId)
|
||||
await cacheShortcuts(userId, shortcuts)
|
||||
|
||||
return res.send({
|
||||
shortcuts: shortcuts ?? [],
|
||||
shortcuts,
|
||||
})
|
||||
}
|
||||
} catch (e) {
|
||||
|
||||
@ -203,7 +203,7 @@ export const userDigestEligible = async (uid: string): Promise<boolean> => {
|
||||
return subscriptionsCount >= 2 && libraryItemsCount >= 10
|
||||
}
|
||||
|
||||
const featuresCacheKey = (userId: string) => `features:${userId}`
|
||||
const featuresCacheKey = (userId: string) => `cache:features:${userId}`
|
||||
|
||||
export const getFeaturesCache = async (userId: string) => {
|
||||
const cachedFeatures = await redisDataSource.redisClient?.get(
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
import { DeepPartial, IsNull } from 'typeorm'
|
||||
import { Shortcut, UserPersonalization } from '../entity/user_personalization'
|
||||
import { authTrx } from '../repository'
|
||||
import { findLabelsByUserId } from './labels'
|
||||
import { findSubscriptionById } from './subscriptions'
|
||||
import { DeepPartial } from 'typeorm'
|
||||
import { Filter } from '../entity/filter'
|
||||
import { Subscription, SubscriptionStatus } from '../entity/subscription'
|
||||
import { Shortcut, UserPersonalization } from '../entity/user_personalization'
|
||||
import { redisDataSource } from '../redis_data_source'
|
||||
import { authTrx } from '../repository'
|
||||
import { findLabelsByUserId } from './labels'
|
||||
|
||||
export const findUserPersonalization = async (userId: string) => {
|
||||
return authTrx(
|
||||
@ -173,3 +173,24 @@ const userDefaultShortcuts = async (userId: string): Promise<Shortcut[]> => {
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
const shortcutsCacheKey = (userId: string) => `cache:shortcuts:${userId}`
|
||||
|
||||
export const getShortcutsCache = async (userId: string) => {
|
||||
const cachedShortcuts = await redisDataSource.redisClient?.get(
|
||||
shortcutsCacheKey(userId)
|
||||
)
|
||||
if (!cachedShortcuts) {
|
||||
return undefined
|
||||
}
|
||||
return JSON.parse(cachedShortcuts) as Shortcut[]
|
||||
}
|
||||
|
||||
export const cacheShortcuts = async (userId: string, shortcuts: Shortcut[]) => {
|
||||
await redisDataSource.redisClient?.set(
|
||||
shortcutsCacheKey(userId),
|
||||
JSON.stringify(shortcuts),
|
||||
'EX',
|
||||
600
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user