找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 326|回复: 0

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

[复制链接]
发表于 2018-8-10 20:07:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
应用场景:为减少源站流量,我们可以永久缓存用户的头像,只在用户上传新头像时,才刷新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;
这一步是取得头像路径。接下来查找:
                        [color=#e2**]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 ,查找:
                [color=#e2**]foreach((array)$uidsarr [color=#e2**]as $uid) {                        file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->[color=#e2**]base->get_avatar($uid, 'big', 'real')) && unlink($avatar_file);                        file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->[color=#e2**]base->get_avatar($uid, 'middle', 'real')) && unlink($avatar_file);                        file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->[color=#e2**]base->get_avatar($uid, 'small', 'real')) && unlink($avatar_file);                        file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->[color=#e2**]base->get_avatar($uid, 'big')) && unlink($avatar_file);                        file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->[color=#e2**]base->get_avatar($uid, 'middle')) && unlink($avatar_file);                        file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->[color=#e2**]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'])));                [color=#e2**]foreach((array)$uidsarr [color=#e2**]as $uid) {                        file_exists($avatar_file = UC_DATADIR.'./avatar/'.$this->[color=#e2**]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->[color=#e2**]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->[color=#e2**]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->[color=#e2**]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->[color=#e2**]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->[color=#e2**]base->get_avatar($uid, 'small')) && unlink($avatar_file) && file_get_contents(str_replace(UC_DATADIR.'./','https://'.$uccdnip.'/purge/data/',$avatar_file), NULL, $uchostname);                }
上传覆盖即可。

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

返回顶部快速回复上一主题下一主题返回列表找客服手机访问

QQ|申请友链|Archiver|小黑屋|手机版|网站地图|谷姐论坛 谷姐提供 ( 渝ICP备2021009247号-1 )

GMT+8, 2024-4-30 21:44

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表