Home Backend Development PHP Tutorial [codeigniter 5], query cache

[codeigniter 5], query cache

Jul 30, 2016 pm 01:31 PM
cache gt query this

Database Cache Class

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

Important

When caching is enabled, this class will be automatically loaded by the database driver, do not load it manually.

Important

Not all query results can be cached, please read the content of this page carefully.

Enable caching

Enabling caching requires three steps:

  • Create a writable directory on the server to save cache files;
  • Set its directory path through the cachedir parameter in the file application/config/database.php ;
  • By setting the cache_on parameter in the file application/config/database.php to TRUE, you can also manually configure it using the following method.

Once caching is enabled, every time a page is loaded, as long as the page contains database queries, it will be automatically cached.

How does caching work?

CodeIgniter’s query caching system will automatically run when you visit a page. If caching is enabled, when the page is first loaded, the query result object is serialized and saved to a text file on the server. When you access the page next time, the cache file will be used directly without accessing the database. In this way, your database access will be reduced to 0 for the cached page.

Only read type (SELECT) queries can be cached, because only this type of query will produce results. Write-type queries (INSERT, UPDATE, etc.) do not generate results, so they will not be cached.

Cache files never expire, and all queries will always be available as long as they are cached unless you delete them. You can delete the cache for specific pages, or you can clear the entire cache. Generally speaking, you can use the following function to clear the cache when certain events occur (such as data being added to the database).

Can caching improve site performance?

Whether caching can achieve performance gains depends on many factors. If you have a heavily optimized database under low load, you may not see a performance improvement. And if your database is being accessed heavily, you may see performance improvements after caching, assuming your file system doesn't have too much overhead. One thing to remember is that caching simply changes the way data is obtained, from accessing the database to accessing the file system.

For example, in some cluster server environments, caching is actually harmful due to too frequent file system operations. Caching may only be beneficial in a shared, single-server environment. Unfortunately, there is no single answer to the question of whether you need to cache your database, it all depends on your situation.

How are cache files stored?

CodeIgniter caches each query into its own cache file, which is further organized into respective subdirectories based on the controller method called. To be more precise, subdirectories are named using the first two segments of your URI (controller name and method name).

For example, you have a blog controller and a comments method with three different queries. The caching system will create a directory named blog+comments and generate three cache files in this directory.

If your URI contains dynamic queries (such as when using paging), each query instance will generate its own separate cache file. Therefore, the number of cache files may eventually be several times the number of queries on your page. Case.

Manage your cache files

Since cache files do not expire, then there should be a mechanism to delete the cache in your application, for example, we assume you have a blog and allow users to comment, whenever a new comment is submitted When doing so, you should delete the cache file corresponding to the controller method that displays comments. There are two different methods for deleting cached data.

Not all database methods are compatible with caching

Finally, we must point out that the cached result object is just a simplified version of the result object. Because of this, several methods of querying results cannot be used.

The methods listed below cannot be used on cached result objects:

  • num_fields()
  • field_names()
  • field_data()
  • free_result()

At the same time, both result_id and conn_id id cannot be used either, because these two ids are only suitable for real-time database operations.

Function reference

$this->db->cache_on() / $this->db->cache_off()

is used to manually enable/disable cache when you don’t want to cache something These two methods can be useful when querying. Example:

// Turn caching on
$this->db->cache_on();
$query = $this->db->query("SELECT * FROM mytable");

// Turn caching off for this one query
$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

$this->db->cache_delete()

Deletes the cache file for a specific page, which is useful when you need to clear the cache after updating your database.

The cache system writes the cache into the corresponding cache file according to the URI of the page you visit. For example, if you are visiting the page example.com/index.php/blog/comments, the cache system will save the cache file. Go to the blog+comments directory. To delete these cache files, you can use:

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

如果你没提供任何参数,将会清除当前 URI 对应的缓存文件。

$this->db->cache_delete_all()

清除所有的缓存文件,例如:

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了[codeigniter 五]、查询缓存,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

After joining the company, I understood what Cache is After joining the company, I understood what Cache is Jul 31, 2023 pm 04:03 PM

The thing is actually like this. At that time, my leader gave me a perf hardware performance monitoring task. During the process of using perf, I entered the command perf list and I saw the following information: My task is to enable these cache events to be counted normally. But the point is, I have no idea what these misses and loads mean.

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Why does using cache increase computer speed? Why does using cache increase computer speed? Dec 09, 2020 am 11:28 AM

Using the cache can increase the speed of the computer because the cache shortens the waiting time of the CPU. Cache is a small but high-speed memory located between the CPU and the main memory DRAM. The function of Cache is to increase the rate of CPU data input and output; Cache has a small capacity but fast speed, while the memory speed is low but has a large capacity. By optimizing the scheduling algorithm, the performance of the system will be greatly improved.

How to reorder multiple columns in Power Query via drag and drop How to reorder multiple columns in Power Query via drag and drop Mar 14, 2024 am 10:55 AM

In this article, we will show you how to reorder multiple columns in PowerQuery by dragging and dropping. Often, when importing data from various sources, columns may not be in the desired order. Reordering columns not only allows you to arrange them in a logical order that suits your analysis or reporting needs, it also improves the readability of your data and speeds up tasks such as filtering, sorting, and performing calculations. How to rearrange multiple columns in Excel? There are many ways to rearrange columns in Excel. You can simply select the column header and drag it to the desired location. However, this approach can become cumbersome when dealing with large tables with many columns. To rearrange columns more efficiently, you can use the enhanced query editor. Enhancing the query

What is cache? What is cache? Nov 25, 2022 am 11:48 AM

Cache is called cache memory. It is a high-speed small-capacity memory between the central processing unit and the main memory. It is generally composed of high-speed SRAM. This kind of local memory is oriented to the CPU. It is introduced to reduce or eliminate the gap between the CPU and the memory. The impact of the speed difference between them on system performance. Cache capacity is small but fast, memory speed is low but capacity is large. By optimizing the scheduling algorithm, the performance of the system will be greatly improved.

React Query database plug-in: how to import and export data React Query database plug-in: how to import and export data Sep 26, 2023 pm 05:37 PM

ReactQuery database plug-in: Methods to implement data import and export, specific code examples are required. With the widespread application of ReactQuery in front-end development, more and more developers are beginning to use it to manage data. In actual development, we often need to export data to local files or import data from local files into the database. In order to implement these functions more conveniently, you can use the ReactQuery database plug-in. The ReactQuery database plugin provides a series of methods

nginx reverse proxy caching tutorial. nginx reverse proxy caching tutorial. Feb 18, 2024 pm 04:48 PM

Here is the tutorial for nginx reverse proxy caching: Install nginx: sudoaptupdatesudoaptinstallnginx Configure reverse proxy: Open nginx configuration file: sudonano/etc/nginx/nginx.conf Add the following configuration in the http block to enable caching: http{...proxy_cache_path /var/cache/nginxlevels=1:2keys_zone=my_cache:10mmax_size=10ginactive=60muse_temp_path=off;proxy_cache

See all articles