Detailed introduction to the caching system that comes with PHP: APCu extension

醉折花枝作酒筹
Release: 2023-03-10 13:04:01
forward
3105 people have browsed it

This article will give you a detailed introduction to the caching system that comes with PHP: APCu extension. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Detailed introduction to the caching system that comes with PHP: APCu extension

I believe everyone has used caching systems such as memcached or redis for daily caching, or to resist traffic, or to save some commonly used hotspot data. In fact, in small projects, PHP has also prepared a simple caching system for us, which is fully capable of handling the development of our daily ordinary-scale sites. This set of extensions is the APCu extension.

APCu extension

APCu extension is an upgrade of APC extension, which is no longer maintained. Both sets of extensions are actually based on opcode caching. That is, the caching capability implemented by PHP's own opcode.

The installation of APCu is just like an ordinary PHP extension. It is very simple. The most important thing is that this extension is very small. Both downloading and installation can be completed in seconds. Therefore, it can be very conveniently applied to small-scale projects, and it is natively supported by PHP and does not require additional configuration such as ports.

Method Description

The functions of adding, deleting, querying, and auto-increasing that cache systems generally have are all implemented in the APCu extension.

  • apcu_add — Create a new cache

  • apcu_cache_info —View all cache information of APCu

  • apcu_cas — Update a cached value to a new value

  • apcu_clear_cache — Clear all caches

  • apcu_dec — Decrement the cached value

  • apcu_delete — Delete a cached content

  • apcu_enabled —Whether APCu cache is enabled in the current environment

  • apcu_entry — Atomicly generate a cache entity

  • apcu_exists — Check if cache exists

  • apcu_fetch — Query cache

  • apcu_inc — Auto-increment cache value

  • apcu_sma_info — Query cached shared memory information

  • apcu_store —Save a cache

Usage Demonstration

apcu_add("int", 1);
apcu_add("string", "I'm String");
apcu_add("arr", [1,2,3]);

class A{
    private $apc = 1;
    function test(){
        echo "s";
    }
}

apcu_add("obj", new A);

var_dump(apcu_fetch("int"));
var_dump(apcu_fetch("string"));
var_dump(apcu_fetch("arr"));
var_dump(apcu_fetch("obj"));
Copy after login

Normal use is relatively simple. When we add various types of data, it can be stored in the cache normally. However, it should be noted that we can directly save the object into the APCu cache without serializing it or JSON into a string. The system will automatically serialize it for us.

apcu_add(string \$key , mixed $var [, int $ttl = 0 ]) The method is to add a cache normally. $ttl can set the expiration time, which is also in seconds. If it is not set, it will be Long term effective. Note that the cache time limit of APCu is valid in one CLI. Calling the CLI again will not get the cache content set in the last CLI. In PHP-FPM, the cache will become invalid after restarting PHP-FPM or FastCGI.

Next we focus on testing a few less common methods.

apcu_cas("int", 1, 2);
var_dump(apcu_fetch("int"));

// Warning  apcu_cas() expects parameter 2 to be int
apcu_cas("string", "I'm String", "I'm  New String");
Copy after login

apcu_cas(string $key, int $old, int $new) is to modify an $old value to a $new value. It can only modify the content of numeric type. If it is a string modification, an error will be reported. . What are the advantages of this function? Its biggest advantage is that it is atomic, that is, it is not affected by high concurrency. Similar to it is the apcu_store(string $key, mixed $var [, int $ttl = 0]) method, but this method simply modifies the contents of a cache. If the cache key does not exist, create a new one. It is not restricted by type and is certainly not atomic.

apcu_entry("entry", function($key){
    return "This is " . $key;
});
var_dump(apcu_fetch("entry"));
Copy after login

apcu_entry(string $key , callable $generator [, int $ttl = 0 ]) The function of this function is to execute the anonymous function $generator and change $key if the cache of $key does not exist. Pass it in as a key value, and then generate and return a content as the value of this cache.

var_dump(apcu_cache_info());
Copy after login

Finally, if we want to view all APCu cache information in the current system, we can directly use the apcu_cache_info() function.

Summary

When there is a lot of data in the cache, it also provides an APCUIterator iterator to facilitate us to perform loop queries and related statistics of cache information. In short, this system is a very convenient small-scale caching system, and you can try to use some small functions in daily development.

Test code:

https://github.com/zhangyue0503/dev-blog/blob/master/php/202004/source/%E6%88%91%E4%BB%AC%E4%B9%9F%E6%9C%89%E8%87%AA%E5%B8%A6%E7%9A%84%E7%BC%93%E5%AD%98%E7%B3%BB%E7%BB%9F%EF%BC%9APHP%E7%9A%84APCu%E6%89%A9%E5%B1%95.php
Copy after login

Recommended learning: php video tutorial

The above is the detailed content of Detailed introduction to the caching system that comes with PHP: APCu extension. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!