Skip to content

广州-肖业翔-同步锁,忽略超时重试 #4

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

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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
44 changes: 44 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const Koa = require('koa');
const Router = require('koa-router');
const redisCli = require('./util/client');
const db = require('./util/db');

const app = new Koa();
const router = new Router();

const PORT = 3000;
const CACHE_KEY = 'cache-top5';

router.get('/', async (ctx, next) => {
await next();
redisCli.set('testcli', '我只是想看看封装的能不能用着了', 'EX', 3600);
ctx.body = `It's OK! ${PORT}`
});

router.get('/cache', async (ctx, next) => {
await next();

let top5 = await redisCli.get(CACHE_KEY);
if(!top5) {
top5 = await db.query('select * from websites limit 5');
console.log('get db: ', top5.length);
if(await redisCli.checkLock(CACHE_KEY)) {
ctx.body = top5;
}
try {
await redisCli.set(CACHE_KEY, JSON.stringify(top5), 'PX', 360000);
} catch (err) {
console.log(`redisCli set err: ${err}`);
ctx.body = top5;
}
}

console.log('get cache: ', top5.length);
ctx.body = top5;
});

app.use(router.routes()).use(router.allowedMethods());

app.listen(PORT, () => {
console.log(`listening http://localhost:${PORT}`);
});
Loading