Skip to content

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 -*-
# 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

This comment has been minimized.

Copy link

commented Apr 26, 2018

what this script does?

@iyaozhen

This comment has been minimized.

Copy link
Owner Author

commented Mar 24, 2019

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
You can’t perform that action at this time.