How to achieve distributed idempotence in golang

Idempotent

After multiple operations and one operation, the final result is consistent.

Go implementation

How to realize the idempotence of an operation in a distributed environment

func Once(conn redis.Conn, key string, seconds int) bool {
    
    
	if seconds == -2 {
    
    
		seconds = int(TomorrowZero().Sub(time.Now()).Seconds())
	}

	rs, e := redis.String(conn.Do("set", key, "done", "ex", seconds, "nx"))
	if e == redis.ErrNil {
    
    
		return false
	}
	if rs == "OK" {
    
    
		return true
	}
	return false
}

This method (within seconds) will be executed only the first time no matter how many times it is called.

Guess you like

Origin blog.csdn.net/fwhezfwhez/article/details/108842950