Skip to content
This repository has been archived by the owner on Sep 27, 2021. It is now read-only.

Commit

Permalink
deeper search
Browse files Browse the repository at this point in the history
  • Loading branch information
montyanderson committed Oct 14, 2019
1 parent be52a33 commit fe54258
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,28 @@ router.get('/userlist/:page', async ctx => {

const {filter} = ctx.query;

const filteredUsers = typeof filter === 'string' && filter.length > 0
? (await redis.zscan('tracked', 0, 'MATCH', `*${filter}*`, 'COUNT', 10))[1].filter((element, index) => index % 2 === 0)
: await redis.zrevrangebyscore('tracked', '+inf', 0, 'LIMIT', page * perPage, perPage);
async function search() {
const iterations = 100;

const results = new Set();
let cursor = 0;

for(let i = 0; i < iterations; i++) {
const [newCursor, users] = await redis.zscan('tracked', cursor, 'MATCH', `*${filter}*`)

console.log(filteredUsers);
users
.filter((element, index) => index % 2 === 0)
.forEach(user => results.add(user))

cursor = newCursor;
}

return [...results];
}

const filteredUsers = typeof filter === 'string' && filter.length > 0
? await search()
: await redis.zrevrangebyscore('tracked', '+inf', '-inf', 'LIMIT', page * perPage, perPage);

const users = await Promise.all(filteredUsers.map(async username => ({
username,
Expand Down

0 comments on commit fe54258

Please sign in to comment.