EBOOK – REDIS IN ACTION

This book covers the use of Redis, an in-memory database/data structure server.

open all | close all

7.4.1 Approaching the problem one job at a time

At first glance, we might consider a straightforward solution to this problem. Start
with every job having its own SET, with members being the skills that the job requires.
To check whether a candidate has all of the requirements for a given job, we’d add the
candidate’s skills to a SET and then perform the SDIFF of the job and the candidate’s
skills. If there are no skills in the resulting SDIFF, then the user has all of the qualifications
necessary to complete the job. The code for adding a job and checking whether
a given set of skills is sufficient for that job looks like this next listing.

Listing 7.17A potential solution for finding jobs when a candidate meets all requirements
def add_job(conn, job_id, required_skills):
    conn.sadd('job:' + job_id, *required_skills)

Add all required job skills to the job’s SET.

def is_qualified(conn, job_id, candidate_skills):
    temp = str(uuid.uuid4())
    pipeline = conn.pipeline(True)
    pipeline.sadd(temp, *candidate_skills)
    pipeline.expire(temp, 5)

Add the candidate’s skills to a temporary SET with an expiration time.

    pipeline.sdiff('job:' + job_id, temp)

Calculate the SET of skills that the job requires that the candidate doesn’t have.

    return not pipeline.execute()[-1]

Return True if there are no skills that the candidate doesn’t have.

Explaining that again, we’re checking whether a job requires any skills that the candidate
doesn’t have. This solution is okay, but it suffers from the fact that to find all of
the jobs for a given candidate, we must check each job individually. This won’t scale,
but there are solutions that will.