Cache AP video route for 5 seconds
This commit is contained in:
parent
8a2db2e8cb
commit
fd4484f19e
|
@ -3,7 +3,7 @@ import * as express from 'express'
|
||||||
import { VideoPrivacy } from '../../../shared/models/videos'
|
import { VideoPrivacy } from '../../../shared/models/videos'
|
||||||
import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub'
|
import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub'
|
||||||
import { pageToStartAndCount } from '../../helpers/core-utils'
|
import { pageToStartAndCount } from '../../helpers/core-utils'
|
||||||
import { ACTIVITY_PUB, CONFIG } from '../../initializers'
|
import { ACTIVITY_PUB, CONFIG, ROUTE_CACHE_LIFETIME } from '../../initializers'
|
||||||
import { buildVideoAnnounce } from '../../lib/activitypub/send'
|
import { buildVideoAnnounce } from '../../lib/activitypub/send'
|
||||||
import { audiencify, getAudience } from '../../lib/activitypub/send/misc'
|
import { audiencify, getAudience } from '../../lib/activitypub/send/misc'
|
||||||
import { createActivityData } from '../../lib/activitypub/send/send-create'
|
import { createActivityData } from '../../lib/activitypub/send/send-create'
|
||||||
|
@ -17,6 +17,7 @@ import { VideoModel } from '../../models/video/video'
|
||||||
import { VideoChannelModel } from '../../models/video/video-channel'
|
import { VideoChannelModel } from '../../models/video/video-channel'
|
||||||
import { VideoCommentModel } from '../../models/video/video-comment'
|
import { VideoCommentModel } from '../../models/video/video-comment'
|
||||||
import { VideoShareModel } from '../../models/video/video-share'
|
import { VideoShareModel } from '../../models/video/video-share'
|
||||||
|
import { cacheRoute } from '../../middlewares/cache'
|
||||||
|
|
||||||
const activityPubClientRouter = express.Router()
|
const activityPubClientRouter = express.Router()
|
||||||
|
|
||||||
|
@ -34,6 +35,7 @@ activityPubClientRouter.get('/accounts?/:name/following',
|
||||||
)
|
)
|
||||||
|
|
||||||
activityPubClientRouter.get('/videos/watch/:id',
|
activityPubClientRouter.get('/videos/watch/:id',
|
||||||
|
executeIfActivityPub(asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS))),
|
||||||
executeIfActivityPub(asyncMiddleware(videosGetValidator)),
|
executeIfActivityPub(asyncMiddleware(videosGetValidator)),
|
||||||
executeIfActivityPub(asyncMiddleware(videoController))
|
executeIfActivityPub(asyncMiddleware(videoController))
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import * as express from 'express'
|
import * as express from 'express'
|
||||||
import { CONFIG, FEEDS } from '../initializers/constants'
|
import { CONFIG, FEEDS, ROUTE_CACHE_LIFETIME } from '../initializers/constants'
|
||||||
import { asyncMiddleware, feedsValidator, setDefaultSort, videosSortValidator } from '../middlewares'
|
import { asyncMiddleware, feedsValidator, setDefaultSort, videosSortValidator } from '../middlewares'
|
||||||
import { VideoModel } from '../models/video/video'
|
import { VideoModel } from '../models/video/video'
|
||||||
import * as Feed from 'pfeed'
|
import * as Feed from 'pfeed'
|
||||||
|
@ -12,8 +12,8 @@ const feedsRouter = express.Router()
|
||||||
feedsRouter.get('/feeds/videos.:format',
|
feedsRouter.get('/feeds/videos.:format',
|
||||||
videosSortValidator,
|
videosSortValidator,
|
||||||
setDefaultSort,
|
setDefaultSort,
|
||||||
|
asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)),
|
||||||
asyncMiddleware(feedsValidator),
|
asyncMiddleware(feedsValidator),
|
||||||
asyncMiddleware(cacheRoute),
|
|
||||||
asyncMiddleware(generateFeed)
|
asyncMiddleware(generateFeed)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -42,6 +42,13 @@ const OAUTH_LIFETIME = {
|
||||||
REFRESH_TOKEN: 1209600 // 2 weeks
|
REFRESH_TOKEN: 1209600 // 2 weeks
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const ROUTE_CACHE_LIFETIME = {
|
||||||
|
FEEDS: 1000 * 60 * 15, // 15 minutes
|
||||||
|
ACTIVITY_PUB: {
|
||||||
|
VIDEOS: 1000 * 5 // 5 seconds
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
// Number of points we add/remove after a successful/bad request
|
// Number of points we add/remove after a successful/bad request
|
||||||
|
@ -413,8 +420,7 @@ const OPENGRAPH_AND_OEMBED_COMMENT = '<!-- open graph and oembed tags -->'
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
const FEEDS = {
|
const FEEDS = {
|
||||||
COUNT: 20,
|
COUNT: 20
|
||||||
CACHE_LIFETIME: 1000 * 60 * 15 // 15 minutes
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
@ -458,6 +464,7 @@ export {
|
||||||
FOLLOW_STATES,
|
FOLLOW_STATES,
|
||||||
SERVER_ACTOR_NAME,
|
SERVER_ACTOR_NAME,
|
||||||
PRIVATE_RSA_KEY_SIZE,
|
PRIVATE_RSA_KEY_SIZE,
|
||||||
|
ROUTE_CACHE_LIFETIME,
|
||||||
SORTABLE_COLUMNS,
|
SORTABLE_COLUMNS,
|
||||||
FEEDS,
|
FEEDS,
|
||||||
NSFW_POLICY_TYPES,
|
NSFW_POLICY_TYPES,
|
||||||
|
|
|
@ -2,7 +2,7 @@ import * as express from 'express'
|
||||||
import { createClient, RedisClient } from 'redis'
|
import { createClient, RedisClient } from 'redis'
|
||||||
import { logger } from '../helpers/logger'
|
import { logger } from '../helpers/logger'
|
||||||
import { generateRandomString } from '../helpers/utils'
|
import { generateRandomString } from '../helpers/utils'
|
||||||
import { CONFIG, FEEDS, USER_PASSWORD_RESET_LIFETIME, VIDEO_VIEW_LIFETIME } from '../initializers'
|
import { CONFIG, USER_PASSWORD_RESET_LIFETIME, VIDEO_VIEW_LIFETIME } from '../initializers'
|
||||||
|
|
||||||
type CachedRoute = {
|
type CachedRoute = {
|
||||||
body: string,
|
body: string,
|
||||||
|
@ -67,14 +67,14 @@ class Redis {
|
||||||
return cached as CachedRoute
|
return cached as CachedRoute
|
||||||
}
|
}
|
||||||
|
|
||||||
setCachedRoute (req: express.Request, body: any, contentType?: string, statusCode?: number) {
|
setCachedRoute (req: express.Request, body: any, lifetime: number, contentType?: string, statusCode?: number) {
|
||||||
const cached: CachedRoute = {
|
const cached: CachedRoute = {
|
||||||
body: body.toString(),
|
body: body.toString(),
|
||||||
contentType,
|
contentType,
|
||||||
statusCode: statusCode.toString()
|
statusCode: statusCode.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.setObject(this.buildCachedRouteKey(req), cached, FEEDS.CACHE_LIFETIME)
|
return this.setObject(this.buildCachedRouteKey(req), cached, lifetime)
|
||||||
}
|
}
|
||||||
|
|
||||||
listJobs (jobsPrefix: string, state: string, mode: 'alpha', order: 'ASC' | 'DESC', offset: number, count: number) {
|
listJobs (jobsPrefix: string, state: string, mode: 'alpha', order: 'ASC' | 'DESC', offset: number, count: number) {
|
||||||
|
|
|
@ -2,36 +2,39 @@ import * as express from 'express'
|
||||||
import { Redis } from '../lib/redis'
|
import { Redis } from '../lib/redis'
|
||||||
import { logger } from '../helpers/logger'
|
import { logger } from '../helpers/logger'
|
||||||
|
|
||||||
async function cacheRoute (req: express.Request, res: express.Response, next: express.NextFunction) {
|
function cacheRoute (lifetime: number) {
|
||||||
const cached = await Redis.Instance.getCachedRoute(req)
|
return async function (req: express.Request, res: express.Response, next: express.NextFunction) {
|
||||||
|
const cached = await Redis.Instance.getCachedRoute(req)
|
||||||
|
|
||||||
// Not cached
|
// Not cached
|
||||||
if (!cached) {
|
if (!cached) {
|
||||||
logger.debug('Not cached result for route %s.', req.originalUrl)
|
logger.debug('Not cached result for route %s.', req.originalUrl)
|
||||||
|
|
||||||
const sendSave = res.send.bind(res)
|
const sendSave = res.send.bind(res)
|
||||||
|
|
||||||
res.send = (body) => {
|
res.send = (body) => {
|
||||||
if (res.statusCode >= 200 && res.statusCode < 400) {
|
if (res.statusCode >= 200 && res.statusCode < 400) {
|
||||||
Redis.Instance.setCachedRoute(req, body, res.getHeader('content-type').toString(), res.statusCode)
|
const contentType = res.getHeader('content-type').toString()
|
||||||
.catch(err => logger.error('Cannot cache route.', { err }))
|
Redis.Instance.setCachedRoute(req, body, lifetime, contentType, res.statusCode)
|
||||||
|
.catch(err => logger.error('Cannot cache route.', { err }))
|
||||||
|
}
|
||||||
|
|
||||||
|
return sendSave(body)
|
||||||
}
|
}
|
||||||
|
|
||||||
return sendSave(body)
|
return next()
|
||||||
}
|
}
|
||||||
|
|
||||||
return next()
|
if (cached.contentType) res.contentType(cached.contentType)
|
||||||
|
|
||||||
|
if (cached.statusCode) {
|
||||||
|
const statusCode = parseInt(cached.statusCode, 10)
|
||||||
|
if (!isNaN(statusCode)) res.status(statusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.debug('Use cached result for %s.', req.originalUrl)
|
||||||
|
return res.send(cached.body).end()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cached.contentType) res.contentType(cached.contentType)
|
|
||||||
|
|
||||||
if (cached.statusCode) {
|
|
||||||
const statusCode = parseInt(cached.statusCode, 10)
|
|
||||||
if (!isNaN(statusCode)) res.status(statusCode)
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.debug('Use cached result for %s.', req.originalUrl)
|
|
||||||
return res.send(cached.body).end()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
Loading…
Reference in New Issue