Fix redis type issues

This commit is contained in:
Ajay Ramachandran
2020-12-17 00:25:33 -05:00
parent a7f04ad732
commit c8d5cec338
2 changed files with 12 additions and 15 deletions

View File

@@ -2,18 +2,15 @@ import {config} from '../config';
import {Logger} from './logger'; import {Logger} from './logger';
import redis, {Callback} from 'redis'; import redis, {Callback} from 'redis';
let get, set; let exportObject = {
get: (key: string, callback?: Callback<string | null>) => callback(null, undefined),
set: (key: string, value: string, callback?: Callback<string | null>) => callback(null, undefined)
};
if (config.redis) { if (config.redis) {
Logger.info('Connected to redis'); Logger.info('Connected to redis');
const client = redis.createClient(config.redis); const client = redis.createClient(config.redis);
get = client.get; exportObject = client;
set = client.set;
} else {
get = (key: string, callback?: Callback<string | null>) => callback(null, undefined);
set = (key: string, value: string, callback?: Callback<string | null>) => callback(null, undefined);
} }
export { export default exportObject;
get,
set,
};

View File

@@ -1,6 +1,6 @@
import {config} from '../config'; import {config} from '../config';
import {Logger} from './logger'; import {Logger} from './logger';
import * as redis from './redis'; import redis from './redis';
// @ts-ignore // @ts-ignore
import _youTubeAPI from 'youtube-api'; import _youTubeAPI from 'youtube-api';
@@ -18,7 +18,7 @@ export class YouTubeAPI {
} }
const redisKey = "youtube.video." + videoID; const redisKey = "youtube.video." + videoID;
redis.get(redisKey, (getErr: string, result: string) => { redis.get(redisKey, (getErr, result) => {
if (getErr || !result) { if (getErr || !result) {
Logger.debug("redis: no cache for video information: " + videoID); Logger.debug("redis: no cache for video information: " + videoID);
_youTubeAPI.videos.list({ _youTubeAPI.videos.list({
@@ -28,9 +28,9 @@ export class YouTubeAPI {
if (!ytErr) { if (!ytErr) {
// Only set cache if data returned // Only set cache if data returned
if (data.items.length > 0) { if (data.items.length > 0) {
redis.set(redisKey, JSON.stringify(data), (setErr: string) => { redis.set(redisKey, JSON.stringify(data), (setErr) => {
if (setErr) { if (setErr) {
Logger.warn(setErr); Logger.warn(setErr.message);
} else { } else {
Logger.debug("redis: video information cache set for: " + videoID); Logger.debug("redis: video information cache set for: " + videoID);
} }
@@ -45,7 +45,7 @@ export class YouTubeAPI {
}); });
} else { } else {
Logger.debug("redis: fetched video information from cache: " + videoID); Logger.debug("redis: fetched video information from cache: " + videoID);
callback(getErr, JSON.parse(result)); callback(getErr.message, JSON.parse(result));
} }
}); });
}; };