In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "writing a Laravel easy to use Cache macro method steps", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "write a Laravel easy to use Cache macro method steps" bar!
The caching tool provided by Laravel is easy to use, and some basic uses, such as get,put,forget,forever, are introduced in the manual. I used it like this at first:
The copy code is as follows:
If (! $article = Cache::get ('article_1')) {
$article = Article::find (1)
Cache::forever ('article_1',$article)
}
This is the most basic usage, which automatically determines whether the cache exists, or if it does not exist, it is fetched from the database and written to the cache.
Later, it was found that the model also comes with remember and rememberForever methods, for example:
The copy code is as follows:
$article = Article::rememberForever ('article_1')-> where (' id','=',1)
This has its limitations that data cannot be fully cached in complex queries, such as when associated data is preloaded with with ().
Then I found that Cache can also customize macro methods like Response, so I made the following attempt:
The copy code is as follows:
/ / register cache access macros
Cache::macro ('want',function ($key,$minutes=0,$callback) {
If (! $data = Cache::get ($key)) {
$data = call_user_func ($callback)
If ($minutes = = 0) {
Cache::forever ($key,$data)
} else {
Cache::put ($key,$data,$minutes)
}
}
Return $data
});
This method can be placed in bootstrap/start.php or in App::before () in filter, as convenient as your own project, and see how to use it:
The copy code is as follows:
$id = Input::get ('id')
$article = Cache::want ('article_'.$id,0,function () use ($id) {
Return Article::with ('tags')-> findOrFail ($id, [' id','cid','title','content_html as content','created_at','updated_at'])
});
Thank you for reading, the above is the "preparation of a Laravel easy-to-use Cache macro method steps" of the content, after the study of this article, I believe you on the preparation of a Laravel easy-to-use Cache macro method steps have a deeper understanding of the problem, the specific use of the need for you to practice and verify. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.