如果第一个脚本不行,可以尝试第二个脚本方案!
网站采用了一些方法进行加速(redis\cdn),这样就降低用户访问后端MySQL的压力、以及前端速度不太好的问题,对用户请求到的所有页面进行了缓存,既然数据缓存了,解决用户看到的页面是否为最新页面就提上日程,下面这个脚本就诞生了。SourceByrd's Weblog-https://note.t4x.org/code/fresh-web-cache/
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#!/bin/bash # Author:Byrd # Version:0.1 # Site:note.t4x.org # Contact:root#t4x.org # define category for category in basic code database environment error mobile other project remark route service switch system do for ((i=2;i<15;i++)) do # Define return value RETURN=`curl -o /dev/null -s -k -w "%{http_code}" https://note.t4x.org/$category/page/$i/?r=y` if [ $RETURN -eq 200 ]; then # if return value 200,then flush page and category. curl -o /dev/null -s -k -w "%{http_code}" https://note.t4x.org/$category/page/$i/?r=y >/dev/null 2>&1 curl -o /dev/null -s -k -w "%{http_code}" https://note.t4x.org/$category/?r=y >/dev/null 2>&1 else # if return value not 200,then flush category first page and site page.break curl -o /dev/null -s -k -w "%{http_code}" https://note.t4x.org/$category/?r=y >/dev/null 2>&1 curl -o /dev/null -s -k -w "%{http_code}" https://note.t4x.org/?r=y >/dev/null 2>&1 break fi done done |
将脚本加入定时任务,由于网站不定期更新,更新不频繁,因此每天执行任务即可。
0 1 2 3 4 |
[root@site scripts]# echo '#flush cache' >>/var/spool/cron/root #注释 [root@site scripts]# echo '00 2 * * * /bin/sh /byrd/scripts/flush_site.sh >/dev/null 2>&1' >>/var/spool/cron/root #两点执行计划任务 [root@site scripts]# crontab -l #flush cache 00 2 * * * /bin/sh /byrd/scripts/flush_site.sh >/dev/null 2>&1 |
另外一个思路:SourceByrd's Weblog-https://note.t4x.org/code/fresh-web-cache/
1:redis的主要目的是为了缓存数据;
2:把redis缓存数据清空,然后重新启动redis即可完成;
3:因为个人网站一般凌晨4:00左右访问量最低,且更新周期不快;SourceByrd's Weblog-https://note.t4x.org/code/fresh-web-cache/
这个脚本一样的功效,效果不如上面的脚本,上面的额脚本缺点是没有做文章的判断。SourceByrd's Weblog-https://note.t4x.org/code/fresh-web-cache/
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[root@www scripts]# cat clean_redis.sh #!/bin/bash # Author:Byrd # Version:0.1 # Site:note.t4x.org # Contact:root#t4x.org CDATA=`ls -l /usr/local/redis/cache/dump.rdb | awk '{print $5}'` #取得硬盘缓存数据大小 REDIS_PID=`ps aux | grep redis | grep -v grep | awk '{print $2}'` #取得redis进程id if [ $CDATA -gt 3145728 ]; then >/usr/local/redis/cache/dump.rdb #kill ${REDIS_PID} sleep 3 #/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf else exit 1 fi |
然后定时任务,每周或者每天执行一次即可!SourceByrd's Weblog-https://note.t4x.org/code/fresh-web-cache/ SourceByrd's Weblog-https://note.t4x.org/code/fresh-web-cache/
申明:除非注明Byrd's Blog内容均为原创,未经许可禁止转载!详情请阅读版权申明!
上海市 3F
内容专业,速度够快
上海市 2F
其实redis使用?c=y就能刷新全部缓存:
缓存问题
index-with-redis.php中有注释
Appending a ?c=y to a url deletes the entire cache of the domain, only works when you are logged in
Appending a ?r=y to a url deletes the cache of that url
Submitting a comment deletes the cache of that page
Refreshing (f5) a page deletes the cache of that page
登录后台网站url后面加上?c=y即可刷新整个网站
可以在网站页面后面加上?r=y即可手工刷新
提交评论会自动刷新页面
刷新(f5)页面也可以刷新页面
上海市 B1
@ 张戈 其实还是有问题,前提是登陆后刷新、或者有人评论、又或者F5刷新,大概三个方式。都必须人为操作,我最原始想法是模拟F5来进行,脚本不好操作,就想redis方面来想办法。其实两个脚本都没有很好的实现我想要的效果。
上海市 1F
哇哦,好专业的博客。
河北省张家口市 B1
@ 斯托克笔记 多谢支持哦!~