Some ideas and feasibility of caching during smarty + php development. Let's take a look at the xuzuning moderator.

Source: Internet
Author: User
Some ideas and feasibility of caching during smarty + php development. The xuzuning moderator will show you how to develop a program using PHP + SMARTY. When SMARTY was used at that time, it was known that SMARTY has the cache function. But I didn't understand it at the time. It is assumed that the cache is completed after the program development is completed .. But now the development is complete, start to study the SMARTY cache. I found that the smarty cache is not suitable for me at all (maybe I did not study it thoroughly ?)
First of all, once smarty has enabled the cache function, it proves that all pages start to be cached, and 90% of my programs are real-time. No cache required

Only 10% features are available. For example, the BANNER of a link website does not need to be cached once every 10 years. You can query a table every time.

However, the local cache function of SMARTY is definitely not suitable for me. Cannot all the 90% functions be called as a non-cache tag?
Baidu is the same after a while, and there is definitely no solution (maybe I didn't understand it)


Later, I began to study the cache technology of PHP... I found that PHP cannot use IIS application to cache like ASP

In addition to installing third-party software .. Most of them are file caches ........ Then the problem arises. I suddenly came up with a strange idea.

Now, both SMARTY and PHP use file caching .. Why don't I write one myself ,,.. The method is as follows:

Function web (){
An array composed of related SQL queries
}
$ Smarty-> assign ('web', web ());
$ Smarty-> display ("web.html ");
This is a normal analysis of smarty... Can we do this?

Function web (){
If (when the cached file does not exist ){
Execute SQL query
Generate a TXT file and put the queried array in it (when the cached file does not exist for the first time)
} Elseif (the cached file is found in the second time ){
Return open the TXT document and read the array in the document;
}
$ Smarty-> assign ('web', web ());
$ Smarty-> display ("web.html ");

}

This is the method I have come up with. I feel that PHP can read TXT at a faster speed than Database reading, right.

Security considerations
1. Is this feasible?
2. will PHP performance be affected when the data volume is large (important)
3. there is no better way
4. Can SMARTY specify a cache object .. None of them are cached.


Please discuss it. Give me an answer.


Reply to discussion (solution)

1. PHP reading TXT is certainly faster than reading the database
Is this feeling accurate? Because in fact it is not just a read, but it also requires other operations, such as checking whether the file exists and checking whether the file is too old .....
2. if the file is cached with query results, is the SQL _CACHE clause provided by mysql more efficient than self-manipulating the cache file?
3. if there is no need to change the information once every 10 years, why not include the static file directly?
4. the method you describe is exactly the same as that described by smarty. of course, there are obviously more smarty code than yours. But why? It's easy. After years of practice, smarty's code can handle the complex situations you haven't encountered before. After your project runs for a period of time, the code in the cache control part will also become fat.
5. if you want to use a third-party memory operating software to cache data to the memory, should you also consider using the mysql memory table? At least you do not need to change the original code to operate the memory like the operation table.
6. if your running environment is a unix-compatible system, can you also consider mounting a memory-based file system?

In short, there is no static way to improve efficiency, and the best combination of various elements is the result of experiments.

1. PHP reading TXT is certainly faster than reading the database
Is this feeling accurate? Because in fact it is not just a read, but it also requires other operations, such as checking whether the file exists and checking whether the file is too old .....
2. if the file is cached with query results, is the SQL _CACHE clause provided by mysql more efficient than self-manipulating the cache file?
3. if there is no need to change the information once every 10 years, why not include the static file directly?
4. the method you describe is exactly the same as that described by smarty. of course, there are obviously more smarty code than yours. But why? It's easy. After years of practice, smarty's code can handle the complex situations you haven't encountered before. After your project runs for a period of time, the code in the cache control part will also become fat.
5. if you want to use a third-party memory operating software to cache data to the memory, should you also consider using the mysql memory table? At least you do not need to change the original code to operate the memory like the operation table.
6. if your running environment is a unix-compatible system, can you also consider mounting a memory-based file system?

In short, there is no static way to improve efficiency, and the best combination of various elements is the result of experiments.




1. you only need to determine whether the txt exists. You do not need to determine the time .. Because if the user modifies the latest data .. You can ask him to clear the cache (that is, to delete the old TXT file)
2. static files such as html cannot be saved, because my idea is to save the array in TXT and then read it out. use the smarty tag {list} to loop through the {/list} of the array p}
3. now I will take a look at the SQL _CACHE clause and mysql memory tables. I am a beginner in PHP. I switched it from ASP.

Thank you, moderator.

The smarty cache is for files and does not limit where cache is not used. that is, some pages do not enable cache. if the cache volume is large, we recommend that you use the memory cache.

1. memory table or memcache
2. file format

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.