Caching - Thundermaker300/RoSharp GitHub Wiki
RoSharp uses an internal caching system for most GET APIs, which means that the API will not update once it is initially accessed. This system has its benefits and drawbacks, as listed below.
Exceptions
The following APIs do not cache and will always make an HTTP request when called:
- Any non-GET APIs (APIs that perform an action on the Roblox site), such as modifying an asset/group/experience, sending a friend request, etc.
- Any API method that retrieves data and returns a
PageResponse<T>
class. These APIs paginate the Roblox API and thus make a new request each call. - Any API method that retrieves data and has parameters to modify the response, such as
Asset.GetThumbnailAsync(ThumbnailSize)
andExperience.GetIncomeAsync(AnalyticTimeLength)
. - Every API call through the
CustomRequest
class. This class does not store any data from its requests; this is up to the program that is using it. - Any other API that says in its documentation that it does not cache.
Benefits
The following code sample highlights the benefits of RoSharp's caching system.
using System.Diagnostics;
using RoSharp;
using RoSharp.API;
using RoSharp.API.Groups;
for (int i = 0; i < 10; i++)
{
// Start a stopwatch to keep track of time consumption.
var watch = Stopwatch.StartNew();
// Perform time-consuming tasks that get cached
Group g = await Group.FromId(123);
MemberManager manager = await g.GetMemberManagerAsync();
// Stop the watch
watch.Stop();
// Show time length
Console.WriteLine($"[{i+1}] {watch.ElapsedMilliseconds}ms");
}
And the output:
[1] 294ms
[2] 0ms
[3] 0ms
[4] 0ms
[5] 0ms
[6] 0ms
[7] 0ms
[8] 0ms
[9] 0ms
[10] 0ms
Note the first attempt took nearly 3/10ths of a second! This is very time consuming for programs. However, notice that every action after took no time at all. This is because of the caching behavior avoided making more HTTP requests. This is the case even if the class is accessed hours later.
This behavior not only speeds up your program if you have to access the same data in different locations, but this also helps to limit the amount of requests going to Roblox's site, which dramatically decreases the chance of hitting their rate-limit and getting temporarily blacklisted.
Drawbacks
What if we expect the data to change? Let's modify the example above to check group members every 5 seconds.
using System.Diagnostics;
using RoSharp;
using RoSharp.API;
using RoSharp.API.Groups;
for (int i = 0; i < 10; i++)
{
// Perform time-consuming tasks and log the amount of members in the group.
Group g = await Group.FromId(123);
MemberManager manager = await g.GetMemberManagerAsync();
Console.WriteLine(manager.Members);
await Task.Delay(5000);
}
What you will notice if you use this code sample is that the value doesn't change, which in some cases is amazing! If we're not expecting the value to change, then it won't take much time at all to access.
However, what if we expect it to change? What if a group member joins while this code is running and we don't detect it?
To counter this, the APIs that perform this caching behavior all have a RefreshAsync()
method that will force all API within the class to be refreshed. Take the below code sample, which is the same as above except one line has been added.
[!WARNING] The
RefreshAsync()
method makes at least once (sometimes multiple) requests to the Roblox API depending on the API type. These requests take time and can trigger Roblox's rate-limit ifRefreshAsync()
is called multiple times in a short time period. Keep this in mind when using it.
using System.Diagnostics;
using RoSharp;
using RoSharp.API;
using RoSharp.API.Groups;
for (int i = 0; i < 10; i++)
{
// Perform time-consuming tasks and log the amount of members in the group.
Group g = await Group.FromId(123);
+ await g.RefreshAsync();
MemberManager manager = await g.GetMemberManagerAsync();
Console.WriteLine(manager.Members);
await Task.Delay(5000);
}
This new sample calls the group's RefreshAsync()
method, which will remove the cache on all of the API members and force the data to be refreshed from the Roblox API. Now, if a member joins while this code is running, we will notice it!