Skip to content

Instantly share code, notes, and snippets.

@iyaozhen
Last active March 24, 2019 10:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iyaozhen/53e6a57a2f7e945ba1161953959a7cb2 to your computer and use it in GitHub Desktop.
Save iyaozhen/53e6a57a2f7e945ba1161953959a7cb2 to your computer and use it in GitHub Desktop.
according to sitemap.xml rebuild cache @Cache Enabler – WordPress Caching
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# https://cn.wordpress.org/plugins/cache-enabler/
# 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
Copy link

vibhi19 commented Apr 26, 2018

what this script does?

@iyaozhen
Copy link
Author

what this script does?

for https://cn.wordpress.org/plugins/cache-enabler/
refresh cache

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