DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Data
  4. APC caching with PHP

APC caching with PHP

Andrey Prikaznov user avatar by
Andrey Prikaznov
·
Oct. 29, 11 · Interview
Like (0)
Save
Tweet
Share
7.32K Views

Join the DZone community and get the full member experience.

Join For Free
today i have another interesting article for php. we will talking about caching, and the practice of using caching in php. i will review apc caching and will show you how you can use apc in php . we will prepare a useful class for working with apc for us and several examples. a little of history: long ago, when computer networks and web sites only just appeared, most web sites were static (and all data/files stored in disks). after that, people invented a ‘database’ to store the data. this was a great innovation.

we started saving into the database more and more, even small pieces of data. years passed, the size of sites grew, and people began to notice that the performance of sites were falling under the too frequent reading of data from the database. and we came up with a new optimization strategy – caching (where we store data in the cache files on the server). interesting, isn`t it? this looks like ‘forward, to the past’ :-)

but why did it happen? it’s simple – the speed of hard drives has increased, even outgrown the velocity of the databases. at the moment i am talking about mysql. other types of databases are even faster. but in any case, progress is not standing still. now people have learned to use the server memory for data storage. ram is much faster than hard disk, and the price of memory falls all the time.

today i have prepared an example on using apc with php.

live demo

download in package


now – download the source files and lets start coding !


step 1. php

i made this useful class for you. we will use this class to work with memory using an apc caching system.

classes/apc.caching.php

<?

class cacheapc {

    var $ittl = 600; // time to live
    var $benabled = false; // apc enabled?

    // constructor
    function cacheapc() {
        $this->benabled = extension_loaded('apc');
    }

    // get data from memory
    function getdata($skey) {
        $bres = false;
        $vdata = apc_fetch($skey, $bres);
        return ($bres) ? $vdata :null;
    }

    // save data to memory
    function setdata($skey, $vdata) {
        return apc_store($skey, $vdata, $this->ittl);
    }

    // delete data from memory
    function deldata($skey) {
        $bres = false;
        apc_fetch($skey, $bres);
        return ($bres) ? apc_delete($skey) : true;
    }
}

?>

i prepared here several necessary functions which we will use: getdata, setdata and deldata. now, lets check the first example file:

index.php

<?php

$adata = array(
    'name' => 'table',
    'color' => 'brown',
    'size' => array(
        'x' => 200,
        'y' => 120,
        'z' => 150,
    ),
    'strength' => 10,
);

require_once('classes/apc.caching.php');
$ocache = new cacheapc();

echo 'initial data: <pre>'; // lets see what we have
print_r($adata);
echo '</pre>';

if ($ocache->benabled) { // if apc enabled

    $ocache->setdata('my_object', $adata); // saving data to memory
    $ocache->setdata('our_class_object', $ocache); // saving object of our class into memory too

    echo 'now we saved all in memory, click <a href="ndex2.php">here</a> to check what we have in memory';

} else {
    echo 'seems apc not installed, please install it to perform tests';
}

?>

in this file you can see that i am saving 2 objects in memory: some predefined array and class object. now, lets check second example file:

index2.php

<?php

require_once('classes/apc.caching.php');
$ocache = new cacheapc();

if ($ocache->benabled) { // if apc enabled

    $amemdata = $ocache->getdata('my_object'); // getting data from memory
    $amemdata2 = $ocache->getdata('our_class_object'); // getting data from memory about our class

    echo 'data from memory: <pre>'; // lets see what we have from memory
    print_r($amemdata);
    echo '</pre>';

    echo 'data from memory of object of cacheapc class: <pre>';
    print_r($amemdata2);
    echo '</pre>';

    echo 'as you can see - all data read successfully, now lets remove data from memory and check results, click <a href="index3.php">here</a> to continue';

} else {
    echo 'seems apc not installed, please install it to perform tests';
}

?>

here we are only reading data from memory. and, as we see – all data is successfully read from the memory. now, lets check last example file:

index3.php

<?php

require_once('classes/apc.caching.php');
$ocache = new cacheapc();

if ($ocache->benabled) { // if apc enabled

    $ocache->deldata('my_object'); // removing data from memory
    $ocache->deldata('our_class_object'); // removing data from memory

    $amemdata = $ocache->getdata('my_object'); // lets try to get data again
    $amemdata2 = $ocache->getdata('our_class_object');

    echo 'data from memory: <pre>'; // lets see what we have from memory
    print_r($amemdata);
    echo '</pre>';

    echo 'data from memory of object of cacheapc class: <pre>';
    print_r($amemdata2);
    echo '</pre>';

    echo 'as you can see - all data successfully removed. great !';

} else {
    echo 'seems apc not installed, please install it to perform tests';
}

?>
live demo
download in archive

conclusion

today, i told you how we can use the server’s memory with apc caching. i hope you share any thoughts on optimizing your website(s). for example you can cache some frequently-used data (information about users on your system, or some settings of the website etc.) good luck in your work!




source:
http://www.script-tutorials.com/how-to-use-apc-caching-with-php/

Cache (computing) PHP

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A Beginner’s Guide To Styling CSS Forms
  • Seamless Integration of Azure Functions With SQL Server: A Developer's Perspective
  • Fixing Bottlenecks in Your Microservices App Flows
  • Introduction to Containerization

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: