html - 根据用户id实现论坛用户头像显示的最佳实现
迷茫
迷茫 2017-04-17 15:18:56
0
2
462

我自己做的一个论坛里需要显示用户头像,但是觉得不太可能在每一个回复中都保存用户头像文件名,因为总不能用户修改头像之后再去修改数据里所有他曾经回复过的内容。

所以我在服务器(node+express)中做了一个专门用于显示头像的路由,get方式,路由中获取头像图片文件返回给用户,并且设置了一个缓存时间来降低服务器负荷,于是现在所有的用户头像url可以用用户id表示了。

然而这出现了一个问题,就是用户修改头像之后除非手动清理缓存或者等缓存过期,不然总是使用缓存的头像

那么如果避免缓存的问题呢?

我第一个想到的是设置must-revalidation,但是似乎并没有作用,而且每次都revalidation会不会导致服务器仍然要读取图片文件,导致磁盘性能下降?

第二种方式我考虑将用户头像文件名存在用户信息里,进入显示头像的路由之后重定向到实际的URI,然后配合nginx做静态资源服务和缓存。但是这似乎就增加了一次服务器访问。

不知道大家有什么更好的主意

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(2)
大家讲道理

Maybe I want it to be simple, or maybe you want it to be complicated.

The avatar is related to the user, and of course it is stored directly in the user table.

The user modifies the avatar and uploads the new avatar to your server. You can get the image and rename it, and then modify the user's corresponding avatar field to point to the new image.

Wouldn’t the user get a new avatar as long as they refresh the page?

伊谢尔伦

I think the main problem is cache updates.

Browsers have two types of cache, Expires and LastModified/Etag.

Expires is based on the time range and obtains data directly from the cache before expiration. There is no network request, while Etag/LastModified will send a network request to verify the data. If there is no update, the cache will be used. If there is an update, the latest data will be obtained.

All solution is to set Etag/LastModified. If image updates do not require high immediacy, you can combine the two caches and set Expires for a certain period of time.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template