Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

上传/转存图像功能缓存文件名变更为缓存文件摘要 #99

Merged
merged 3 commits into from
Feb 17, 2025

Conversation

EvenTiming
Copy link
Contributor

原本的代码中,在反复上传同一张图像但是不同名图像的时候会反复存储重复图像。
将原本图像缓存的<图像名/外链图像地址,服务器图像地址>键值对更改为<图像摘要,服务器图像地址>
图像摘要使用SHA256算法。
不仅外链图像转存支持缓存,限制上传图像也支持转存。

使用postman进行上传图像测试,不同名图像在缓存时间内都会指向同一个服务器上的图像。
0e1e41967647cef435ddfb30b9bfe9d
164de5bfdda8b7a8920cc37ae2aacd4

Copy link
Collaborator

@liuyueyi liuyueyi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

挺好的改进思路,辛苦再确认一下计算图片摘要这里,从流中读取数据然后返回摘要,此时流被读取了一次,后续是否还可以继续读取inputStream的数据? (即需要确认一下InputStream是否支持重复读)

额外说一下:生产使用的oss保存图片,服务器在图片上传oss时,使用的是md5作为文件名,因此oss端,同一张图片实际上只会存储一次,并不会出现重复存储

@EvenTiming
Copy link
Contributor Author

挺好的改进思路,辛苦再确认一下计算图片摘要这里,从流中读取数据然后返回摘要,此时流被读取了一次,后续是否还可以继续读取inputStream的数据? (即需要确认一下InputStream是否支持重复读)

额外说一下:生产使用的oss保存图片,服务器在图片上传oss时,使用的是md5作为文件名,因此oss端,同一张图片实际上只会存储一次,并不会出现重复存储

感谢提醒,又学到了一个知识点。在图像转存的时候确实存在无法重复使用的情况,于是我在摘要算法上去包装了一下InputStream
。具体使用的方法来自 https://hhui.top/tutorial/java/basic/12.InputStream%E9%87%8D%E5%A4%8D%E4%BD%BF%E7%94%A8%E5%B0%8F%E6%8A%80%E5%B7%A7.html
感谢提醒,感谢六年前的博客。经过测试已经支持重复使用。

@liuyueyi liuyueyi merged commit 85c2163 into itwanger:main Feb 17, 2025
@liuyueyi
Copy link
Collaborator

可以考虑在实现层再进行一次代码优化,比如:

  1. 重复代码的提取
    image

  2. 缓存的key可以考虑使用md5替换现在的sha256, 主要原因是阿里oss的存储图片名就是计算的md5,我们尽量确保在传图的一个生命周期内,只计算一次md5/sha256,提高接口性能
    image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants