Enabling PHP APC Request Caching

I wrote my first functional PHP webapp called Heater. It features interactive calendars using the Google Chart and AWS Redshift backend .

Now that it works for me, I started to improve performance. I installed APC and confirmed that it works.

My question is: how do I enable request caching before Redshift?

Here is an example of how I am loading data now:

getRsData.php:



    <?php
        $id=$_GET["id"];
        $action=$_GET["action"];
        $connect = $rec = "";
        $connect = pg_connect('host=myredshift.redshift.amazonaws.com port=5439 dbname=mydbname user=dmourati password=mypasword');
        if ($action == "upload")
                $rec = pg_query($connect,"SELECT date,SUM(upload_count) as upload_count from dwh.mytable where enterprise_id='$id' GROUP BY date");
...

    ?>

> 5 , . , . Redshift APC cron ( ) , . script .

. Google, , , . AWS Linux PHP 5.3 apc-3.1.15.

.

EDIT,

if (!preg_match("/^[0-9]*$/",$id)) {
        $idErr = "Only numbers allowed";
        }

if (empty($_GET["action"])) {
     $actionErr = "Action is required";
   } else {
     $action = test_input($action);
   }

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
+4
2

, APC, , .

($cache_path). redshift , . , , , , db .

/ $rows

<?php
    $id=$_GET["id"];
    $action=$_GET["action"];
    $connect = $rec = "";
    $connect = pg_connect('host=myredshift.redshift.amazonaws.com port=5439 dbname=mydbname user=dmourati password=mypasword');
    if ($action == "upload") {

        $cache_path = "/my_cache_path/upload_count/$id";

        if(!file_exists($cache_path) 
            || date('Y-m-d',filemtime($cache_path)) < date('Y-m-d')
            || false === $rows = unserialize(file_get_contents($cache_path))) {

            $rows = array();

            $rec = pg_query($connect,"SELECT date,SUM(upload_count) as upload_count from dwh.mytable where enterprise_id='$id' GROUP BY date");
            while($r = pg_fetch_assoc($rec)) {
                $rows[] = $r;
            }

            file_put_contents($cache_path,serialize($rows));
        }
    }
?>
+2

, , FastCache

http://www.phpfastcache.com/

apc .

    <?php
    // In your config file
    include("phpfastcache/phpfastcache.php");
    phpFastCache::setup("storage","auto");

    // phpFastCache support "apc", "memcache", "memcached", "wincache" ,"files", "sqlite" and    "xcache"
    // You don't need to change your code when you change your caching system. Or simple keep it auto
    $cache = phpFastCache();

    // In your Class, Functions, PHP Pages
    // try to get from Cache first. product_page = YOUR Identity Keyword
    $products = $cache->get("product_page");

    if($products == null) {
        $products = YOUR DB QUERIES || GET_PRODUCTS_FUNCTION;
        // set products in to cache in 600 seconds = 10 minutes
        $cache->set("product_page", $products,600);
    }

    // Output Your Contents $products HERE
?>

http://www.phpfastcache.com/

, , !

+1

Source: https://habr.com/ru/post/1541984/


All Articles