Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
according to sitemap.xml rebuild cache @Cache Enabler – WordPress Caching
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# rebuild cache according to sitemap.xml
import os
import requests
import xml.etree.ElementTree as ET
import time
import shutil
CACHE_DIR = "/var/www/wordpress/wp-content/cache/cache-enabler/"
SITEMAP_URL = "https://iyaozhen.com/sitemap.xml"
if os.path.isdir(CACHE_DIR):
shutil.rmtree(CACHE_DIR)
try:
sitemap = requests.get(SITEMAP_URL)
except (requests.HTTPError, requests.ConnectionError) as e:
print("get sitemap.xml error: %s" % e)
else:
urlset = ET.fromstring(sitemap.content)
print("start rebuild cache at %s" % time.asctime())
for url in urlset:
loc = url[0].text
try:
requests.get(loc)
except (requests.HTTPError, requests.ConnectionError) as e:
print("request %s error: %s" % (loc, e))
else:
print("hit %s" % loc)
finally:
time.sleep(0.1)
else:
print("no cache dir: %s" % CACHE_DIR)
@vibhi19

This comment has been minimized.

Show comment
Hide comment
@vibhi19

vibhi19 Apr 26, 2018

what this script does?

vibhi19 commented Apr 26, 2018

what this script does?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment