Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

SetAsync or UpdateAsync?

Asked by
MrFlimsy 345 Moderation Voter
9 years ago

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?

1 answer

Log in to vote
5
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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.

0
This helped. Thank you :) MrFlimsy 345 — 9y
Ad

Answer this question