CodeIgniter's caching mechanism and usage_PHP tutorial
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");
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');
$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();
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);
其中 n 是你希望缓存更新的 分钟 数。可以使用 m/60 来精确到秒,例如 1/60 ,则是精确到 1秒。上面的代码可以放到任何一个 function 里面。他的出现顺序对缓存并没有影响,所以将它放在你认为最合乎逻辑的地方。一旦上面的代码放到了控制器的方法中,页面就会被缓存。
由于CI存储缓存文件的方式,只有通过 view 文件的输出才能被缓存。在缓存文件产生之前,请确保 application/cache 文件夹可写。
如果你不再想使用缓存,仅需将上面的代码从你的controller里面删除即可。注意: 这样做并不能让缓存文件立即消失,它将会自动过期并被删除。如果你想立即删除那些文件,就必须自己动手了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to implement custom middleware in CodeIgniter Introduction: In modern web development, middleware plays a vital role in applications. They can be used to perform some shared processing logic before or after the request reaches the controller. CodeIgniter, as a popular PHP framework, also supports the use of middleware. This article will introduce how to implement custom middleware in CodeIgniter and provide a simple code example. Middleware overview: Middleware is a kind of request

Detailed explanation of MyBatis caching mechanism: One article to understand the principle of cache storage Introduction When using MyBatis for database access, caching is a very important mechanism, which can effectively reduce access to the database and improve system performance. This article will introduce the caching mechanism of MyBatis in detail, including cache classification, storage principles and specific code examples. 1. Cache classification MyBatis cache is mainly divided into two types: first-level cache and second-level cache. The first-level cache is a SqlSession-level cache. When

Java cache mechanisms include memory cache, data structure cache, cache framework, distributed cache, cache strategy, cache synchronization, cache invalidation mechanism, compression and encoding, etc. Detailed introduction: 1. Memory cache, Java's memory management mechanism will automatically cache frequently used objects to reduce the cost of memory allocation and garbage collection; 2. Data structure cache, Java's built-in data structures, such as HashMap, LinkedList, HashSet, etc. , with efficient caching mechanisms, these data structures use internal hash tables to store elements and more.

CodeIgniter Middleware: Accelerating Application Responsiveness and Page Rendering Overview: As web applications continue to grow in complexity and interactivity, developers need to use more efficient and scalable solutions to improve application performance and responsiveness. . CodeIgniter (CI) is a lightweight PHP-based framework that provides many useful features, one of which is middleware. Middleware is a series of tasks that are performed before or after the request reaches the controller. This article will introduce how to use

With the vigorous development of e-commerce business, recommendation algorithms have become one of the keys to competition among major e-commerce platforms. As an efficient and high-performance language, Golang has great advantages in implementing e-commerce recommendation algorithms. However, while implementing efficient recommendation algorithms, the caching mechanism is also an issue that cannot be ignored. This article will introduce how to implement the caching mechanism of efficient e-commerce recommendation algorithm in Golang. 1. Why is the caching mechanism needed? In the e-commerce recommendation algorithm, the generation of recommendation results requires a large amount of computing resources. For high-concurrency e-commerce

Analysis of MyBatis' caching mechanism: The difference and application of first-level cache and second-level cache In the MyBatis framework, caching is a very important feature that can effectively improve the performance of database operations. Among them, first-level cache and second-level cache are two commonly used caching mechanisms in MyBatis. This article will analyze the differences and applications of first-level cache and second-level cache in detail, and provide specific code examples to illustrate. 1. Level 1 Cache Level 1 cache is also called local cache. It is enabled by default and cannot be turned off. The first level cache is SqlSes

Introduction to the method of using the database query builder (QueryBuilder) in the CodeIgniter framework: CodeIgniter is a lightweight PHP framework that provides many powerful tools and libraries to facilitate developers in web application development. One of the most impressive features is the database query builder (QueryBuilder), which provides a concise and powerful way to build and execute database query statements. This article will introduce how to use Co

Alibaba Cloud caching mechanisms include Alibaba Cloud Redis, Alibaba Cloud Memcache, distributed cache service DSC, Alibaba Cloud Table Store, CDN, etc. Detailed introduction: 1. Alibaba Cloud Redis: A distributed memory database provided by Alibaba Cloud that supports high-speed reading and writing and data persistence. By storing data in memory, it can provide low-latency data access and high concurrency processing capabilities; 2. Alibaba Cloud Memcache: the cache system provided by Alibaba Cloud, etc.
