Hongmu Notes
Home Program Notes High-efficiency random calls for the entire empire cms site (millisecond-level calls)
Program Notes Empire cms

High-efficiency random calls for the entire empire cms site (millisecond-level calls)

High-efficiency random calls for the entire empire cms site (millisecond-level calls)

Features:

The calling efficiency is extremely high. It only takes 500 milliseconds to call the test randomly with 790,000 data.

The call can be made to a specified column or to the entire table.

Easy to install, just one function

PHP code:


function ecms_rand($classid,$num){
    global $empire,$dbtbpre;
    $file_cache = ECMS_PATH."d/news.json"; //缓存文件
    //缓存整个news表的数据
    if(!file_exists($file_cache)){
        //查询分类
        $hm_class=$empire->query("select classid from {$dbtbpre}enewsclass");   
        while($hm_r=$empire->fetch($hm_class))
        {
            //查询分类下面的所有id
            $hm_news=$empire->query("SELECT id FROM `{$dbtbpre}ecms_news` WHERE `classid` = {$hm_r[classid]}");   
            while($hm_nr=$empire->fetch($hm_news))
            {
                $news_data[$hm_r['classid']][] = $hm_nr[id];
            }
        }
        file_put_contents($file_cache,json_encode($news_data));
    }
    //获取缓存数据文件
    $file_arrs = json_decode(file_get_contents($file_cache),true);
    if($classid=="0"){
        $num = ceil($num/count($file_arrs));
        foreach($file_arrs as $k=>$v)
        {      
                     if(count($v)  <= $num){ continue;}
            //根据数据表获取id,每个栏目随机分配文章id
            $key_array = array_rand($file_arrs[$k],$num);
            foreach($key_array as $v)
            {
                $rand_id.= $file_arrs[$k][$v].",";    
            }
        }
        $rand_id = rtrim($rand_id,",");
    }else{
              if(count($file_arrs[$classid]) <= $num){
            $key_array = array_rand($file_arrs[$classid],count($file_arrs[$classid]));
        }else{
            $key_array = array_rand($file_arrs[$classid],$num);
        }
        //根据数据表获取id,每个栏目随机分配文章id
        $key_array = array_rand($file_arrs[$classid],$num);
        foreach($key_array as $v)
        {
            $rand_id.= $file_arrs[$classid][$v].",";    
        }
        $rand_id = rtrim($rand_id,",");
    }
    return $rand_id;
}

Installation method:

Put the code in the e/class/userfun.php file

Calling method:


<?php
$id = ecms_rand(1,50);
?>
调用的文章id:
[e:loop={0,50,3,0,"id in ($id)"}]   
<?=$bqr['id']?>             
[/e:loop]

Summary of efficiency:

1. Reading files directly is more efficient than database query, and the connection and disconnection time is not included in the article.

2. The larger the content read at one time, the more obvious the advantage of reading the file directly (the file reading time increases slightly, which is related to the continuity of file storage and cluster size). This result is exactly the opposite of expected, indicating that MYSQL may have additional operations for reading larger files (the two times increased by nearly 30%). If it is just a simple assignment conversion, the difference should be small.

3. It can be inferred that the database efficiency will only be worse when writing files and INSERT without testing.

4. If a very small configuration file does not need to use database features, it is more suitable to store it in a separate file. There is no need to create a separate data table or record. It is more convenient to store large files such as pictures and music in files. It is more reasonable to only put index information such as paths or thumbnails in the database.

5. If you only read files on PHP, file_get_contents is more efficient than fopen and fclose. Excluding the time required to determine the existence of this function, it will take about 3 seconds less.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

Empire CMS database statement & SQL statement format

Empire CMS database statement & SQL statement format Program Notes Empire cms

Introduction to Imperial CMS extended SQL program writing, basic examples of Imperial CMS database statements & SQL statement formats: Note: The following examples are based on placing PHP files in the system root directory. Example 1: Connect to MYSQL program. (a.php)<?php require('e/class/connect.php'); //Introduce database configuration files and public function files requ…
👁 353
Empire CMS obtains the system COOKIE variable function getcvar()

Empire CMS obtains the system COOKIE variable function getcvar() Program Notes Empire cms

Get the system COOKIE variable function syntax: getcvar($var,$ecms) Description: $var: is the variable name $ecms: 0 is to set the foreground COOKIE variable, 1 is to set the background COOKIE variable. This parameter can be omitted and defaults to 0. Usage example: getcvar('mlusername'), get the user name of the front-end login member getcvar('loginu...
👁 296
Empire CMS smart label call field collection

Empire CMS smart label call field collection Program Notes Empire cms

Collect and classify all fields that support smart tag calls into Empire CMS smart tags: [e:loop={column ID/topic ID, number of items displayed, operation type, only display pictures with titles, additional SQL conditions, display sorting}] Template code content [/e:loop] Calling time: <?=date('m-d',$bqr[newstime])?> <?=dat…
👁 3749

Recommended reading

PHP: Delete files with a specified file extension from a given directory.

PHP: Delete files with a specified file extension from a given directory. Language Notes PHP

You can use PHP's `glob` function to match file names within a specified directory and then use the `unlink` function to delete them. The specific steps are as follows: Use the `glob` function to match the file names in the specified directory and store them in an array; the code is as follows: `$files = glob(' /path/to/dir/*.txt');` where `/path/to/dir` refers to the directory you wish to operate on...
👁 239
(Self-adaptive mobile phone terminal) logistics, transportation, express delivery and warehousing website template-with three-level column 0980

(Self-adaptive mobile phone terminal) logistics, transportation, express delivery and warehousing website template-with three-level column 0980 Practical Collection pbootcms Template

A logistics transportation express warehouse PbootCMS website template, supporting PC and WAP, with three columns. The design style is efficient and modern, which is suitable for express delivery and warehousing companies to show their service network and distribution strength. It is helpful for logistics enterprises to display their brands online and attract e-commerce and corporate customers. Template display installation instructions website background: /admin.php account number: admin password: admin decompression password: www.4s5…
👁 74
(PC+WAP) Security Service Website Template 0981

(PC+WAP) Security Service Website Template 0981 Practical Collection pbootcms Template

A security services PbootCMS website template compatible with both PC and WAP devices. Its professional and sophisticated design makes it ideal for security firms and property management companies to showcase their services and qualifications. This template helps security service providers build a strong online brand image and attract customers. Template Overview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn | Related Articles: Pb...
👁 32
(Adaptive mobile version) Hotel bedding English-language international e-commerce website template – includes download functionality and three-level navigation menu – 0982

(Adaptive mobile version) Hotel bedding English-language international e-commerce website template – includes download functionality and three-level navigation menu – 0982 Practical Collection pbootcms Template

A professional English-language PbootCMS website template designed for hotel bedding products targeting international trade, compatible with both PC and WAP devices, featuring a download function and a three-level navigation structure. Its sophisticated, international design makes it ideal for hotel bedding manufacturers and exporters to showcase their products and brand identity, supporting effective brand promotion in global markets. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5....
👁 42
Resource website typecho001 template

Resource website typecho001 template Program Notes Typecho

typecho001 template template please do not modify the folder name of this template. The folder name is: typecho001 1.4 Fix the js output problem on the article page 1.3 Fix the error prompt when the plug-in is not installed Optimize the list page code output 1.2 Fix the comment function and add a custom homepage title 1.1 Fix the comment reply asymmetry function Add a separate title setting function on the homepage Add the website favicon.ico icon Add one...
👁 199