联系方式
保定市遨游计算机服务有限公司
全国热线:400-012-6696
售前咨询:13313028229
售前咨询:0312-3229012
售后:0312-6791400
售后:0312-6791120
地址:保定朝阳大街国贸大厦808室
网址:http://www.aoyouwangluo.com
简述网站开发中缓存机制的运用
[来源:www.aoyouwangluo.com] [作者:aowl] [日期:18-06-07] [浏览次数:]
基于页面级的缓存是非常容易实现的,也是最简单的一种。当我们访问一个页面的时候,网页程序从上到下依次执行,没执行一条语句后,服务器端对数据库做处理,然后返回给我们一个结果,那么假设一个网页需要有10个地方调用数据库返回信息,然后有100个人在线访问,数据库就要执行1000次的操作,可想而知大的网站负担有多么重。页面级缓存的思路就在于如何能让访问相同页面的人不重复的读取数据库,这样问题就解决了。 Page level caching is easy to implement and the simplest. When we access a page, the web program executes from top to bottom. After not executing a statement, the server handles the database and returns to us a result. Then it is assumed that a web page needs 10 places to call the database to return the information, and then there are 100 people accessing the database online, and the database is required. The implementation of the 1000 operation can imagine how heavy the site is. The idea of page level caching is how to let people who visit the same page read the database without duplication, so the problem is solved. 实现页面级缓存的方式有两种。第一种是生成静态页面,网站开发者先做好一个模版页面,然后当用户第一次访问的时候用程序载入这个模版页面,然后调用数据库,当整个页面程序完成之后,用整个结果写入到指定位置的一个html文件当中,这样接下来访问的用户可以直接访问这个页面,因为静态页面不需要读取数据库,所以执行的效率是很快的。这种方式的缺点在于我们需要手动,或者设置时间让程序自动重新生成静态页面,以保证程序的及时性。所以虽然静态页面访问速度较快,但只适用于页面不随时更新的网站。比如门户网站的首页,基本都是有规律的时间更新,就可以使用这种方式。因为实现比较简单,具体代码不一一写出,有需要的可以给我的博客留言。 There are two ways to implement page level caching. The first is to generate a static page, and the web developer makes a template page first, then the template is loaded into the template page when the user first visits, then the database is invoked, and when the whole page is completed, the entire result is written to a HTML file in the specified location, which is then accessed. Users can directly access this page, because static pages do not need to read the database, so the efficiency of execution is very fast. The disadvantage of this method is that we need to manually or set time to regenerate the static pages automatically, so as to ensure the timeliness of the program. So although the static page access speed is fast, it only applies to websites that are not updated at any time. For example, the front page of a portal website is basically updated regularly, so it can be used in this way. Because the implementation is relatively simple, the specific code is not written one by one, if necessary, you can leave a message for my blog. 实现页面级缓存的另一种方式是用php的smarty模版机制(或类似模版)。smarty是一套可以将网站的程序开发和网页样式开发分离的类库,同时又能够自动实现缓存功能。我们只需要把网页建立好程序页,然后在模版路径中建立对应的模版页面,smarty能够根据用户定义的模版页面自动生成缓存页,当第一次访问时候生成缓存,之后的用户访问自动调用缓存页面,并且页面有改动的时候,会自动更新缓存,对于网站开发者维护是非常方便的。但由于并不是生成纯静态页面,所以单论效率来说不如纯静态页面,不过相比普通的网页文件,已经快得多了。 Another way to implement page level caching is to use PHP's Smarty template mechanism (or similar template). Smarty is a class library that can separate web site's program development from webpage style development. At the same time, it can automatically realize caching function. We just have to set up a page for the page, and then build the corresponding template page in the template path. Smarty can automatically generate the caching page according to the user defined template page. When the first visit is generated, the cache is generated, and the later users automatically call the caching page, and when the page changes, it will automatically Updating caching is very convenient for website developers to maintain. But because it does not generate pure static pages, it is not as efficient as pure static pages in terms of efficiency, but it is much faster than ordinary web pages.