1. 用 ids 取数据的接口返回 Map
使用
- public Map<Integer, User> getUserByIds(Collection<Integer> ids) {
- ...
- }
而不是
- public List<User> getUserByIds(Collection<Integer> ids) {
- ...
- }
原因是后者会引入一些困惑 :
1>id 可以重复吗? 重复时会去重吗?
2 > 返回的 List 的顺序和 ids 相关吗?
3 > 某个 id 不存在, List 里是跳过还是 null?
2. 按 ids 取数据的 RPC 接口需要校验长度
接口实现时应该做 ids 最大长度校验. 若超出则直接抛异常, 否则可能会因性能上的问题拖垮整个服务.
- import BoundExceededException;
- Map<Integer, User> getUserbyIds(List<Integer> ids) throws BoundExceededException {
- if (ids.size()> bound) {
- throw new BoundExceededException();
- }
- ...
- }
来源: http://www.bubuko.com/infodetail-3489106.html