Table of Contents
What is Yac" >What is Yac
Basic operations" >Basic operations
Add and get cache
Set cache
Delete cache
Alias ​​space
Cache aging
Summary" >Summary
Home Backend Development PHP Problem Detailed introduction to Yac, another efficient caching extension for PHP

Detailed introduction to Yac, another efficient caching extension for PHP

Jun 03, 2021 pm 05:42 PM
php

This article will give you a detailed introduction to Yac, another efficient cache extension for PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Detailed introduction to Yac, another efficient caching extension for PHP

In the previous article, we have learned about an extension cache Apc that comes with PHP. Today we will learn about another cache extension: Yac.

What is Yac

It can be seen from the name that this is another work of the master Niao Ge. After all, he is the core developer of PHP and his work never disappoints us every time. Brother Niao can be said to be the pride of our Chinese programmers. He plays a decisive role in the PHP world. You can search his blog yourself. Although the update frequency is not high, every article is worth learning.

Yac is a lock-free shared cache system. Because it is lock-free, it is very efficient. Apc is said to be more than twice as efficient as Memcached, while Yac is faster than Apc. This is its biggest feature.

Compared with Memcached or Redis, Yac is more lightweight. We don’t need to install any other software in the server. We only need to install this extension to use it. For small systems, especially systems that simply cache data, we do not need complex data types. Just using this extension of the programming language can make our development more convenient and faster.

The installation method is also very simple. Just download the installation package from PECL and then install the extension.

Basic operations

For cache-related operations, they are nothing more than adding, modifying, and deleting cache. Unlike external caching systems, when saving arrays or objects, the cache of PHP extension classes can directly save these data types without serializing them into strings or converting them into JSON strings. This is one of the advantages of Apc and Yac.

Add and get cache

$yac = new Yac();
$yac->add('a', 'value a');
$yac->add('b', [1,2,3,4]);

$obj = new stdClass;
$obj->v = 'obj v';
$yac->add('obj', $obj);


echo $yac->get('a'), PHP_EOL; // value a
echo $yac->a, PHP_EOL; // value a


print_r($yac->get('b'));
// Array
// (
//     [0] => 1
//     [1] => 2
//     [2] => 3
//     [3] => 4
// )

var_dump($yac->get('obj'));
// object(stdClass)#3 (1) {
//     ["v"]=>
//     string(5) "obj v"
// }
Copy after login

Very simple operation, we only need to instantiate a Yac class, and then we can add and get cache content through the add() method and get() method.

Yac extension also overrides the __set() and __get() magic methods, so we can directly operate the cache by operating variables.

Next, we can view the current cached status information through the info() function.

print_r($yac->info());
// Array
// (
//     [memory_size] => 71303168
//     [slots_memory_size] => 4194304
//     [values_memory_size] => 67108864
//     [segment_size] => 4194304
//     [segment_num] => 16
//     [miss] => 0
//     [hits] => 4
//     [fails] => 0
//     [kicks] => 0
//     [recycles] => 0
//     [slots_size] => 32768
//     [slots_used] => 3
// )
Copy after login

Set cache

$yac->set('a', 'new value a!');
echo $yac->a, PHP_EOL; // new value a!

$yac->a = 'best new value a!';
echo $yac->a, PHP_EOL; // best new value a!
Copy after login

The function of the set() function is to modify the content of the cache if the current cache key exists. If it does not exist, create a cache.

Delete cache

$yac->delete('a');
echo $yac->a, PHP_EOL; // 

$yac->flush();
print_r($yac->info());
// Array
// (
//     [memory_size] => 71303168
//     [slots_memory_size] => 4194304
//     [values_memory_size] => 67108864
//     [segment_size] => 4194304
//     [segment_num] => 16
//     [miss] => 1
//     [hits] => 6
//     [fails] => 0
//     [kicks] => 0
//     [recycles] => 0
//     [slots_size] => 32768
//     [slots_used] => 0
// )
Copy after login

For deletion of a single cache, we can directly use the delete() function to delete the contents of this cache. If you want to clear the entire cache space, you can directly use flush() to clear the entire cache space.

Alias ​​space

We mentioned the cache space above. In fact, when instantiating Yac, you can pass an alias configuration to the default Yac class constructor. In this way, different Yac instances are equivalent to being placed in different namespaces, and caches of the same Key in different spaces will not affect each other.

$yacFirst = new Yac();
$yacFirst->a = 'first a!';;

$yacSecond = new Yac();
$yacSecond->a = 'second a!';

echo $yacFirst->a, PHP_EOL; // second a!
echo $yacSecond->a, PHP_EOL; // second a!
Copy after login

We all use the default instantiated Yac object in this code. Although they are instantiated separately, the spaces they save are the same, so the same a variables will overwrite each other.

$yacFirst = new Yac('first');
$yacFirst->a = 'first a!';;

$yacSecond = new Yac('second');
$yacSecond->a = 'second a!';

echo $yacFirst->a, PHP_EOL; // first a!
echo $yacSecond->a, PHP_EOL; // second a!
Copy after login

When we use different instantiation parameters, the same a will not affect each other, they are stored in different spaces. In other words, Yac will automatically add a prefix to these Keys.

Cache aging

Finally, the caching system will have aging restrictions on cached content. If an expiration time is specified, the cached content will expire after the specified time.

$yac->add('ttl', '10s', 10);
$yac->set('ttl2', '20s', 20);
echo $yac->get('ttl'), PHP_EOL; // 10s
echo $yac->ttl2, PHP_EOL; // 20s

sleep(10);

echo $yac->get('ttl'), PHP_EOL; // 
echo $yac->ttl2, PHP_EOL; // 20s
Copy after login

The ttl cache in the above code only sets an expiration time of 10 seconds, so after 10 seconds of sleep(), the output ttl will have no content.

It should be noted that if the time setting is not set, it will be effective for a long time, and the expiration time cannot be set using the __set() method. You can only use the set() or add() function to set the expiration time. time.

Summary

How about the Yac extension? Is it as convenient and easy to use as our Apc? Of course, the more important thing is its performance and applicable scenarios. For small systems, especially in operating environments where the machine configuration is not so strong, this extended cache system can make our development faster and more convenient. Regarding the concept of lock-free sharing, we can refer to the second link in the reference document below, which is detailed in Brother Niao's article.

Test code:

https://github.com/zhangyue0503/dev-blog/blob/master/php/202006/source/PHP%E7%9A%84%E5%8F%A6%E4%B8%80%E4%B8%AA%E9%AB%98%E6%95%88%E7%BC%93%E5%AD%98%E6%89%A9%E5%B1%95%EF%BC%9AYac.php
Copy after login

Recommended learning: php video tutorial

The above is the detailed content of Detailed introduction to Yac, another efficient caching extension for PHP. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1667
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles