- setCachedRoute (req: express.Request, body: any, contentType?: string, statusCode?: number) {
- const cached: CachedRoute = {
- body: body.toString(),
- contentType,
- statusCode: statusCode.toString()
+ setCachedRoute (req: express.Request, body: any, lifetime: number, contentType?: string, statusCode?: number) {
+ const cached: CachedRoute = Object.assign(
+ {},
+ { body: body.toString() },
+ (contentType) ? { contentType } : null,
+ (statusCode) ? { statusCode: statusCode.toString() } : null
+ )
+
+ return this.setObject(this.generateCachedRouteKey(req), cached, lifetime)
+ }
+
+ /* ************ Video views ************ */
+
+ addVideoView (videoId: number) {
+ const keyIncr = this.generateVideoViewKey(videoId)
+ const keySet = this.generateVideosViewKey()
+
+ return Promise.all([
+ this.addToSet(keySet, videoId.toString()),
+ this.increment(keyIncr)
+ ])
+ }
+
+ async getVideoViews (videoId: number, hour: number) {
+ const key = this.generateVideoViewKey(videoId, hour)
+
+ const valueString = await this.getValue(key)
+ const valueInt = parseInt(valueString, 10)
+
+ if (isNaN(valueInt)) {
+ logger.error('Cannot get videos views of video %d in hour %d: views number is NaN (%s).', videoId, hour, valueString)
+ return undefined