原文链接: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

PHP: APC Configuration and Usage Tips and Tricks的更多相关文章

  1. Nginx and PHP-FPM Configuration and Optimizing Tips and Tricks

    原文链接:http://www.if-not-true-then-false.com/2011/nginx-and-php-fpm-configuration-and-optimizing-tips- ...

  2. 10 Essential TypeScript Tips And Tricks For Angular Devs

    原文: https://www.sitepoint.com/10-essential-typescript-tips-tricks-angular/ ------------------------- ...

  3. (转) How to Train a GAN? Tips and tricks to make GANs work

    How to Train a GAN? Tips and tricks to make GANs work 转自:https://github.com/soumith/ganhacks While r ...

  4. Matlab tips and tricks

    matlab tips and tricks and ... page overview: I created this page as a vectorization helper but it g ...

  5. LoadRunner AJAX TruClient协议Tips and Tricks

    LoadRunner AJAX TruClient协议Tips and Trickshttp://automationqa.com/forum.php?mod=viewthread&tid=2 ...

  6. Android Studio tips and tricks 翻译学习

    Android Studio tips and tricks 翻译 这里是原文的链接. 正文: 如果你对Android Studio和IntelliJ不熟悉,本页提供了一些建议,让你可以从最常见的任务 ...

  7. Tips and Tricks for Debugging in chrome

    Tips and Tricks for Debugging in chrome Pretty print On sources panel ,clicking on the {} on the bot ...

  8. [转]Tips——Chrome DevTools - 25 Tips and Tricks

    Chrome DevTools - 25 Tips and Tricks 原文地址:https://www.keycdn.com/blog/chrome-devtools 如何打开? 1.从浏览器菜单 ...

  9. WWDC笔记:2011 Session 125 UITableView Changes, Tips and Tricks

    What’s New Automatic Dimensions - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSect ...

随机推荐

  1. 有关查询和执行计划的DMV

    转自:http://www.cnblogs.com/CareySon/archive/2012/05/17/2506035.html 查看被缓存的查询计划 SET TRANSACTION ISOLAT ...

  2. SVD总结

    1.概述 我们先从实数域R开始说起,再延伸到复数域C上去,先列出一个表格,把实数域以及复数域中常见的矩阵及其性质概括如下: 表1 常见矩阵及其性质 我们知道实对称矩阵正交相似于对角阵,从而将一个方阵对 ...

  3. 微信授权获取code(微信支付)

    摘要:最近在做h5支付,然后发现一个问题,微信自带浏览器不支持h5支付,然后后台又做了一个微信支付的接口,然后要传code参数,代码写好总结后,就发到这里记录一下: 因为有两个支付接口,所以首先判断打 ...

  4. node错误集合

    1.端口被占用 node .\app.js events.js:167 throw er; // Unhandled 'error' even 解决办法:8888端口被占用了,更改一个端口就好 2. ...

  5. C# OO(初级思想)。

    继承,多态,封装 在C#中,为了能够合理描述自然界的规律,面向对象的编程引入了继承的概念,是面向对象编程中最重要的概念之一,定义了如何根据现有的类创建新类的过程. 继承:一个类派生出来的子类具有这个类 ...

  6. Java线程入门第二篇

    Java线程通信方法 0.(why)每个线程都有自己的栈空间,我们要线程之间进行交流,合作共赢. 1.synchronized和volatile关键字 a)  看下面的synchronized关键字 ...

  7. 进度监视器--ProgressMonitorInputStream

    进度监视器--ProgressMonitorInputStream ProgressMonitorInputStream 可以创建一个进度监视器,以监视读取输入流的进度.如果需要一段时间,将会弹出 P ...

  8. 【C++并发实战】(三) std::future和std::promise

    std::future和std::promise std::future std::future期待一个返回,从一个异步调用的角度来说,future更像是执行函数的返回值,C++标准库使用std::f ...

  9. 关于Python的那点吐槽

    之前听到过别人有说过Python只是一个玩具做不了大项目,我当时是嗤之以鼻的,不说豆瓣这样的公司采用Python做的网站,GitHub上那么多大项目都是用Python写的,怎么能说Python只是一个 ...

  10. 洛谷P3939 数颜色(二分 vector)

    题意 题目链接 Sol 直接拿vector维护每种颜色的出现位置,然后二分一下. #include<bits/stdc++.h> using namespace std; const in ...