https://github.com/qianshou/SeckillSolution
https://blog.csdn.net/koastal/article/details/78995885
=====
https://blog.csdn.net/qq_38512763/article/details/118830903
node.js express.js sequelize redis cache
http://liu-xin.me/2017/03/24/%E8%AE%A9%E5%86%99%E5%85%A5%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84%E6%95%B0%E6%8D%AE%E8%87%AA%E5%8A%A8%E5%86%99%E5%85%A5%E7%BC%93%E5%AD%98/
const to = require('await-to-js').default;
const Redis = require('ioredis');
const redis = new Redis(ooxxooxx);
function Cache() {}
Cache.get = async function(key) {
const [err, res] = await to(redis.get(key));
if (err) {console.error('REDIS::Cache::get]', err); return null;}
return JSON.parse(res);
};
Cache.set = async function(key, value) {
value = JSON.stringify(value);
const [err] = await to(redis.set(key, value));
if (err) {console.error('REDIS::Cache::set]', err); }
};
//
// sequelize.query
//
async function CacheSequelizeQuery(sequelize, sql, params, redisKey) {
const cacheData = await Cache.get(redisKey);
if (cacheData) return cacheData; // not empty return cache data
const [err, lists] = await to(sequelize.query(sql, params));
if (err) throw err;
if (lists) await Cache.set(redisKey, lists); // not empty set cache data
return lists;
}
//
// findOneCache
//
Sequelize.Model.findOneCache = async function() {
let redisKey;
for (const parms of Object.values(arguments)) {
if (parms.where) redisKey = [this.name, JSON.stringify(parms.where)].join(':');
}
const cacheValue = await Cache.get(redisKey);
if (cacheValue) return JSON.parse(cacheValue);
const result = await Sequelize.Model.findOne.apply(this, arguments);
Cache.set(redisKey, JSON.stringify(result));
return result;
};
node.js redis special delete del
https://github.com/luin/ioredis
const Redis = require('ioredis');
const redis = new Redis();
const delScript = `
local all_keys = {};
local keys = {};
local done = false;
local cursor = "0"
repeat
local result = redis.call("SCAN", cursor, "match", KEYS[1], "count", KEYS[2])
cursor = result[1];
keys = result[2];
for i, key in ipairs(keys) do
all_keys[#all_keys+1] = key;
end
if cursor == "0" then
done = true;
end
until done
for i, key in ipairs(all_keys) do
redis.call("DEL", key);
end
return true;
`;
redis.defineCommand('del', {
numberOfKeys: 2,
lua: delScript
});
redis.del('login:202007*', 10000, function(error, result) {
console.log('error: ', error);
console.log('result: ', result);
});
nodejs express cache redis
https://sematext.com/blog/expressjs-best-practices/
const express = require('express')
const app = express()
const redis = require('redis')
const redisClient = redis.createClient(6379)
async function getSomethingFromDatabase (req, res, next) {
try {
const { id } = req.params;
const data = await database.query()
// Set data to Redis
redisClient.setex(id, 3600, JSON.stringify(data))
res.status(200).send(data)
} catch (err) {
console.error(err)
res.status(500)
}
}
function cache (req, res, next) {
const { id } = req.params
redisClient.get(id, (err, data) => {
if (err) {
return res.status(500).send(err)
}
// If data exists return the cached value
if (data != null) {
return res.status(200).send(data)
}
// If data does not exist, proceed to the getSomethingFromDatabase function
next()
})
}
app.get('/data/:id', cache, getSomethingFromDatabase)
app.listen(3000, () => console.log(`Server running on Port ${port}`))
[轉]github-重新定位-redis-的功能
https://blog.gslin.org/archives/2017/01/12/7076/github-重新定位-redis-的功能/
==============
https://tech.trivago.com/2017/01/25/learn-redis-the-hard-way-in-production/
==============
https://tech.trivago.com/2017/01/25/learn-redis-the-hard-way-in-production/
訂閱:
文章 (Atom)