原文链接: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. spring之validation校验

    对于任何一个应用而言在客户端做的数据有效性验证都不是安全有效的,这时候就要求我们在开发的时候在服务端也对数据的有效性进行验证.SpringMVC自身对数据在服务端的校验有一个比较好的支持,它能将我们提 ...

  2. SSIS教程:创建简单的ETL包 -- 3. 添加日志(Adding Logging)

    Microsoft Integration Services 包含日志记录功能,可通过提供任务和容器事件跟踪监控包执行情况以及进行故障排除. 日志记录功能非常灵活,可以在包级别或在包中的各个任务和容器 ...

  3. RabbitMQ入门教程系列

    https://blog.csdn.net/column/details/18247.html

  4. centos 网络很慢且无法远程登陆的解决办法

    安装了centOS,但是发现网速实在是卡得几乎不能上网,连百度都打不开 后来想到偶然记得有一次看过一段话,说到关闭ipv6,测试来一下,果然有效,关闭来ipv6打开网速飞快. 关闭方法,在/etc/m ...

  5. csu 1365 双向链表模拟超时

    1365: Play with Chain Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 21  Solved: 5[Submit][Status][W ...

  6. HDU 3306 Another kind of Fibonacci ---构造矩阵***

    Another kind of Fibonacci Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  7. bnu 10805 矩形神码的 平面向量的运行

    矩形神码的 Time Limit: 1000ms Memory Limit: 65536KB Special Judge   64-bit integer IO format: %lld      J ...

  8. 如何使Wpf浏览器应用程序被完全信任运行

    原文地址链接:http://blogs.microsoft.co.il/maxim/2008/03/05/how-to-run-wpf-xbap-as-full-trust-application/ ...

  9. Web安全相关(五):SQL注入(SQL Injection)

    简介 SQL注入攻击指的是通过构建特殊的输入作为参数传入Web应用程序,而这些输入大都是SQL语法里的一些组合,通过执行SQL语句进而执行攻击者所要的操作,其主要原因是程序没有细致地过滤用户输入的数据 ...

  10. 多线程拷贝备份文件方法(Windows)

    D:\bat\robocopy.exe D:\backupdata  \\192.168.36.45\BData\  db_20170716.dmp 解释: 微软提供的robocopy.exe命令,默 ...