Skip to content

Commit

Permalink
Cache results from getUsernameHistory (#852)
Browse files Browse the repository at this point in the history
Cache results from getUsernameHistory
  • Loading branch information
Regalijan authored Feb 13, 2025
1 parent 6df57d6 commit 482dda1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/users/getUsernameHistory.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Includes
const getPageResults = require('../util/getPageResults.js').func
const cache = require('../cache')

// Args
exports.required = ['userId']
Expand All @@ -11,7 +12,7 @@ exports.optional = ['limit', 'sortOrder', 'pageCursor']
* @category User
* @alias getUsernameHistory
* @param {number} userId
* @param {Limit=} [limit=10]
* @param {Limit=} [limit=100]
* @param {SortOrder=} [sortOrder=Asc]
* @param {string} cursor
* @returns {Promise<UsernameHistoryEntry[]>}
Expand All @@ -31,5 +32,11 @@ function getUsernameHistory (userId, limit, sortOrder, cursor) {
}

exports.func = function (args) {
return getUsernameHistory(args.userId, args.limit, args.sortOrder, args.cursor)
let { userId, limit, sortOrder, cursor } = args
limit ||= 100
sortOrder ||= 'Asc'

return cache.wrap('UsernameHistory', `${userId}-${limit}-${sortOrder}-${cursor}`, function () {
return getUsernameHistory(userId, limit, sortOrder, cursor)
})
}
6 changes: 6 additions & 0 deletions settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@
"expire": false,
"refresh": false,
"desc": "Caches rank by user ID. Changes cannot be anticipated so this is not enabled by default."
},

"UsernameHistory": {
"expire": 86400,
"refresh": false,
"desc": "Caches a user's username history based on user id, limit, sort order, and cursor. The rate limit for the associated endpoint is very low, and this setting should be left alone as a result."
}
}
}

0 comments on commit 482dda1

Please sign in to comment.