Skip to content
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

feat:add redis #72

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
# Application port
APP_PORT=3000

# Database type: either 'sqlite' or 'mongodb'
# Database type: either 'sqlite' or 'mongodb' or 'mysql'
DB_TYPE=sqlite

# If using MongoDB, provide the connection string
# DB_URL=mongodb://127.0.0.1:27017

# If using MySQL, provide the connection string
# DB_URL=mysql://127.0.0.1:3306/dbname?user=root&password=root

# Database write interval in seconds (0 for real-time)
DB_INTERVAL=60

Expand Down
6 changes: 6 additions & 0 deletions db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ switch(process.env.DB_TYPE){
case 'mongodb':
db = require('./mongodb')
break;
case 'redis':
db = require('./redis')
break;
case 'mysql':
db = require('./mysql')
break;
case 'sqlite':
default:
db = require('./sqlite')
Expand Down
48 changes: 48 additions & 0 deletions db/mysql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"use strict";

const mysql = require('mysql2/promise');

// the default mysql url (local server)
const mysqlURL = process.env.DB_URL || "mysql://localhost:3306/counter";

let connection;



async function initDb() {
connection = await mysql.createConnection(mysqlURL);
await connection.execute("CREATE TABLE IF NOT EXISTS tb_count (name VARCHAR(32) NOT NULL, num INT NOT NULL, PRIMARY KEY (name)) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
}

async function getNum(name) {
const [rows] = await connection.execute("SELECT num FROM tb_count WHERE name = ?", [name]);
console.log(rows)
return rows[0];
}

async function getAll() {
const [rows] = await connection.execute("SELECT * FROM tb_count");
console.log(rows)
return rows;
}

async function setNum(name, num) {
connection.execute("INSERT INTO tb_count (name, num) VALUES (?, ?) ON DUPLICATE KEY UPDATE num = ?", [name, num, num]);
// await connection.execute("INSERT INTO tb_count (name, num) VALUES (?, ?) ON DUPLICATE KEY UPDATE num = ?", [name, num, num]);
}

async function setNumMulti(counters) {
for (let counter of counters) {
setNum(counter.name, counter.num);
// await setNum(counter.name, counter.num);
}
}

initDb();

module.exports = {
getNum,
getAll,
setNum,
setNumMulti,
};
43 changes: 43 additions & 0 deletions db/redis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"use strict";

const Redis = require("ioredis");

// the default redis url (local server)
const redisURL = process.env.DB_URL || "redis://127.0.0.1:6379";
const redisPrefix = process.env.REDIS_PREFIX || "moe_count_"

const redis = new Redis(redisURL);

async function getNum(name) {
const result = await redis.get(redisPrefix+name)
if(result === null || isNaN(result) ){
return {name:name,num:0}
}
return {name:name,num:parseInt(result)}
}


async function getAll() {
const keys = await redis.keys(redisPrefix+'*');
const values = await redis.mget(keys);
return keys.map((key, i) => ({ name: key, num: values[i] || 0 }))
}

async function setNum(name, num) {
return redis.set(redisPrefix+name, num);
}

async function setNumMulti(counters) {
const pipeline = redis.pipeline()
counters.forEach(({ name, num }) => {
pipeline.set(redisPrefix+name, num)
})
await pipeline.exec()
}

module.exports = {
getNum,
getAll,
setNum,
setNumMulti,
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
"dotenv": "^16.4.5",
"express": "^4.18.2",
"image-size": "^0.8.3",
"ioredis": "^5.4.1",
"mime-types": "^2.1.27",
"mongoose": "^5.9.28",
"mysql2": "^3.11.5",
"pug": "^3.0.0",
"zod": "^3.23.8"
},
Expand Down