Shell regularly clears website cache data

Source: Internet
Author: User
Tags cdata curl flush redis

If the first script does not work, you can try the second script solution!

The website uses some methods for acceleration (redis \ cdn), which reduces the pressure on users to access backend mysql and the front-end speed, all pages requested by the user are cached. Since the data is cached, the following script is generated to check whether the page you see is the latest page.

#! /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

Add the script to the scheduled task. Because the website is occasionally updated and the update is not frequent, you can execute the task every day.

[Root @ site scripts] # echo '# flush cache'>/var/spool/cron/root # Comment
[Root @ site scripts] # echo '00 2 ***/bin/sh/byrd/scripts/flush_site.sh>/dev/null 2> & 1'>/var/spool /cron/root # two-point execution plan task
[Root @ site scripts] # crontab-l
# Flush cache
00 2 ***/bin/sh/byrd/scripts/flush_site.sh>/dev/null 2> & 1

Another idea:

1: The main purpose of redis is to cache data;
2: clear redis Cache data and restart redis;
3: personal websites generally have the lowest access volume at around a.m. and the update cycle is not fast;
This script has the same effect as the above script. The disadvantage of the above script is that there is no judgment.

[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} ''# obtain the disk cache data size
REDIS_PID = 'PS aux | grep redis | grep-v grep | awk' {print $2} ''# obtain the redis process 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

The scheduled task can be executed once a week or every day!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.