search
HomeBackend DevelopmentPHP ProblemDetailed introduction to Yac, another efficient caching extension for 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"
// }

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
// )

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!

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
// )

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!

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!

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

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

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
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete

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

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment