示例#1
0
function listAction()
{
    $config = getConfig();
    $columnsName = [];
    foreach ($_GET['columns'] as $column) {
        if (!empty($column['data'])) {
            $columnsName[] = $column['data'];
        }
    }
    $order = $_GET['order'][0];
    $orderColumn = $order['column'];
    $start = intval($_GET['start']);
    $sqlAssoc = ['table' => ['name' => 'item', 'dbname' => 'db_vktest', 'as' => 'i'], 'query' => ['select' => $columnsName, 'from' => 'item', 'order' => ['column' => $columnsName[$orderColumn], 'dir' => $order['dir']], 'start' => $start, 'length' => $_GET['length']]];
    $mysqli = db_mysqli_connect($sqlAssoc['table']['dbname']);
    $rowStore = db_mysqli_query_fetch_store($mysqli, 'SELECT s.countitems FROM store AS s WHERE s.idstore = 1', MYSQLI_ASSOC)[0];
    $sqlQueryes = buildListItemQuery($sqlAssoc, $rowStore['countitems']);
    $rows = db_mysqli_query_fetch_list($mysqli, $sqlQueryes, MYSQLI_ASSOC);
    db_mysqli_close($mysqli);
    //    var_dump($_SESSION);
    //    var_dump($sqlQueryes);
    $_SESSION['list'] = ['lastitem' => $rows[count($rows) - 1], 'firstitem' => $rows[0], 'lastpage' => $start, 'slowQueryType' => $sqlQueryes['slowQueryType']];
    //for cahce before last result
    afterLictAction($sqlAssoc, $rowStore);
    $response = ['recordsTotal' => $rowStore['countitems'], 'recordsFiltered' => $rowStore['countitems'], 'data' => $rows, 'draw' => $_GET['draw'], 'start' => $_GET['start']];
    echo json_encode($response);
}
function afterLictAction($queryAssoc, $rowStore)
{
    $config = getConfig();
    /**
     * Count of items page,that need be cached,because
     * but last-1/-2/-3/-4 use slow query
     */
    $COUNT_BEFORE_END = 5;
    $length = $queryAssoc['query']['length'];
    $itemsLast = $rowStore['countitems'] % $length;
    $lastPage = $rowStore['countitems'] - $itemsLast;
    $start = $queryAssoc['query']['start'];
    /**
     * only for last page
     */
    if ($start == $lastPage) {
        //last 5 page in cache&
        $queryCheckPre = array_merge($queryAssoc, []);
        $queryCheckPre['query']['start'] = $start - 1 * $length;
        $queryPre = buildListItemQuery($queryCheckPre, $rowStore['countitems']);
        $params = $config['memcache'];
        $memcache = memcache_connect($params['host'], $params['port']);
        $memcacheQueryKey = 'QUERY' . $queryPre['slow'];
        $data = memcache_get($memcache, $memcacheQueryKey);
        memcache_close($memcache);
        //another proc
        if (empty($data)) {
            $pid = pcntl_fork();
            if ($pid == 0) {
                $mysqli = db_mysqli_connect($queryAssoc['table']['dbname']);
                for ($i = 1; $i < $COUNT_BEFORE_END; $i++) {
                    $queryAssoc['query']['start'] = $start - $i * $length;
                    $sqlQueryes = buildListItemQuery($queryAssoc, $rowStore['countitems']);
                    $rows = db_mysqli_query_fetch_list($mysqli, $sqlQueryes, MYSQLI_ASSOC);
                    $_SESSION['list'] = ['lastitem' => $rows[count($rows) - 1], 'firstitem' => $rows[0], 'lastpage' => $queryAssoc['query']['start'], 'slowQueryType' => $sqlQueryes['slowQueryType']];
                    //
                    usleep(500);
                }
                db_mysqli_close($mysqli);
                exit(0);
                //end new proc
            }
        }
    }
}