什么是幂等运算?
当前回答
幂等操作:多次执行没有副作用的操作。 示例:从数据资源检索值并打印值的操作 非幂等操作:多次执行会造成伤害的操作。(当它们改变某些值或状态时) 示例:从银行账户提款的操作
其他回答
只是想提出一个真实的用例来证明幂等性。在JavaScript中,假设你定义了一堆模型类(就像MVC模型一样)。它的实现方式通常是这样的(基本示例):
function model(name) {
function Model() {
this.name = name;
}
return Model;
}
然后你可以像这样定义新的类:
var User = model('user');
var Article = model('article');
但如果你试图通过模型(' User ')从代码的其他地方获取User类,它会失败:
var User = model('user');
// ... then somewhere else in the code (in a different scope)
var User = model('user');
这两个User构造函数是不同的。也就是说,
model('user') !== model('user');
为了让它是幂等的,你只需要添加一些缓存机制,像这样:
var collection = {};
function model(name) {
if (collection[name])
return collection[name];
function Model() {
this.name = name;
}
collection[name] = Model;
return Model;
}
通过添加缓存,每次你建模('user')它都是同一个对象,所以它是幂等的。所以:
model('user') === model('user');
无论调用该操作多少次,结果都是相同的。
幂等操作是一种可以应用多次而不改变结果(即系统状态)的操作、动作或请求,超出初始应用。
示例(web应用上下文):
IDEMPOTENT: Making multiple identical requests has the same effect as making a single request. A message in an email messaging system is opened and marked as "opened" in the database. One can open the message many times but this repeated action will only ever result in that message being in the "opened" state. This is an idempotent operation. The first time one PUTs an update to a resource using information that does not match the resource (the state of the system), the state of the system will change as the resource is updated. If one PUTs the same update to a resource repeatedly then the information in the update will match the information already in the system upon every PUT, and no change to the state of the system will occur. Repeated PUTs with the same information are idempotent: the first PUT may change the state of the system, subsequent PUTs should not.
非幂等性: 如果一个操作总是导致状态的变化,比如反复向用户发送相同的消息,导致每次都发送新消息并存储在数据库中,我们称该操作为NON-IDEMPOTENT。
NULLIPOTENT: 如果一个操作没有副作用,就像仅仅在网页上显示信息而没有对数据库进行任何更改(换句话说,你只是在读取数据库),我们说这个操作是NULLIPOTENT。所有get都应该是无效的。
当谈论系统的状态时,我们显然忽略了无害的和不可避免的影响,如日志和诊断。
my 5c: In integration and networking the idempotency is very important. Several examples from real-life: Imagine, we deliver data to the target system. Data delivered by a sequence of messages. 1. What would happen if the sequence is mixed in channel? (As network packages always do :) ). If the target system is idempotent, the result will not be different. If the target system depends of the right order in the sequence, we have to implement resequencer on the target site, which would restore the right order. 2. What would happen if there are the message duplicates? If the channel of target system does not acknowledge timely, the source system (or channel itself) usually sends another copy of the message. As a result we can have duplicate message on the target system side. If the target system is idempotent, it takes care of it and result will not be different. If the target system is not idempotent, we have to implement deduplicator on the target system side of the channel.
在计算中,幂等运算是指如果使用相同的输入参数多次调用它,则不会产生额外影响的运算。例如,从集合中移除一项可以被认为是集合上的幂等操作。
在数学中,幂等运算是指f(f(x)) = f(x)。例如,abs()函数是幂等的,因为对于所有x, abs(abs(x)) = abs(x)。
考虑到数学定义中的x代表一个对象的状态,而f是一个可能改变该对象的操作,可以调和这些略有不同的定义。例如,考虑Python集合及其discard方法。discard方法从集合中删除一个元素,如果该元素不存在,则不执行任何操作。所以:
my_set.discard(x)
与执行两次相同的操作具有完全相同的效果:
my_set.discard(x)
my_set.discard(x)
幂等操作经常用于网络协议的设计中,其中执行操作的请求保证至少发生一次,但也可能发生多次。如果操作是幂等的,那么执行两次或两次以上的操作是没有伤害的。
更多信息请参见维基百科关于幂等性的文章。
上面的答案之前有一些不正确和误导性的例子。下面的评论写在2014年4月之前,是指一个较旧的修订。