PHP企业级应用缓存技术详解_PHP
之前我们曾深入的探讨过PHP缓存技术,其中主要提到了数据缓存。数据缓存主要是指数据库查询缓存,每次访问页面的时候,都会先检测相应的缓存数据是否存在,如果不存在,就连接数据库,得到数据, 并把查询结果序列化后保存到文件中,以后同样的查询结果就直接从缓存表或文件中获得。
用的最广的例子看Discuz的搜索功能,把结果ID缓存到一个表中,下次搜索相同关键字时先搜索缓存表。
举个常用的方法,多表关联的时候,把附表中的内容生成数组保存到主表的一个字段中,需要的时候数组分解一下,这样的好处是只读一个表,坏处就是两个 数据同步会多不少步骤,数据库永远是瓶颈,用硬盘换速度,是这个的关键点。
页面缓存
每次访问页面的时候,都会先检测相应的缓存页面文件是否存在,如果不存在,就连接数据库,得到数据,显示页面并同时生成缓存页面文件,这样下次访问 的时候页面文件就发挥作用了。(模板引擎和网上常见的一些缓存类通常有此功能)
时间触发缓存
检查文件是否存在并且时间戳小于设置的过期时间,如果文件修改的时间戳比当前时间戳减去过期时间戳大,那么就用缓存,否则更新缓存。
内容触发缓存
当插入数据或更新数据时,强制更新缓存。
静态缓存
这里所说的静态缓存是指静态化,直接生成HTML或xml等文本文件,有更新的时候重生成一次,适合于不太变化的页面,这就不说了。
内存缓存
Memcached是高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度。
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li> <li> <span>$</span><span class="attribute"><font color="#ff0000">memcache</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> Memcache; </span> </li> <li class="alt"> <span>$memcache-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>connect(‘localhost’, 11211) or die (“Could not connect”); </span> </li> <li> <span>$</span><span class="attribute"><font color="#ff0000">version</font></span><span> = $memcache-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>getVersion(); </span> </li> <li class="alt"><span>echo “Server’s version: “.$version.”\n”; </span></li> <li> <span>$</span><span class="attribute"><font color="#ff0000">tmp_object</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> stdClass; </span> </li> <li class="alt"> <span>$tmp_object-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">str_attr</font></span><span> = ‘test’; </span> </li> <li> <span>$tmp_object-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">int_attr</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">123</font></span><span>; </span> </li> <li class="alt"> <span>$memcache-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>set(‘key’, $tmp_object, false, 10) or die (“Failed to save data at the server”); </span> </li> <li><span>echo “Store data in the cache (data will expire in 10 seconds)\n”; </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">get_result</font></span><span> = $memcache-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>get(‘key’); </span> </li> <li><span>echo “Data from the cache:\n”; </span></li> <li class="alt"><span>var_dump($get_result); </span></li> <li> <span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
读库的例子:
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li> <li> <span>$</span><span class="attribute"><font color="#ff0000">sql</font></span><span> = ‘SELECT * FROM users’; </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">key</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">md5</font></span><span>($sql); //memcached 对象标识符 </span> </li> <li> <span>if ( !($</span><span class="attribute"><font color="#ff0000">datas</font></span><span> = $mc-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>get($key)) ) { </span> </li> <li class="alt"><span> </span></li> <li><span>// 在 memcached 中未获取到缓存数据,则使用数据库查询获取记录集 </span></li> <li class="alt"><span> </span></li> <li><span>echo “n”.str_pad(‘Read datas from MySQL.’, 60, ‘_’).”n”; </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">conn</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">mysql_connect</font></span><span>(‘localhost’, ‘test’, ‘test’); </span> </li> <li><span>mysql_select_db(‘test’); </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">result</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">mysql_query</font></span><span>($sql); </span> </li> <li> <span>while ($</span><span class="attribute"><font color="#ff0000">row</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">mysql_fetch_object</font></span><span>($result)) </span> </li> <li class="alt"><span>$datas[] = $row; </span></li> <li><span> </span></li> <li class="alt"><span>// 将数据库中获取到的结果集数据保存到 memcached 中,以供下次访问时使用 </span></li> <li><span> </span></li> <li class="alt"> <span>$mc-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>add($key, $datas); </span> </li> <li><span>} else { </span></li> <li class="alt"><span>echo “n”.str_pad(‘Read datas from memcached.’, 60, ‘_’).”n”; </span></li> <li><span>} </span></li> <li class="alt"><span>var_dump($datas); </span></li> <li> <span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
PHP的缓冲器
比如eaccelerator,apc,phpa,xcache等等。
MySQL缓存
这也算非代码级的,经典的数据库就是用的这种方式,看下面的运行时间,0.09xxx之类的。
- [client]
- ……
- default-character-set=gbk
- default-storage-engine=MYISAM
- max_connections=600
- max_connect_errors=500
- back_log=200
- interactive_timeout=7200
- query_cache_size=64M
- ……
- table_cache=512
- ……
- myisam_max_sort_file_size=100G
- myisam_max_extra_sort_file_size=100G
- myisam_sort_buffer_size=128M
- key_buffer_size=1024M
- read_buffer_size=512M
- ……
- thread_concurrency=8
<span></span>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The role and practical application of arrow symbols in PHP In PHP, the arrow symbol (->) is usually used to access the properties and methods of objects. Objects are one of the basic concepts of object-oriented programming (OOP) in PHP. In actual development, arrow symbols play an important role in operating objects. This article will introduce the role and practical application of arrow symbols, and provide specific code examples to help readers better understand. 1. The role of the arrow symbol to access the properties of an object. The arrow symbol can be used to access the properties of an object. When we instantiate a pair

