Webアプリで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
考え?この方法は機能しますか?また、注意が必要なパフォーマンスへの影響はありますか?