Home Backend Development PHP Tutorial PHP 资料缓存数组的实现

PHP 资料缓存数组的实现

Jun 13, 2016 pm 01:22 PM
data query quot

PHP 文件缓存数组的实现

在一个试验性项目中,我需要从 sqlite3 数据库中随机读取一条记录给用户,要读取的数据表现在有23850条记录,按 skemu 分类,一般每个 skemu 下有 3000多条记录,原先我使用了 sqlite3 的随机查询语句:

 

$query="SELECT * FROM shiti WHERE skemu = " . intval($kemuid) . " order by random() limit 1";
Copy after login

然后在我现在用的电脑上没有感觉到明显迟延,但当我把服务器换到一个 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;
 }
Copy after login

<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;
 }

Copy after login

<span style="font-family:Arial">这样比每次执行随机查询快多了,但是还是要比生成一个HTML缓存要慢一点,但那样需要把更多数据暴露给客户端,或是存储更多缓存,我想这已经是一个比较均衡的方案了。</span>

<span style="font-family:Arial"></span> 

<span style="font-family:Arial">这个解决办法的要点在于数组的序列化与反序列化,大家应该很容易看得懂。</span>

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
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)

How to reorder multiple columns in Power Query via drag and drop How to reorder multiple columns in Power Query via drag and drop Mar 14, 2024 am 10:55 AM

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

React Query database plug-in: how to import and export data React Query database plug-in: how to import and export data Sep 26, 2023 pm 05:37 PM

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提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

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

How to split data to NTFS using Power Query How to split data to NTFS using Power Query Mar 15, 2024 am 11:00 AM

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

What data is in the data folder? What data is in the data folder? May 05, 2023 pm 04:30 PM

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.

What to do if mysql load data is garbled? What to do if mysql load data is garbled? Feb 16, 2023 am 10:37 AM

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;".

React Query Database Plugin: Strategies for Data Backup and Restore React Query Database Plugin: Strategies for Data Backup and Restore Sep 28, 2023 pm 11:22 PM

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

AI project failure rates top 80% — study cites poor problem recognition and a focus on latest tech trends among major problems AI project failure rates top 80% — study cites poor problem recognition and a focus on latest tech trends among major problems Aug 31, 2024 am 12:59 AM

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

See all articles