api_cache.js
1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'use strict';
import Router from 'koa-router';
import _ from 'lodash';
import {
MemcachedHost
} from '../../models';
import ApiCache from '../../ci/api_cache';
let r = new Router();
const api_cache = {
async index(ctx) {
let hosts = await MemcachedHost.findAll();
await ctx.render('action/api_cache', {hosts: hosts});
},
async addHost(ctx) {
let q = ctx.request.body;
await MemcachedHost.insert({host: q.host});
return ctx.body = {
code: 200
};
},
async delHost(ctx) {
let q = ctx.request.body;
await MemcachedHost.removeById(q.id);
return ctx.body = {
code: 200
};
},
async cleanKey(ctx) {
let q = ctx.request.body;
let key = q.key;
let hosts = await MemcachedHost.findAll();
_.each(hosts, (h) => {
(new ApiCache(h.host)).clean(key);
});
return ctx.body = {
code: 200
};
},
async flushAll(ctx) {
let hosts = await MemcachedHost.findAll();
_.each(hosts, (h) => {
(new ApiCache(h.host)).flushAll();
});
return ctx.body = {
code: 200
};
}
};
r.get('/', api_cache.index);
r.post('/host/add', api_cache.addHost);
r.post('/host/del', api_cache.delHost);
r.post('/clean', api_cache.cleanKey);
r.post('/flush', api_cache.flushAll);
export default r;