Im 내 웹 앱에서 Memcache 작업을 기동하지 못하게하는 가장 좋은 방법은 무엇인지 궁금합니다.PHP Atomic Memcache
은 고려 사항으로 다음과 같은 시나리오를 보자
이 경우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
생각을? 이 방법이 효과가 있으며 조심해야 할 성능 관련 히트가 있습니까?
불행히도 수정은 1 씩 증가하는 것처럼 간단하지 않습니다. 크레딧이 가변 양에 의해 더 해지거나 뺄 때, 나는 프로세스에서 절대적인 원 자성을 필요로합니다. 두 번째 옵션은 내 (잠재적 인) 솔루션과 비슷합니다. – Kovo
가장 좋은 해결책은'cas()'입니다. 두 번째로 가장 좋은 해결책은'$ keyname'과'$ keyname .'_ lock '두 개의 키를 사용하는 것입니다. 잠금을 획득/해제하려면'add()/delete()'를 사용하십시오. –