- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Laravel使用Caching缓存数据减轻数据库查询压力的方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
本文实例讲述了Laravel使用Caching缓存数据减轻数据库查询压力的方法。分享给大家供大家参考,具体如下:
昨天想把自己博客的首页做一下缓存,达到类似于生成静态页缓存的效果,在群里问了大家怎么做缓存,都挺忙的没多少回复,我就自己去看了看文档,发现了Caching这个部分,其实之前也有印象,但是没具体接触过,顾名思义,就是缓存了,那肯定和我的需求有点联系,我就认真看了看,发现的确是太强大了,经过很简单的几个步骤,我就改装好了首页,用firebug测试了一下,提高了几十毫秒解析时间,当然了有人会笑这有必要吗,岂不是闲的蛋疼?其实我想这是有必要的,只是在我这里一来访问人少(其实根本没人还,嘿嘿....),二来我在首页里做的查询目前还挺少,就一次,就是取得所有博文,如果一个页面里面有个七八次乃至十多次查询,我想这个效果应该就很明显了吧!(当然了,Raymond哥还有提到用更高级的专用缓存去做(memcached之类吧貌似),这是要自己能取得服务器控制权,能自由安装软件或者服务器本来就有这些缓存机制的情况下才能实现的,我需求比较简单,也没有这个环境去做,所以这里就不考虑了) 。
闲话少说,开始吧,先说说我的具体需求
一. 实现首页的数据缓存,如果有没过期的缓存,就不查数据库,这样基本模拟出静态页的效果(当然了,其实还是要经过php处理的) 。
二. 实现刷新指定缓存的功能(这里只有首页,就单指刷新首页缓存了,这个功能,我做到了admin模块下 。
具体实现
一. 查阅文档,找到能帮我实现需求的模块 。
我查了一下文档,发现了有Caching这样一个模块,顾名思义,就是缓存了,那它能否帮到我呢,看看先
1. http://laravel.com/docs/cache/config 这里是laravel的Caching模块的实现 。
2. 文档中有如下描述
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 could store them for 10 minutes, or even an hour, allowing you to dramatically speed up your application? Laravel's caching makes it simple. 。
我简单理解为
假设你的应用展示了用户投票最多的10首流行歌曲,你真的需要在每个人访问你的网站的时候都去查一遍这10首歌吗?如果你想按10分钟或者是一小时的频率来缓存查询结果来加速你的应用,Laravel 的 caching缓存模块能将使工作变得异常简单. 。
嗯,从这段话,我已经了解到这完全符合我现在的需求了,接下来我只需要找到对应的使用方法和API,一步一步来就行了. 。
二. 学习相应API等 。
1. 还是上面文档,里面接着向下看,有如下描述
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. 。
我简单理解为
默认情况下,Laravel使用文件系统作为缓存的驱动, 这是不需配置就可使用的, 文件系统驱动会将缓存的数据存入缓存目录下的文件里面去, 如果你觉得合适的话不需要做任何其他的配置直接开始用就行了. 。
当然了, 这也是符合我的想法的, 其实我就是想把页面缓存成静态页文件, 用户再次访问时直接输出缓存的静态页就ok了, 如果需要更高级的需求, 还可以使用其他的驱动,有数据库驱动, memcached, redis驱动等, 很好很强大.
2. 接下来查看用例,找到使用方法 。
用例文档在这: http://laravel.com/docs/cache/usage 。
可以看出, 里面有 get, put, forever, remember, has, forget 等方法,这些方法使用也是基本上能 "望文生义" 就能搞定的,呵呵 。
具体使用方法文档里面已经说的够详细, 使用方法一目了然我就不细说了, 只在代码里面说吧 。
三. 具体实现 。
1. 我首页之前的代码 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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
);
}
}
|
这是我首页的controller,作用只有一个, 就是从博文表里面取得所有博文, 然后输出, 每次有人访问, 都要查表, 如果没有发表新的博文, 也要查表, 的确有很多不必要的开销 。
2. 下面是我改装之后的代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
class
Home_Controller
extends
Base_Controller {
public
function
get_index() {
// 添加静态缓存支持
// 如果不存在静态页缓存就立即缓存
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
Cache::get(
'staticPageCache_home'
);
}
}
|
这里我用到了三个api 。
1). Cache::has ,这个判断是说如果当前不存在 staticPageCache_home 这个名字的缓存, 就立即去取数据 。
2). Cache::forever, 这个从用例文档里面可知是"永久缓存"的意思, 因为我一般都是很勤劳的,如果发表了博文,自己再去后台立即刷新一下缓存就好了, 所以不需要设置过期啊失效时间之类的, 当然这个是要按各自的具体需求来的 。
3). Cache::get , 这句是从缓存里面取出 staticPageCache_home 这个名字的缓存, 然后作为响应内容返回 。
嗯, 就这么简单, 呵呵, 一个基本的缓存功能就完成了, laravel的确是不错地.
3. 为后台添加刷新缓存功能 。
还是贴代码吧, 不过也很简单
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
// 刷新首页缓存(暂时只支持首页)
public
function
get_refreshcache() {
/*
@var $GID admin组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
'刷新首页缓存成功!'
;
}
return
'对不起,只有管理员组才可进行此操作!'
;
}
|
我给后台添加了一个项目, 对应这个方法, 方法内容和首页的大同小异, 取数据, 然后Cache::forever 刷新一下缓存,就这么简单,当然了,上面的Auth::user() 判断是个简单的判断,只有管理员组才能进行刷新操作,呵呵 。
嗯, 全部内容就这么多, 很简单, 欢迎童鞋们拍砖指正.
希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助.
最后此篇关于Laravel使用Caching缓存数据减轻数据库查询压力的方法的文章就讲到这里了,如果你想了解更多关于Laravel使用Caching缓存数据减轻数据库查询压力的方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
已关闭。此问题旨在寻求有关书籍、工具、软件库等的建议。不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以
我在 tomcat 服务器上部署了一个 Web 应用程序。我通过 POST 方法上传文件,然后上传文件被转码为另一种类型(例如 3gp 到 flv)。我如何测试,有多少并发上传和转码我的 Web 应用
我想知道是否有任何方法可以获取单击时的压力级别(用户单击鼠标键/按钮时产生的压力)。有资源或链接吗? 抱歉我的英语不好,希望我的问题很清楚,希望不仅仅是乌托邦! 最佳答案 我意识到我正在用一些当时不相
我写信是为了问一个关于 WCF 性能的问题。 1。背景 我们有一个在 .NET 3.5 上运行的客户端-服务器系统。服务器是 C# 服务,客户端是 silverlight 应用。 我写了一个压力测
我想编写一份包含 NMS 排序的报告。我想在报告中包含代码,但不包含运行压力。 我试过 message=FALSE, warning=FALSE, results='hide' 但它仍然包含在报告中。
我今天第一次尝试使用 cassandra-stress 工具。尽管我能够运行该工具,但输出中会显示很多“无法通过 JMX 连接;未收集这些统计信息”消息 命令 cassandra-stress use
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 9 年前。 Improve this ques
我希望通过一遍又一遍地不断访问网址列表来模拟网络服务器上的重负载! 最初,我计划使用 wget/curl 编写一个简单的脚本,但由于该站点使用大量 ajax 请求来完成大部分繁重的工作,因此这不是一个
我最近创建了一个回合制游戏服务器,可以接受数以万计的同时客户端连接(长话短说 - Linux 上的 epoll)。通信基于简单、自定义、基于线路的协议(protocol)。该服务器允许客户端连接、寻找
Stress-ng:我们可以使用stress-ng测试RAM吗?用于在 MIPS 32 设备上测试 RAM 的命令是什么? 最佳答案 在stress-ng中有许多基于内存的压力源: stress-ng
我正在 iPhone 上测试我的简单 OpenGL ES 实现(2D 游戏),并且在使用分析器时我注意到渲染利用率很高。这些是事实: 我以 60 fps 的速度仅显示一个预加载的大型纹理(512x51
我正在 canvas 上编写应该支持平板电脑和触摸压力的网络应用程序。不过,如果我没记错的话,我相信只有IE10支持这些指针事件。我想知道 chrome 和 firefox 是否会很快支持它? 最佳答
Stress-ng:如何使用 execv 在 C 或 Cpp 中编写应用程序来调用 stress-ng 命令以在 MIPS 中进行 CPU 和内存测试,并在成功或失败时返回其状态?给定一个可执行的 s
我是一名优秀的程序员,十分优秀!