Deleted something important from your home screen and trying to get it back? You can put app icons back on the screen in a variety of ways. We have discussed all the methods you can follow and put the app icon back on the home screen. How to Undo Remove from Home Screen in iPhone As we mentioned before, there are several ways to restore this change on iPhone. Method 1 – Replace App Icon in App Library You can place an app icon on your home screen directly from the App Library. Step 1 – Swipe sideways to find all apps in the app library. Step 2 – Find the app icon you deleted earlier. Step 3 – Simply drag the app icon from the main library to the correct location on the home screen. This is the application diagram

This paper explores the problem of accurately detecting objects from different viewing angles (such as perspective and bird's-eye view) in autonomous driving, especially how to effectively transform features from perspective (PV) to bird's-eye view (BEV) space. Transformation is implemented via the Visual Transformation (VT) module. Existing methods are broadly divided into two strategies: 2D to 3D and 3D to 2D conversion. 2D-to-3D methods improve dense 2D features by predicting depth probabilities, but the inherent uncertainty of depth predictions, especially in distant regions, may introduce inaccuracies. While 3D to 2D methods usually use 3D queries to sample 2D features and learn the attention weights of the correspondence between 3D and 2D features through a Transformer, which increases the computational and deployment time.

The Linuxtee command is a very useful command line tool that can write output to a file or send output to another command without affecting existing output. In this article, we will explore in depth the various application scenarios of the Linuxtee command, from entry to proficiency. 1. Basic usage First, let’s take a look at the basic usage of the tee command. The syntax of tee command is as follows: tee[OPTION]...[FILE]...This command will read data from standard input and save the data to

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

The Go language is an open source programming language developed by Google and first released in 2007. It is designed to be a simple, easy-to-learn, efficient, and highly concurrency language, and is favored by more and more developers. This article will explore the advantages of Go language, introduce some application scenarios suitable for Go language, and give specific code examples. Advantages: Strong concurrency: Go language has built-in support for lightweight threads-goroutine, which can easily implement concurrent programming. Goroutin can be started by using the go keyword

Written above & The author’s personal understanding is that image-based 3D reconstruction is a challenging task that involves inferring the 3D shape of an object or scene from a set of input images. Learning-based methods have attracted attention for their ability to directly estimate 3D shapes. This review paper focuses on state-of-the-art 3D reconstruction techniques, including generating novel, unseen views. An overview of recent developments in Gaussian splash methods is provided, including input types, model structures, output representations, and training strategies. Unresolved challenges and future directions are also discussed. Given the rapid progress in this field and the numerous opportunities to enhance 3D reconstruction methods, a thorough examination of the algorithm seems crucial. Therefore, this study provides a comprehensive overview of recent advances in Gaussian scattering. (Swipe your thumb up

In September 23, the paper "DeepModelFusion:ASurvey" was published by the National University of Defense Technology, JD.com and Beijing Institute of Technology. Deep model fusion/merging is an emerging technology that combines the parameters or predictions of multiple deep learning models into a single model. It combines the capabilities of different models to compensate for the biases and errors of individual models for better performance. Deep model fusion on large-scale deep learning models (such as LLM and basic models) faces some challenges, including high computational cost, high-dimensional parameter space, interference between different heterogeneous models, etc. This article divides existing deep model fusion methods into four categories: (1) "Pattern connection", which connects solutions in the weight space through a loss-reducing path to obtain a better initial model fusion
