Skip to content

Instantly share code, notes, and snippets.

View Prosen-Ghosh's full-sized avatar
🎯
Focusing

Prosen Ghosh Prosen-Ghosh

🎯
Focusing
View GitHub Profile
const Redis = require("ioredis");
const redis = new Redis();
app.post("/login", async (req, res) => {
const userId = req.body.userId;
const timestamp = Date.now();
const bitmapKey = `user_login:${userId}`;
const offset = Math.floor(timestamp / (24 * 60 * 60 * 1000));
await redis.setbit(bitmapKey, offset, 1);
const Redis = require("ioredis");
const redis = new Redis();
app.use(async (req, res, next) => {
const userIp = req.ip;
const currentTime = Date.now();
const rateLimit = 100; // Number of requests per hour
const rateLimitPeriod = 3600; // 1 hour
const rateLimitKey = `rate_limit:${userIp}`;
sudo apt-get install redis-server
sudo service redis-server start
const Redis = require("ioredis");
const redis = new Redis();
// Acquire lock
const lock = await redis.set("lock:resource", "locked", "EX", 10, "NX");
if (lock === "OK") {
// Do something with the resource
console.log("Lock acquired");
// Release lock
const Redis = require("ioredis");
const redis = new Redis();
redis.get("page:home").then(cacheData => {
if (cacheData) {
console.log(JSON.parse(cacheData));
} else {
// Fetch data from database
const data = await fetchDataFromDb();
// set cache data for page
const Redis = require("ioredis");
const redis = new Redis();
// Fetch data from database
const data = await fetchDataFromDb();
// set cache data for page
redis.set("page:home", JSON.stringify(data));
const Redis = require("ioredis");
const redis = new Redis();
redis.get("session:user1").then(sessionData => {
console.log(JSON.parse(sessionData));
// Output: { username: "user1", email: "user1@example.com" }
});
const Redis = require("ioredis");
const redis = new Redis();
// set session data for user
redis.set("session:user1", JSON.stringify({
username: "user1",
email: "user1@example.com"
}));
var button = document.getElementById("myButton");
button.addEventListener("click", function() {
document.body.style.overflow = 'hidden';
});
// Disable scrolling
document.getElementById("content-area").style.position = "fixed";
document.getElementById("content-area").style.top = "0";
// Enable scrolling
document.getElementById("content-area").style.position = "relative";
document.getElementById("content-area").style.top = "auto";