This article mainly introduces how Laravel uses Caching to cache data to relieve the pressure on database queries. it analyzes in detail the principle and implementation skills of Laravel framework using Caching cache in the form of project instances, for more information about how Laravel uses Caching to cache data to relieve the pressure on database queries, see the following example. We will share this with you for your reference. The details are as follows:
Yesterday I wanted to cache the homepage of my blog, which is similar to generating static page cache. I asked you in the group how to cache it. I was very busy and didn't reply much, I read the document myself and found the Caching part. I was also impressed before, but I have never been in touch with it. as the name suggests, it is a cache. it must be a little related to my needs, I took a serious look and found that it was indeed too powerful. after several simple steps, I modified the home page and tested it with firebug, improving the resolution time by dozens of milliseconds, of course, is it necessary for someone to laugh? In fact, I think this is necessary, but there are few visitors here (in fact, no one pays back, hey ....), second, I have made few queries on the home page, just one time, that is, getting all the blog posts. if one page contains seven or eight or more queries, I think this effect should be obvious! (Of course, Raymond also mentioned using more advanced dedicated caches (memcached and so on). This requires you to gain control of the server, software or servers can be freely installed only when these caching mechanisms are available. my requirements are simple and I do not have this environment to do so, so I will not consider it here)
Let's talk less about it. let's start with my specific needs:
I. implement data caching on the home page. if there is a cache that has not expired, the database will not be checked. this basically simulates the static page effect (of course, it still needs to be processed by php)
2. implement the function of refreshing the specified cache (only the home page is available here, and the homepage cache is refreshed by a single means. This function is implemented under the admin module.
Specific implementation:
1. check the document and find the module that can help me meet the requirements.
I checked the document and found that a module such as Caching, as its name implies, is a cache. can it help me? let's see first:
1. http://laravel.com/docs/cache/config here is the implementation of laravel's Caching module
2. the document is described as follows:
The Basics Imagine your application displays the ten most popular songs as voted on by your users. Do you really need to look up these ten songs every time someone visits your site? What if you cocould store them for 10 minutes, or even an hour, allowing you to dramatically speed up your application? Laravel's caching makes it simple.
I simply understand:
Suppose your app shows the top 10 popular songs that users vote for. do you really need to check these 10 songs when everyone visits your website? If you want to cache query results for 10 minutes or an hour to accelerate your application, Laravel's caching Cache module can make your work very simple.
Well, from this passage, I have learned that this is fully in line with my current needs. Next, I just need to find the corresponding methods and APIs, one step at a time.
2. learn related APIs
1. continue to the above document, which is shown below:
By default, Laravel is configured to use the file system cache driver. it's ready to go out of the box with no configuration. the file system driver stores cached items as files in the cache directory. if you're satisfied with this driver, no other configuration is required. you're ready to start using it.
I simply understand:
By default, Laravel uses the file system as the cache driver, which can be used without configuration. the file system driver saves cached data to files in the cache directory, if you think it is appropriate, you don't need to do any other configuration to start using it.
Of course, this is also in line with my idea. In fact, I just want to cache the page as a static page file. when the user accesses the page again, it will be OK to directly output the cached static page, if you need more advanced features, you can also use other drivers, such as database drivers, memcached, and redis drivers, which are very powerful!
2. check the use case and find the usage method.
Case document here: http://laravel.com/docs/cache/usage
It can be seen that there are get, put, forever, remember, has, forget and other methods in it. these methods can also be used in a way that can be basically done by "looking at the context ".
The detailed instructions are provided in the usage documentation. I will not elaborate on the usage instructions at a glance. I will only discuss the instructions in the code.
III. specific implementation
1. code before my homepage
class Home_Controller extends Base_Controller { public function get_index() { $posts = Post::with('user') ->join('users', 'users.id', '=', 'posts.post_author') -> order_by('posts.created_at', 'desc') ->get(array('posts.id', 'posts.support', 'posts.against', 'users.username', 'posts.post_author', 'posts.post_title', 'posts.post_body')); $data = array(); foreach($posts as $p){ $data[] = array( 'id' => $p -> id, 'support' => $p -> support, 'against' => $p -> against, 'username'=> $p -> username, 'post_author' => $p -> post_author, 'post_title' => $p -> post_title, 'post_body' => $p -> post_body ); } return View::make('home.index') -> with('posts', $data); }}
This is the controller of my homepage. the function is to retrieve all the blog posts from the blog table and output the results. If no new blog posts are published, there is indeed a lot of unnecessary overhead to look up the table.
2. the code after my modification is as follows:
Class Home_Controller extends Base_Controller {public function get_index () {// add static cache support // cache if no static page cache exists if (! Cache: has ('staticpagecache _ home') {$ data = array (); $ posts = Post: with ('user')-> join ('users ', 'Users. ID', '=', 'posts. post_author ')-> order_by ('posts. created_at ', 'desc')-> get (array ('posts. ID', 'posts. support ', 'posts. against', 'users. username', 'posts. post_author ', 'posts. post_title ', 'posts. post_body '); foreach ($ posts as $ p) {$ data [] = array ('id' => $ p-> id, 'support '=> $ p-> support, 'against' => $ p-> against, 'username' => $ p-> username, 'Post _ author' => $ p-> post_author, 'post _ title' => $ p-> post_title, 'Post _ body' => $ p-> post_body);} $ res = View: make ('Home. index')-> with ('posts', $ data); Cache: forever ('staticpagecache _ home', $ res);} // return the cached data return Cache:: get ('staticpagecache _ home ');}}
Three APIs are used here.
1). Cache: has, which indicates that if the name staticPageCache_home does not exist, the data will be retrieved immediately.
2 ). cache: forever. This document shows the meaning of "permanent Cache", because I usually work very hard. if I post a blog post, you just need to refresh the cache in the background, so you don't need to set the expiration time or the Expiration Time. of course, this is based on your specific needs.
3). Cache: get, which is the Cache that extracts the name staticPageCache_home from the Cache and then returns the response content.
Well, it's that simple. well, a basic cache function is complete, and laravel is really good!
3. added the cache refresh function for the background.
Paste the code, but it is also very simple:
// Refresh the homepage cache (currently only the homepage is supported) public function get_refreshcache () {/* @ var $ GID admin group id */$ GID = 1; if (Auth :: user ()-> gid = 1) {$ data = array (); $ posts = Post: with ('user')-> join ('users ', 'Users. ID', '=', 'posts. post_author ')-> order_by ('posts. created_at ', 'desc')-> get (array ('posts. ID', 'posts. support ', 'posts. against', 'users. username', 'posts. post_author ', 'posts. post_title ', 'posts. post_body ')); Foreach ($ posts as $ p) {$ data [] = array ('id' => $ p-> id, 'support' => $ p-> support, 'against' => $ p-> against, 'username' => $ p-> username, 'post _ author' => $ p-> post_author, 'Post _ title' => $ p-> post_title, 'post _ body' => $ p-> post_body);} $ res = View: make ('Home. index')-> with ('posts', $ data); Cache: forever ('staticpagecache _ home', $ res); return 'homepage Cache refreshed successfully! ';} Return' Sorry, only the administrator group can perform this operation! ';}
I added a project to the background. corresponding to this method, the content of the method is similar to that of the home page. it is as simple as getting data and then Cache: forever refresh the Cache. of course, the above Auth: user () judgment is a simple judgment. only the administrator group can perform the refresh operation.
Well, that's all about it. it's very simple. You are welcome to shoes and correct them!