我想知道在我的网络应用程序中使我的 Memcache 操作 atmoic 的最佳方法是什么。
考虑以下场景:
Client 1 connects and retrieves data from key 1
Client 2 connects a few microsecond after Client 1, requests the same data from key 1
Client 1 saves new data to key 1
Client 2 saves new (different data) to key 1, not taking into account that Client 1 modified the value already
在这种情况下,进程中没有原子性。
我的(潜在的)解决方案是在我的应用程序中设置、获取和释放键上的锁。
因此,在我实施之后,上述过程将像这样工作:
Client 1 connects, checks for an active lock on key 1, finds none, and gets the data
Client 2 connects a few microsecond after Client 1, requests the same data from key 1, but finds a lock
Client 2 enters a retry loop until Client 1 releases the lock
Client 1 saves new data to key 1, releases the lock
Client 2 gets the fresh data, sets a lock on key 1, and continues
想法?这种方法会奏效吗?我应该提防任何性能问题吗?