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

[codeigniter 5], query cache

WBOY
Release: 2016-07-30 13:31:39
Original
947 people have browsed it

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教程有兴趣的朋友有所帮助。

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