当前位置: 首页 > news >正文

[搬运]什么叫幂等性?

看到了一篇讲幂等性的文章,我认为把幂等性说得十分清楚。如果你像我一样对幂等性还有困惑,可以仔细看下这篇文章。原文地址:https://www.restapitutorial.com/lessons/idempotency.html

【英文原文】

Idempotence is a funky word that often hooks people. Idempotence is sometimes a confusing concept, at least from the academic definition.

From a RESTful service standpoint, for an operation (or service call) to be idempotent, clients can make that same call repeatedly while producing the same result. In other words, making multiple identical requests has the same effect as making a single request. Note that while idempotent operations produce the same result on the server (no side effects), the response itself may not be the same (e.g. a resource's state may change between requests).

The PUT and DELETE methods are defined to be idempotent. However, there is a caveat on DELETE. The problem with DELETE, which if successful would normally return a 200 (OK) or 204 (No Content), will often return a 404 (Not Found) on subsequent calls, unless the service is configured to "mark" resources for deletion without actually deleting them. However, when the service actually deletes the resource, the next call will not find the resource to delete it and return a 404. However, the state on the server is the same after each DELETE call, but the response is different.

GET, HEAD, OPTIONS and TRACE methods are defined as safe, meaning they are only intended for retrieving data. This makes them idempotent as well since multiple, identical requests will behave the same.

【为了方便,我翻译一下】

“幂等性”是一个经常引人注意的时髦时汇。同时“幂等性”有时也是一个令人很困惑的慨念(至少从其学术定义来看)。

从RESTful服务的角度来说,如果一个operation(或service call)是幂等的,那么客户端以相同的方式重复调用服务就应该产生相同的结果。换句话说,发出多个相同的请求,与发出单个请求具有相同的效果。需要注意的是,虽然幂等性操作会在服务器端产生相同的结果(没有副作用),但操作的response本身可能并不相同(比如资源状态会在不同请求之间发生变化)。

PUT和DELETE方法被定义为幂等的。DELETE操作要特别注意的一个问题是,DELETE操作成功通常会返回200(OK)或204(No Content),但后续相同的DELETE操作可能会返回404(Not Found),除非服务被配置成只标记资源的删除状态而不是真正地删除资源。但是,当服务实际上已删除指定资源时,后续DELETE调用就会找不到该资源,并返回404。多次DELETE调用之后,服务器端的状态是相同的,但返回可以不同。

GET、HEAD、OPTIONS和TRACE方法被定义成安全的,意味着它们只是为了获取数据,多个相同请求时服务器端的表现是相同的,所以它们是天然幂等的。

[感想]

幂等性不同于返回值。

相关文章:

  • python学习之老男孩python全栈第九期_数据库day001知识点总结 —— MySQL操作数据库以及数据表、基本数据类型、基本增删改查、外键定义以及创建...
  • virtualenvwrapper
  • GetDlgItem的用法小结
  • MySQL——索引基础
  • 用Go语言实现微信支付SDK
  • 文档和元素的几何滚动
  • Java编程——数据库两大神器:索引和锁
  • 80% UI 初学者走过的弯路,你走了几条?
  • Numpy数值计算基础
  • MYSQL设置查询内存表大小
  • 聊聊rocketmq的FileAppender
  • JS高级
  • MongoDB4.0构建分布式分片群集
  • (六)Hibernate的二级缓存
  • 用 PreparedStatement 向 SqlServer 中一次性插入多条记录
  • crontab执行失败的多种原因
  • Hibernate【inverse和cascade属性】知识要点
  • Linux编程学习笔记 | Linux IO学习[1] - 文件IO
  • Linux链接文件
  • php中curl和soap方式请求服务超时问题
  • React Native移动开发实战-3-实现页面间的数据传递
  • vue从入门到进阶:计算属性computed与侦听器watch(三)
  • 基于Dubbo+ZooKeeper的分布式服务的实现
  • 基于阿里云移动推送的移动应用推送模式最佳实践
  • 如何打造100亿SDK累计覆盖量的大数据系统
  • 王永庆:技术创新改变教育未来
  • 微信公众号开发小记——5.python微信红包
  • 为视图添加丝滑的水波纹
  • 学习HTTP相关知识笔记
  • 原生 js 实现移动端 Touch 滑动反弹
  • 【运维趟坑回忆录 开篇】初入初创, 一脸懵
  • 不要一棍子打翻所有黑盒模型,其实可以让它们发挥作用 ...
  • ​LeetCode解法汇总518. 零钱兑换 II
  • #### go map 底层结构 ####
  • #define用法
  • (1)(1.13) SiK无线电高级配置(五)
  • (C语言)字符分类函数
  • (pytorch进阶之路)CLIP模型 实现图像多模态检索任务
  • (分布式缓存)Redis哨兵
  • (一)【Jmeter】JDK及Jmeter的安装部署及简单配置
  • (轉貼) 資訊相關科系畢業的學生,未來會是什麼樣子?(Misc)
  • * CIL library *(* CIL module *) : error LNK2005: _DllMain@12 already defined in mfcs120u.lib(dllmodu
  • .net core 微服务_.NET Core 3.0中用 Code-First 方式创建 gRPC 服务与客户端
  • .NET Framework 服务实现监控可观测性最佳实践
  • .NET 表达式计算:Expression Evaluator
  • .net6Api后台+uniapp导出Excel
  • @entity 不限字节长度的类型_一文读懂Redis常见对象类型的底层数据结构
  • @FeignClient注解,fallback和fallbackFactory
  • [2019/05/17]解决springboot测试List接口时JSON传参异常
  • [ACL2022] Text Smoothing: 一种在文本分类任务上的数据增强方法
  • [android] 练习PopupWindow实现对话框
  • [AutoSar]BSW_Com07 CAN报文接收流程的函数调用
  • [C++]C++入门--引用
  • [C++]打开新世界的大门之C++入门
  • [C++]拼图游戏