Skip to content

Setup Redis #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ PASSWORD = <MYSQL_SERVER_PASSWORD>
# jwt
JWT_SECRET = 'stack_overflow_api'
JWT_EXPIRES_IN = 1

# redis
REDIS_HOST='redis-19923.c302.asia-northeast1-1.gce.cloud.redislabs.com'
REDIS_PORT=19923
REDIS_PASSWORD='Ov9lLYsVxts6alqucdMFjMdtnqnYjWhA'
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
.env
package-lock.json
.vercel
yarn.lock
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
"express-session": "^1.17.0",
"express-validator": "^6.4.0",
"helmet": "^3.23.3",
"ioredis": "^5.2.3",
"jsonwebtoken": "^8.5.1",
"morgan": "^1.10.0",
"mysql2": "^2.3.2",
"nodemon": "^2.0.15",
"redis": "^4.3.1",
"redis-om": "^0.3.6",
"sequelize": "^6.6.4",
"xss-clean": "^0.1.1"
},
Expand Down
5 changes: 5 additions & 0 deletions src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ const config = {
SECRET: getEnvVariable('JWT_SECRET'),
EXPIRES_IN: +getEnvVariable('JWT_EXPIRES_IN'),
},
REDIS: {
HOST: getEnvVariable('REDIS_HOST'),
PORT: getEnvVariable('REDIS_PORT'),
PASSWORD: getEnvVariable('REDIS_PASSWORD'),
},
};

module.exports = config;
21 changes: 21 additions & 0 deletions src/config/redis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const redis = require('redis');

const config = require('./index');

let client;

(async () => {
client = redis.createClient({
socket: {
host: config.REDIS.HOST,
port: config.REDIS.PORT,
},
password: config.REDIS.PASSWORD,
});

client.on('error', (err) => console.log('Redis Client Error', err));

await client.connect();
})();

module.exports = client;
14 changes: 13 additions & 1 deletion src/services/posts.service.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const db = require('../config/db.config');
const client = require('../config/redis');
const { responseHandler, investApi } = require('../helpers');
const utils = require('../utils');
const {
Expand Down Expand Up @@ -117,7 +118,18 @@ exports.retrieveOne = async (postId, result) => {
};

exports.retrieveAll = async (result) => {
const postsMap = await PostsRepository.retrieveAll();
await client.set('key', 'testing');
const value = await client.get('key');

console.log('value: ', value);

await client.rpush(['frameworks_list', 'ReactJS', 'Angular']);

const frameworksList = await client.get('frameworks_list');

console.log('frameworksList: ', frameworksList);

postsMap = await PostsRepository.retrieveAll();

const postCounts = await PostsRepository.countForAll();

Expand Down
Loading