Search in sources :

Example 1 with CachedLeaderboardMember

use of net.kodehawa.mantarobot.commands.utils.leaderboards.CachedLeaderboardMember in project MantaroBot by Mantaro.

the class LeaderboardCmd method getMember.

/**
 * Caches an user in redis if they're in the leaderboard. This speeds up User lookup times tenfold.
 * The key will expire after 48 hours in the set, then we will just re-cache it as needed.
 * This should also take care of username changes.
 *
 * This value is saved in Redis so it can be used cross-node.
 * This also fixes leaderboards being incomplete in some nodes.
 *
 * This method is necessary to avoid calling Discord every single time we call a leaderboard,
 * since this might create hundreds of API requests in a few seconds, causing some nice 429s.
 *
 * @param id The id of the user.
 * @return A instance of CachedLeaderboardMember.
 * This can either be retrieved from Redis or cached on the spot if the cache didn't exist for it.
 */
private CachedLeaderboardMember getMember(Context ctx, String id) {
    try (Jedis jedis = MantaroData.getDefaultJedisPool().getResource()) {
        var savedTo = "cachedlbuser:" + id;
        var missed = "lbmiss:" + id;
        var json = jedis.get(savedTo);
        if (json == null) {
            // No need to keep trying missed entries for a while. Entry should have a TTL of 12 hours.
            if (jedis.get(missed) != null) {
                return null;
            }
            // Sadly a .complete() call for an User won't fill the internal cache, as JDA has no way to TTL it, instead, we will add it
            // to our own cache in Redis, and expire it in 48 hours to avoid it filling up endlessly.
            // This is to avoid having to do calls to discord all the time a leaderboard is retrieved, and only do the calls whenever
            // it's absolutely needed, or when we need to re-populate the cache.
            var user = ctx.retrieveUserById(id);
            // If no user was found, we need to return null. This is later handled on generateLeaderboardEmbed.
            if (user == null) {
                jedis.set(missed, "1");
                jedis.expire(missed, TimeUnit.HOURS.toSeconds(12));
                return null;
            }
            CachedLeaderboardMember cached = new CachedLeaderboardMember(user.getIdLong(), user.getName(), user.getDiscriminator(), System.currentTimeMillis());
            jedis.set(savedTo, JsonDataManager.toJson(cached));
            // Set the value to expire in 48 hours.
            jedis.expire(savedTo, TimeUnit.HOURS.toSeconds(48));
            return cached;
        } else {
            return JsonDataManager.fromJson(json, CachedLeaderboardMember.class);
        }
    } catch (JsonProcessingException e) {
        // This would be odd, really.
        e.printStackTrace();
        return null;
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) CachedLeaderboardMember(net.kodehawa.mantarobot.commands.utils.leaderboards.CachedLeaderboardMember) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 CachedLeaderboardMember (net.kodehawa.mantarobot.commands.utils.leaderboards.CachedLeaderboardMember)1 Jedis (redis.clients.jedis.Jedis)1