原文链接: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. JVM内存模型和垃圾回收

    Java开发有个很基础的问题,虽然我们平时接触的不多,但是了解它却成为Java开发的必备基础——这就是JVM.在C++中我们需要手动申请内存然后释放内存,否则就会出现对象已经不再使用内存却仍被占用的情 ...

  2. IOS贝塞尔曲线圆形进度条和加载动画

    做项目让做一个加载动画,一个圈圈在转中间加一个图片,网上有好多demo,这里我也自己写了一个,中间的图片可加可不加.其中主要用到贝塞尔曲线.UIBezierPath是对CGContextRef的进一步 ...

  3. DataGridView 绑定数据方法

    DataGridView控件用于显示来自多种外部数据源中的数据,用户可以在此控件添加行和列,并可以填充数据.   如要让DataGridView显示数据库中的数据,只需要将此控件绑定到挑用数据库的数据 ...

  4. jQuery基础---动画效果

    内容摘要: 1.显示.隐藏 2.滑动.卷动 3.淡入.淡出 4.自定义动画 5.列队动画方法 6.动画相关方法 7.动画全局属性  发文不易,转载请注明出处~ 一.显示.隐藏 jQuery 中显示方法 ...

  5. 基于标注的AOP面向切面编程

    1.什么是AOP Aspect  Orientied   Programming的简称,即 面向(方面)切面编程 ,不改变一个组件源代码的情况下 可以对组件功能进行增强. 例如:servlet中的过滤 ...

  6. html全局属性(收藏)

    HTML 属性赋予元素意义和语境. 下面的全局属性可用于任何 HTML 元素. 参考链接:http://www.w3school.com.cn/tags/html_ref_standardattrib ...

  7. HDU 2544(简单最短路)

    http://acm.hdu.edu.cn/showproblem.php?pid=2544 /* 使用pair代替结构 */ #include <iostream> #include & ...

  8. C++11:实用特性

    今天逛cplusplus.com发现C++还真多了不少方便使用的特性,先了解些最常用的 初始化列表 vector<,,,}); vector<pair<int, int> &g ...

  9. java 自定义泛型

    package com.direct.demo; import java.util.ArrayList; import java.util.HashMap; import java.util.Link ...

  10. 关于html 中form表单的内标签和使用

    表单标记 1.普通文本框: <input type=”text” name=”名称” value=”值”;不写value默认为空/> 2.密码框:<input type=”passw ...