Discuz! X 上传头像到UCenter时自动刷新CDN服务器上的头像缓存-Discuz教程下载

Discuz! X 上传头像到UCenter时自动刷新CDN服务器上的头像缓存

开通本站Svip会员,全站资源免费下
应用场景:为减少源站流量,我们可以永久缓存用户的头像,只在用户上传新头像时,才刷新CDN服务器上的缓存。
本文案例中的CDN服务器环境如下,仅供参考:
Nginx,已安装 cache-purge 扩展,通过请求 $host/purge/$uri 的方式清除缓存。所使用的域名与源站一致。
1、首先在CDN服务器中配置清除规则,将源站服务器IP设置为允许;
2、打开 uc_server/data/config.inc.php ,在末尾加入CDN服务器的IP地址,便于统一调用和修改。
define('UC_CDN_DOMAIN', 'www.xxx.com');
define('UC_CDN_IP', '123.123.123.123');
3、打开 uc_server/control/user.php ,添加上传头像后自动清除CDN缓存的功能。
查找:
  1.                 $bigavatarfile = UC_DATADIR.'./avatar/'.$this->get_avatar($uid, 'big', $avatartype);
  2.                 $middleavatarfile = UC_DATADIR.'./avatar/'.$this->get_avatar($uid, 'middle', $avatartype);
  3.                 $smallavatarfile = UC_DATADIR.'./avatar/'.$this->get_avatar($uid, 'small', $avatartype);
复制代码

替换为:
  1.                 $getavatar_big = $this->get_avatar($uid, 'big', $avatartype);
  2.                 $getavatar_middle = $this->get_avatar($uid, 'middle', $avatartype);
  3.                 $getavatar_small = $this->get_avatar($uid, 'small', $avatartype);
  4.                 $bigavatarfile = UC_DATADIR.'./avatar/'.$getavatar_big;
  5.                 $middleavatarfile = UC_DATADIR.'./avatar/'.$getavatar_middle;
  6.                 $smallavatarfile = UC_DATADIR.'./avatar/'.$getavatar_small;
复制代码

这一步是取得头像路径。接下来查找:
  1.                         return '<?xml version="1.0" ?><root><face success="1"/></root>';
复制代码

在前方加入:
  1.                         $avatarurl = 'http://'.UC_CDN_IP.'/purge/data/avatar/';
  2.                         $uchostname = stream_context_create(array('http' => array('header' => 'Host: '.UC_CDN_DOMAIN)));
  3.                         file_get_contents($avatarurl.$getavatar_big, NULL, $uchostname);
  4.                         file_get_contents($avatarurl.$getavatar_middle, NULL, $uchostname);
  5.                         file_get_contents($avatarurl.$getavatar_small, NULL, $uchostname);
复制代码

上传覆盖即可。
在上传头像的页面加入下面的JS,在上传头像后就会自动刷新当前面,方法名updateavatar不能改变
  1.     function updateavatar() {
  2.         window.location.reload();
  3.     }   
复制代码

4、管理员清除头像时,同步清除CDN服务器上的头像缓存。
打开 uc_server/model/user.php ,查找:
  1.                 foreach((array)$uidsarr as $uid) {
  2.                         file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->base->get_avatar($uid, 'big', 'real')) && unlink($avatar_file);
  3.                         file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->base->get_avatar($uid, 'middle', 'real')) && unlink($avatar_file);
  4.                         file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->base->get_avatar($uid, 'small', 'real')) && unlink($avatar_file);
  5.                         file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->base->get_avatar($uid, 'big')) && unlink($avatar_file);
  6.                         file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->base->get_avatar($uid, 'middle')) && unlink($avatar_file);
  7.                         file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->base->get_avatar($uid, 'small')) && unlink($avatar_file);
  8.                 }
复制代码

替换为:
  1.                 $avatarurl = 'http://'.UC_CDN_IP.'/purge/data/';
  2.                 $uchostname = stream_context_create(array('http' => array('header' => 'Host: '.UC_CDN_DOMAIN)));
  3.                 foreach((array)$uidsarr as $uid) {
  4.                         file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->base->get_avatar($uid, 'big', 'real')) && unlink($avatar_file) && file_get_contents(str_replace(UC_DATADIR.'./', $avatarurl, $avatar_file), NULL, $uchostname);
  5.                         file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->base->get_avatar($uid, 'middle', 'real')) && unlink($avatar_file) && file_get_contents(str_replace(UC_DATADIR.'./', $avatarurl, $avatar_file), NULL, $uchostname);
  6.                         file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->base->get_avatar($uid, 'small', 'real')) && unlink($avatar_file) && file_get_contents(str_replace(UC_DATADIR.'./', $avatarurl, $avatar_file), NULL, $uchostname);
  7.                         file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->base->get_avatar($uid, 'big')) && unlink($avatar_file) && file_get_contents(str_replace(UC_DATADIR.'./', $avatarurl, $avatar_file), NULL, $uchostname);
  8.                         file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->base->get_avatar($uid, 'middle')) && unlink($avatar_file) && file_get_contents(str_replace(UC_DATADIR.'./', $avatarurl, $avatar_file), NULL, $uchostname);
  9.                         file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->base->get_avatar($uid, 'small')) && unlink($avatar_file) && file_get_contents(str_replace(UC_DATADIR.'./', $avatarurl, $avatar_file), NULL, $uchostname);
  10.                 }
复制代码

上传覆盖即可。

全部评论 0

您需要登录后才可以回帖 立即登录
登录
0
0
0
返回顶部