shell实现定期清理网站缓存数据库_shell实现定期清理网站缓存数据

更新时间:2020-10-09    来源:redis    手机版     字体:

【www.bbyears.com--redis】

如果第一个脚本不行,可以尝试第二个脚本方案!

网站采用了一些方法进行加速(redis\cdn),这样就降低用户访问后端mysql的压力、以及前端速度不太好的问题,对用户请求到的所有页面进行了缓存,既然数据缓存了,解决用户看到的页面是否为最新页面就提上日程,下面这个脚本就诞生了。

#!/bin/bash
# Author:Byrd
# Version:0.1
# Site:www.111cn.net
# Contact:root#111cn.net
# 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}" http://www.111cn.net /$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}" http://www.111cn.net /$category/page/$i/?r=y >/dev/null 2>&1
            curl -o /dev/null -s -k -w "%{http_code}" http://www.111cn.net /$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}" http://www.111cn.net /$category/?r=y >/dev/null 2>&1
            curl -o /dev/null -s -k -w "%{http_code}" http://www.111cn.net /?r=y >/dev/null 2>&1
            break
        fi
    done
done

将脚本加入定时任务,由于网站不定期更新,更新不频繁,因此每天执行任务即可。

[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

另外一个思路:

1:redis的主要目的是为了缓存数据;
2:把redis缓存数据清空,然后重新启动redis即可完成;
3:因为个人网站一般凌晨4:00左右访问量最低,且更新周期不快;
这个脚本一样的功效,效果不如上面的脚本,上面的额脚本缺点是没有做文章的判断。

[root@www scripts]# cat clean_redis.sh
#!/bin/bash
# Author:Byrd
# Version:0.1
# Site:www.111cn.net
# Contact:root#111cn.net
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

然后定时任务,每周或者每天执行一次即可!

本文来源:http://www.bbyears.com/shujuku/103664.html

热门标签

更多>>

本类排行