What's the difference between the two? Why is UpdateAsync()
safer than SetAsync()
? The explanation on the wiki page is too vague.
I understand how DataStore works, I just fail to see the difference between the two methods. What does UpdateAsync do that SetAsync doesn't?
UpdateAsync
looks at the current value in the data store - as opposed to what the particular server / script may believe it to be, which may be wrong due to caching or your own logic errors in designing the scripts interacting with DataStore.
Ideally, UpdateAsync(key,fun)
could be defined like this:
function UpdateAsync(key,fun) SetAsync(key,fun(GetAsync(key))); end
However, as stated in the Wiki article, GetAsync
caches-- which means its value may be out of date. The value that UpdateAsync
gives to the function, however, is always up to date, meaning it will not suffer the 10 second cache lag possible. If you have multiple open servers interacting with the data, this is even more significant.
The bottom line is that if you are updating a key in the data store using the value currently stored in it, you should only use UpdateAsync
.
You would be encouraged to only use SetAsync
if the new value is completely unrelated to the old one.