mentorenwahl/backend/src/backend/worker/jobs/cache_ldap_user_job.cr
Dominic Grimm 50379148bc
Some checks failed
continuous-integration/drone/push Build is failing
Update codebase
2022-10-31 09:47:26 +01:00

41 lines
1.4 KiB
Crystal

# Mentorenwahl: A fullstack application for assigning mentors to students based on their whishes.
# Copyright (C) 2022 Dominic Grimm
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
module Backend
module Worker
module Jobs
# Caches user data in redis cache
class CacheLdapUserJob < Mosquito::QueuedJob
params id : Int32
# :ditto:
def perform : Nil
key = "ldap:user:#{id}"
user = Db::User.find!(id)
if user
log "Caching user ##{id}..."
ldap_user = Ldap::User.from_username(user.username)
Redis::CLIENT.set(key, ldap_user.to_json, ex: (Time.utc + Backend.config.ldap.cache_refresh_interval.minutes).to_unix)
else
log "User ##{id} not found. Deleting cache..."
Redis::CLIENT.del(key)
end
end
end
end
end
end