This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sudo apt-get install redis-server | |
sudo service redis-server start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var button = document.getElementById("myButton"); | |
button.addEventListener("click", function() { | |
document.body.style.overflow = 'hidden'; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"; |