page_cache.js 2.29 KB
/**
 *
 * @author: chenfeng<feng.chen@yoho.cn>
 * @date: 16/10/13
 */

'use strict';

import Router from 'koa-router';
import sh from 'shelljs';
import {
    PageCache
} from '../../models';

const r = new Router();

const pageCahe = {
    async query(ctx) {
        let servers = await PageCache.findAll();
        let awsCount = 0;
        let awsList = '';
        let qCloudCount = 0;
        let qCloudList = '';
        servers.forEach((server) => {
            if (server.server === 'AWS') {
                awsCount++;
                awsList += server.host + ',';
            } else if (server.server === 'qCloud') {
                qCloudCount++;
                qCloudList += server.host + ',';
            }
        });
        let data = {
            storeList: [{
                tableName: 'ngx_cache_pc',
                name: 'PC'
            }, {
                tableName: 'ngx_cache_wap',
                name: 'Wap'
            }],
            serverList: [{
                name: 'AWS',
                value: 'AWS',
                list: awsList,
                count: awsCount
            }, {
                name: 'qCloud',
                value: 'qCloud',
                list: qCloudList,
                count: qCloudCount
            }],
            count: 0
        };
        await ctx.render('action/page_cache', data);
    },
    async clear(ctx) {
        let queryUris = ctx.request.body.query_uri; 
        let storeTableName = ctx.request.body.table_name;
        let serverType = ctx.request.body.server;
        let servers = serverType.split(',').filter(server => server);
        if (queryUris && storeTableName && servers.length) {
            PageCache.removeCache(queryUris, storeTableName, servers);
        }
        return ctx.body = {
            code: 200
        };
    },
    async clearAll(ctx) {
        let storeTableName = ctx.request.body.table_name;
        let serverType = ctx.request.body.server;
        let servers = serverType.split(',').filter(server => server);
        if (storeTableName && servers.length) {
            PageCache.removeAllCache(storeTableName, servers);
        }
        return ctx.body = {
            code: 200
        };
    }
}
r.get('/query', pageCahe.query);
r.post('/clear', pageCahe.clear);
r.post('/clear/all', pageCahe.clearAll);

export default r;