PHP 资料缓存数组的实现
PHP 文件缓存数组的实现
在一个试验性项目中,我需要从 sqlite3 数据库中随机读取一条记录给用户,要读取的数据表现在有23850条记录,按 skemu 分类,一般每个 skemu 下有 3000多条记录,原先我使用了 sqlite3 的随机查询语句:
$query="SELECT * FROM shiti WHERE skemu = " . intval($kemuid) . " order by random() limit 1";
然后在我现在用的电脑上没有感觉到明显迟延,但当我把服务器换到一个 P4 1.8G 512M内存的机器上运行时,感觉数据读取速度非常慢,需要2、3秒才能读出数据。如果每次用户要取一条随机记录,都执行这样一个随机查询的话,瓶颈将会出现在数据库查询过程中,难以想像这样的性能如何来适应众多用户的同时访问。为此,我考虑减少数据库的查询次数,先把记录 id 取出来放到数组中,然后从中再来随机取一条记录,同时存为文件供以后读取,这样以减少数据库查询的次数。
以下函数实现读出 id 集:
static function getIDs($kemuid) { $cachefile="cache/" . $kemuid . ".cache"; $datas=array(); if (!file_exists($cachefile)||time() < (filemtime($cachefile) + 14400)) //缓存不存在或超过4小时 { global $data; //读取 id 集 $query="SELECT sid FROM shiti WHERE skemu = " . intval($kemuid); $res = $data->query($query); while($r = $data->fetchArray($res)) { $datas[]=$r['sid']; } //写入缓存 file_put_contents($cachefile,serialize($datas)); } else { //读出缓存 $fp = fopen($cachefile,'r');//读 $datas = unserialize(fread($fp,filesize($cachefile)));//反序列化,并赋值 } return $datas; }
<span style="font-family:Arial"><span style="color:#548dd4"><span style="color:#366092">调用它的读取随机记录函数:</span></span></span>
static function getRondam($kemuid) { global $data; $ids=self::getIDs($kemuid); $index=rand(0,count($ids)-1); $id=$ids[$index]; $query="SELECT * FROM shiti WHERE sid = " . intval($id); $res = $data->query($query); $r = $data->fetchArray($res); $r['da']=$s; return $r; }
<span style="font-family:Arial">这样比每次执行随机查询快多了,但是还是要比生成一个HTML缓存要慢一点,但那样需要把更多数据暴露给客户端,或是存储更多缓存,我想这已经是一个比较均衡的方案了。</span>
<span style="font-family:Arial"></span>
<span style="font-family:Arial">这个解决办法的要点在于数组的序列化与反序列化,大家应该很容易看得懂。</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

In this article, we will show you how to reorder multiple columns in PowerQuery by dragging and dropping. Often, when importing data from various sources, columns may not be in the desired order. Reordering columns not only allows you to arrange them in a logical order that suits your analysis or reporting needs, it also improves the readability of your data and speeds up tasks such as filtering, sorting, and performing calculations. How to rearrange multiple columns in Excel? There are many ways to rearrange columns in Excel. You can simply select the column header and drag it to the desired location. However, this approach can become cumbersome when dealing with large tables with many columns. To rearrange columns more efficiently, you can use the enhanced query editor. Enhancing the query

ReactQuery database plug-in: Methods to implement data import and export, specific code examples are required. With the widespread application of ReactQuery in front-end development, more and more developers are beginning to use it to manage data. In actual development, we often need to export data to local files or import data from local files into the database. In order to implement these functions more conveniently, you can use the ReactQuery database plug-in. The ReactQuery database plugin provides a series of methods

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

This article will introduce how to use PowerQuery to split data into rows. When exporting data from other systems or sources, it is common to encounter situations where the data is stored in cells combining multiple values. With PowerQuery, we can easily split such data into rows, making the data easier to process and analyze. This can happen if the user doesn't understand Excel's rules and accidentally enters multiple data into a cell, or if the data is not formatted correctly when copying/pasting it from other sources. Processing this data requires additional steps to extract and organize the information for analysis or reporting. How to split data in PowerQuery? PowerQuery transformations can be based on a variety of different factors such as word

The data folder contains system and program data, such as software settings and installation packages. Each folder in the Data folder represents a different type of data storage folder, regardless of whether the Data file refers to the file name Data or the extension. Named data, they are all data files customized by the system or program. Data is a backup file for data storage. Generally, it can be opened with meidaplayer, notepad or word.

The solution to the garbled mysql load data: 1. Find the SQL statement with garbled characters; 2. Modify the statement to "LOAD DATA LOCAL INFILE "employee.txt" INTO TABLE EMPLOYEE character set utf8;".

ReactQuery database plug-in: Strategies to implement data backup and restoration, specific code examples are required Introduction: In modern web development, data backup and restoration is a very important task. Especially when using state management tools like ReactQuery, we need to ensure data security and reliability. This article will introduce a database plug-in based on ReactQuery to implement data backup and restore strategies, and provide specific code examples. ReactQu

Everyone and their aunt seem to be hopping aboard the AI train in search of inflated profit margins and marketing hype — just look at AMD's recent Ryzen rebrand as a prime example of this AI hype. A recent study conducted by RAND has found that this
