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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

See all articles