Discuz! X 上传头像到UCenter时自动刷新CDN服务器上的头像缓存
应用场景:为减少源站流量,我们可以永久缓存用户的头像,只在用户上传新头像时,才刷新CDN服务器上的缓存。本文案例中的CDN服务器环境如下,仅供参考:Nginx,已安装 cache-purge 扩展,通过请求 $host/purge/$uri 的方式清除缓存。所使用的域名与源站一致。1、首先在CDN服务器中配置清除规则,将源站服务器IP设置为允许;2、打开 uc_server/data/config.inc.php ,在末尾加入CDN服务器的IP地址,便于统一调用和修改。define('UC_CDNIP', '123.123.123.123');3、打开 uc_server/control/user.php ,添加上传头像后自动清除CDN缓存的功能。查找: $bigavatarfile = UC_DATADIR.'./avatar/'.$this->get_avatar($uid, 'big', $avatartype); $middleavatarfile = UC_DATADIR.'./avatar/'.$this->get_avatar($uid, 'middle', $avatartype); $smallavatarfile = UC_DATADIR.'./avatar/'.$this->get_avatar($uid, 'small', $avatartype);替换为: $getavatar_big = $this->get_avatar($uid, 'big', $avatartype); $getavatar_middle = $this->get_avatar($uid, 'middle', $avatartype); $getavatar_small = $this->get_avatar($uid, 'small', $avatartype); $bigavatarfile = UC_DATADIR.'./avatar/'.$getavatar_big; $middleavatarfile = UC_DATADIR.'./avatar/'.$getavatar_middle; $smallavatarfile = UC_DATADIR.'./avatar/'.$getavatar_small;这一步是取得头像路径。接下来查找: return '<?xml version="1.0" ?><root><face success="1"/></root>';在前方加入: $uccdnip = UC_CDNIP; //在 data/config.inc.php 中设置UC的CDN服务器IP,用于通知刷新头像 $avatarurl = 'https://'.$uccdnip.'/purge/data/avatar/'; $uchostname = stream_context_create(array('http' => array('header' => 'Host: '.$_SERVER['SERVER_NAME']))); file_get_contents($avatarurl.$getavatar_big, NULL, $uchostname); file_get_contents($avatarurl.$getavatar_middle, NULL, $uchostname); file_get_contents($avatarurl.$getavatar_small, NULL, $uchostname);上传覆盖即可。附:若你要在用户上传头像后点击“完成”时自动刷新头像上传页面,请查看 wenzhang-2040.html 。4、管理员清除头像时,同步清除CDN服务器上的头像缓存。打开 uc_server/model/user.php ,查找: foreach((array)$uidsarr as $uid) { file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->base->get_avatar($uid, 'big', 'real')) && unlink($avatar_file); file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->base->get_avatar($uid, 'middle', 'real')) && unlink($avatar_file); file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->base->get_avatar($uid, 'small', 'real')) && unlink($avatar_file); file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->base->get_avatar($uid, 'big')) && unlink($avatar_file); file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->base->get_avatar($uid, 'middle')) && unlink($avatar_file); file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->base->get_avatar($uid, 'small')) && unlink($avatar_file); }替换为: $uccdnip = UC_CDNIP; //在 data/config.inc.php 中设置UC的CDN服务器IP,用于通知刷新头像 $uchostname = stream_context_create(array('http' => array('header' => 'Host: '.$_SERVER['SERVER_NAME']))); foreach((array)$uidsarr as $uid) { file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->base->get_avatar($uid, 'big', 'real')) && unlink($avatar_file) && file_get_contents(str_replace(UC_DATADIR.'./','https://'.$uccdnip.'/purge/data/',$avatar_file), NULL, $uchostname); file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->base->get_avatar($uid, 'middle', 'real')) && unlink($avatar_file) && file_get_contents(str_replace(UC_DATADIR.'./','https://'.$uccdnip.'/purge/data/',$avatar_file), NULL, $uchostname); file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->base->get_avatar($uid, 'small', 'real')) && unlink($avatar_file) && file_get_contents(str_replace(UC_DATADIR.'./','https://'.$uccdnip.'/purge/data/',$avatar_file), NULL, $uchostname); file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->base->get_avatar($uid, 'big')) && unlink($avatar_file) && file_get_contents(str_replace(UC_DATADIR.'./','https://'.$uccdnip.'/purge/data/',$avatar_file), NULL, $uchostname); file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->base->get_avatar($uid, 'middle')) && unlink($avatar_file) && file_get_contents(str_replace(UC_DATADIR.'./','https://'.$uccdnip.'/purge/data/',$avatar_file), NULL, $uchostname); file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->base->get_avatar($uid, 'small')) && unlink($avatar_file) && file_get_contents(str_replace(UC_DATADIR.'./','https://'.$uccdnip.'/purge/data/',$avatar_file), NULL, $uchostname); }上传覆盖即可。
页:
[1]