zoukankan      html  css  js  c++  java
  • PHP: APC Configuration and Usage Tips and Tricks

    原文链接:http://www.if-not-true-then-false.com/2012/php-apc-configuration-and-usage-tips-and-tricks/3/

    This PHP APC guide is divided on four different section:
    1. PHP APC Configuration
    2. Enable PHP APC Statistics
    3. Howto Use PHP APC User Cache
    4. PHP APC Performance Testing

     

    3. Howto Use PHP APC User Cache Examples

    3.1 PHP APC User Cache Example with Numeric Values

    Here is an example howto use apc_addapc_casapc_fetchapc_dec and apc_inc:

    <?php
        // Add num variable to data store
        apc_add('num', 1);
     
        // Print initial value
        echo "Initial value: ", apc_fetch('num'), "<br />";
     
        // Update old value with a new value
        apc_cas('num', 1, 10);
     
        // Print just updated value
        echo "Updated value: ", apc_fetch('num'), "<br />";
     
        // Decrease a stored number
        echo "Decrease 1: ", apc_dec('num'), "<br />";
        echo "Decrease 3: ", apc_dec('num', 3), "<br />";
     
        // Increase a stored number
        echo "Increase 2: ", apc_inc('num', 2), "<br />";
        echo "Increase 1: ", apc_inc('num'), "<br />";
    ?>

    Output:

    Initial value: 1
    Updated value: 10
    Decrease 1: 9
    Decrease 3: 6
    Increase 2: 8
    Increase 1: 9
     

    3.2 PHP APC User Cache Example with String

    This example shows howto use apc_fetchapc_existsapc_storeapc_clear_cache:

    <?php
        // Check if str found from cache
        if (apc_exists('str')) {
            // Print str from cache
            echo "str from cache: ", apc_fetch('str'), "<br />";
            // Clear cache
            apc_clear_cache('user');
            // Try to fetch str again
            echo "str from cache, after user cache is cleared: ", "<br />";
            var_dump(apc_fetch('str'));
        }
        else {
            // Save str to cache and set ttl 120 seconds
            echo 'str not found from cache...saving', "<br />";
            $str = "This is just test";
            apc_store('str', $str, 120);
        }
    ?>

    First run output:

    str not found from cache...saving

    Second run output:

    str from cache: This is just test
     

    3.3 PHP APC User Cache Example with Array

    <?php
        // Check if arr found from cache
        if ($arr = apc_fetch('arr')) {
            echo "arr from cache: ", "<br />";
            print_r($arr);
        }
        else {
            echo 'arr not found from cache...saving', "<br />";
            $arr = array('Test 1', 'Test 2', 'Test 3');
            apc_add('arr', $arr, 120);
        }
    ?>

    First run output:

    arr not found from cache...saving

    Second run output:

    arr from cache:
    Array ( [0] => Test 1 [1] => Test 2 [2] => Test 3 )
     

    3.3 PHP APC User Cache Example with Object

    <?php
        // Simple Person class
        class Person {
            private $name;
            private $age;
     
            public function setName($name) {
                $this->name = $name;
            }
     
            public function setAge($age) {
                $this->age = $age;
            }
     
            public function getName() {
                return $this->name;
            }
     
            public function getAge() {
                return $this->age;
            }
        }
     
        // Check if Person object found from cache
        if ($obj = apc_fetch('person')) {
            echo "Person data from cache: ", "<br />";
            echo "Name: ", $obj->getName(), "<br />";
            echo "Age: ", $obj->getAge(), "<br />";
        }
        else {
            echo 'Person data not found from cache...saving', "<br />";
            $obj = new Person;
            $obj->setName('Test Person');
            $obj->setAge(35);
            apc_add('person', $obj, 3600);
        }
    ?>

    First run output:

    Person data not found from cache...saving

    Second run output:

    Person data from cache: 
    Name: Test Person
    Age: 35
     

    This PHP APC guide is divided on four different section:
    1. PHP APC Configuration
    2. Enable PHP APC Statistics
    3. Howto Use PHP APC User Cache
    4. PHP APC Performance Testing

  • 相关阅读:
    Webservice实践(七)CXF 与Spring结合+tomcat发布
    slave have equal MySQL Server UUIDs原因及解决
    VM+CentOS+hadoop2.7搭建hadoop完全分布式集群
    Ubuntu下MySQL主从同步配置
    解决Ubuntu14.04下vi编辑器不能使用方向键和退格键问题
    Ubuntu解压缩zip,tar,tar.gz,tar.bz2
    sersync+rsync原理及部署
    mysqldump备份成压缩包
    linux下如何实现mysql数据库每天自动备份定时备份
    ubuntu mysql 数据库备份以及恢复[命令行]
  • 原文地址:https://www.cnblogs.com/argb/p/3604344.html
Copyright © 2011-2022 走看看