原文链接: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. Python36和Python27共存的方法

    Python26和Python37环境的配置 设置环境变量 我的电脑右键属性-高级系统属性-环境变量 选择系统变量中的Path,双击打开 加入你的Python安装路径 C:\Python27;C:\P ...

  2. Yii2:记一次尴尬的bug

    创建一个文章模块,写完添加动作之后,分配到视图,发现报错: Exception (Not Supported) 'yii\base\NotSupportedException' with messag ...

  3. Spring 学习(四)--- AOP

    问题 : AOP 解决的问题是什么 Spring AOP 的底层实现是什么 Spring AOP 和 AspectJ 的区别是什么 概述 在软件业,AOP为Aspect Oriented Progra ...

  4. 撩课-Web大前端每天5道面试题-Day5

    1.写一个深度克隆方法(es5)? /** * 深拷贝 * @param {object}fromObj 拷贝的对象 * @param {object}toObj 目标对象 */ function d ...

  5. 图解源码之java锁的获取和释放(AQS)篇

    以独占式不公平锁为例,通过5个线程争夺ReentrantLock的过程,图解ReentrantLock源码实现,了解显示锁的工作流程. 任何时刻拿到锁的只有一个线程,未拿到锁的线程会打包成节点(nod ...

  6. 设计模式之——外观or门面模式

    1.概念 定义一个高层的统一的外观接口类,该接口用于客户端调用,和一个实现类用来包装子系统中多个类,客户端可以通过客户端完成对子系统的方法调用. 2.适用场景 2.1 代码移植,降低了现有系统的复杂度 ...

  7. LinkedList实现队列存储结构

    package com.tercher.demo; import java.util.LinkedList; public class Queue { //用LinkedList 实现队列的数据存储结 ...

  8. js在ie6下的一个bug—未结束标签的错误

    在IE6下,如果在body标签没结束前,用代码获取body对象就会出现错误.如: <html> <head> <script type="text/javasc ...

  9. BZOJ4516: [Sdoi2016]生成魔咒(后缀数组 set RMQ)

    题意 题目链接 Sol 毒瘤SDOI 终于有一道我会做的题啦qwq 首先,本质不同的子串的个数 $ = \frac{n(n + 1)}{2} - \sum height[i]$ 把原串翻转过来,每次就 ...

  10. C# 读写txt文件方法

    添加引用: using System.IO; 1.File类写入文本文件: private void btnTextWrite_Click(object sender, EventArgs e) { ...