Home > Backend Development > PHP Tutorial > CodeIgniter's caching mechanism and usage_PHP tutorial

CodeIgniter's caching mechanism and usage_PHP tutorial

WBOY
Release: 2016-07-13 10:33:52
Original
1271 people have browsed it

Database cache

The database cache class allows you to save database query results in text files to reduce database access.

Activating cache requires three steps:

  • Create a writable directory on the server to save cache files.
  • Set its directory in the file application/config/database.php.
  • To activate the cache feature, you can set global options in the file application/config/database.php, or you can set it manually using the method below on this page.

Once activated, caching occurs automatically every time a page containing a database query is loaded.

CodeIgniter’s query caching system can be executed dynamically when the page is viewed. If the caching feature is activated, the result objects of the database query will be serialized and saved in a text file on your server when this page is first loaded. The cached file will replace the database query when the page is loaded again. In this way, your database usage will drop to 0 among cached pages.

Only read-type (SELECT) queries will be cached because only this type of query will produce a result set. Write-type (Write-type) (INSERT, UPDATE, etc.) queries do not generate result sets, so the cache system does not cache them.

Cache files will not expire and any cached queries will always exist unless you delete them. The cache system allows you to clear page by page, or clear all caches. Generally speaking, you can use the following function to clear the cache when certain events (such as adding data to the database) occur.

Whether caching can achieve performance gains depends on many factors. If you have a highly optimized database with little load, you may not see a performance improvement. If your database is under heavy use, you may see a performance improvement from caching, provided your file system doesn't have too much overhead. In some cluster server environments, this situation may occur because the file system operations are too frequent and the cache cannot be generated correctly. On a single server in a shared environment, caching may be beneficial. Whether there is any performance improvement may also depend on your database. It depends on your specific situation.

CI places the results of each query in its own cache file. Depending on your controller function, the set of cache files will be further organized into subdirectories. To be precise, the name of the subdirectory is determined by the first two segments of your URI (controller class name and function name). For example, suppose you have a controller blog and a comments function that contains three queries. The caching system will create a directory called blog+comments and create three cache files in this directory. When you dynamically query based on information at the URI (such as using paging), each instance of the query will create its own cache file. Therefore, after many queries, the number of cached files may be more than the number of queries you have made.

Since cache files do not expire, you need to write code in your application to delete the cache operation. For example, let's say you have a blog that lets users post comments. Whenever a new comment is submitted you will definitely want to delete the cache file in a controller method with the controller function. You will find the following introduction of two deletion functions that can help you clear data.

The cache switch can be set manually. This feature is useful if you want to keep certain queries from being cached. For example:

// 打开缓存开关
$this->db->cache_on();
$query = $this->db->query("SELECT * FROM mytable");
// 使下面这条查询不被缓存
$this->db->cache_off();
$query = $this->db->query("SELECT * FROM members WHERE member_id = '$current_user'");
// Turn caching back on
$this->db->cache_on();
$query = $this->db->query("SELECT * FROM another_table");
Copy after login

Delete cache files and specific web pages. If you need to clear cache after updating your database.

The cache system will create a subdirectory corresponding to the accessed URL in the cache storage directory, and store the cache file in that subdirectory. The main cache directory is what you set in application/config/database.php Cache directory. For example, if you are browsing the page at example.com/index.php/blog/comments, the cache system will put all generated cache files into a folder named blog+comments. If If you want to delete the cache file corresponding to the example just mentioned, you need to execute the following code:

$this->db->cache_delete('blog', 'comments');
Copy after login

$this->db->cache_delete('blog', 'comments'), it didn't work when I actually tested it. I don't know why. I don't know if it is a small bug? But the following $this-> db->cache_delete_all() is OK, no problem.

If you don't use any parameters, the current URI settings will determine when the cache should be cleared/updated.

Clear all cached files. Example:

$this->db->cache_delete_all();
Copy after login

Web page caching

Codeigniter supports caching technology to achieve the fastest speed. Although CI is already quite efficient, factors such as the dynamic content in the web page, the host's memory CPU and database reading speed directly affect the loading speed of the web page. Relying on web caching, your web pages can achieve a loading speed close to that of static web pages, because they save the program output results to the hard disk.

CI支持每个页面单独缓存,而且可以设置缓存更新时间。当一个网页第一次被加载的时候,缓存文件将被保存到application/cache文件夹。 下次访问的时候,系统就会直接读取缓存文件,然后返回给用户的浏览器。如果缓存文件过期,它将被删除并重新生成。

启用缓存功能,只需要将下面的代码放入你的任何一个控制器(controller)的方法(function)内:

$this->output->cache(n);
Copy after login

其中 n 是你希望缓存更新的 分钟 数。可以使用 m/60 来精确到秒,例如 1/60 ,则是精确到 1秒。上面的代码可以放到任何一个 function 里面。他的出现顺序对缓存并没有影响,所以将它放在你认为最合乎逻辑的地方。一旦上面的代码放到了控制器的方法中,页面就会被缓存。

由于CI存储缓存文件的方式,只有通过 view 文件的输出才能被缓存。在缓存文件产生之前,请确保 application/cache 文件夹可写。

如果你不再想使用缓存,仅需将上面的代码从你的controller里面删除即可。注意: 这样做并不能让缓存文件立即消失,它将会自动过期并被删除。如果你想立即删除那些文件,就必须自己动手了。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/752391.htmlTechArticle数据库缓存 数据库缓存类允许你把数据库查询结果保存在文本文件中以减少数据库访问。 激活缓存需要三步: 在服务器上创建一个可写的...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template