Skip to content

Commit

Permalink
Merge pull request #913 from The-Commit-Company/785-cache-user-list
Browse files Browse the repository at this point in the history
perf: cache user list
  • Loading branch information
nikkothari22 authored May 10, 2024
2 parents 334159f + d6c8521 commit 38f2f23
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
3 changes: 3 additions & 0 deletions raven-app/src/utils/users/UserListProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export const UserListProvider = ({ children }: PropsWithChildren) => {
revalidateOnReconnect: false,
})

/** TODO: If a bulk import happens, this gets called multiple times potentially causing the server to go down.
* Instead, throttle this - wait for all events to subside
*/
useFrappeDocTypeEventListener('Raven User', () => {
mutate()

Expand Down
20 changes: 10 additions & 10 deletions raven/api/raven_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import frappe
from frappe import _
from frappe.utils.caching import redis_cache


@frappe.whitelist(methods=["GET"])
Expand All @@ -12,7 +13,7 @@ def get_current_raven_user():

# Check if the user is a Raven User and has he "Raven User" role
# If not, then throw an error
if "Raven User" not in frappe.get_roles():
if not frappe.has_permission("Raven User"):
frappe.throw(
_(
"You do not have a <b>Raven User</b> role. Please contact your administrator to add your user profile as a <b>Raven User</b>."
Expand All @@ -23,30 +24,29 @@ def get_current_raven_user():
return frappe.get_cached_doc("Raven User", {"user": frappe.session.user})


@frappe.whitelist()
@frappe.whitelist(methods=["GET"])
@frappe.read_only()
def get_list():
"""
Fetches list of all users who have the role: Raven User
"""

# Check if the user is a Raven User and has he "Raven User" role
# If not, then throw an error
if "Raven User" not in frappe.get_roles():
if not frappe.has_permission("Raven User"):
frappe.throw(
_(
"You do not have a <b>Raven User</b> role. Please contact your administrator to add your user profile as a <b>Raven User</b>."
),
title=_("Insufficient permissions. Please contact your administrator."),
)

if not frappe.db.exists("Raven User", {"user": frappe.session.user}):
frappe.throw(
_(
"You do not have a <b>Raven User</b> profile. Please contact your administrator to add your user profile as a <b>Raven User</b>."
),
title=_("Insufficient permissions. Please contact your administrator."),
)
# Get users is cached since this won't change frequently
return get_users()


@redis_cache()
def get_users():
users = frappe.db.get_all(
"Raven User",
fields=["full_name", "user_image", "name", "first_name", "enabled", "type"],
Expand Down
14 changes: 14 additions & 0 deletions raven/raven/doctype/raven_user/raven_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ def before_save(self):
if self.type != "Bot":
self.update_photo_from_user()

def after_insert(self):
self.invalidate_user_list_cache()

def on_update(self):
self.invalidate_user_list_cache()

def on_trash(self):
"""
Remove the Raven User from all channels
Expand All @@ -66,6 +72,14 @@ def after_delete(self):
user.remove_roles("Raven User")
user.save()

self.invalidate_user_list_cache()

def invalidate_user_list_cache(self):

from raven.api.raven_users import get_users

get_users.clear_cache()

def update_photo_from_user(self):
"""
We need to create a new File record for the user image and attach it to the Raven User record.
Expand Down

0 comments on commit 38f2f23

Please sign in to comment.