Skip to content

Commit

Permalink
models: Add get_user function.
Browse files Browse the repository at this point in the history
This is intended to replace get_user_profile_by_email.
  • Loading branch information
hackerkid authored and timabbott committed May 22, 2017
1 parent a9f4d56 commit bdf7c6c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
4 changes: 2 additions & 2 deletions zerver/lib/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
from zerver.lib.create_user import create_user
from zerver.lib import bugdown
from zerver.lib.cache import cache_with_key, cache_set, \
user_profile_by_email_cache_key, cache_set_many, \
cache_delete, cache_delete_many
user_profile_by_email_cache_key, user_profile_cache_key, \
cache_set_many, cache_delete, cache_delete_many
from zerver.decorator import statsd_increment
from zerver.lib.utils import log_statsd_event, statsd
from zerver.lib.html_diff import highlight_html_differences
Expand Down
5 changes: 5 additions & 0 deletions zerver/lib/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ def user_profile_by_email_cache_key(email):
# with high likelihood be ASCII-only for the foreseeable future.
return u'user_profile_by_email:%s' % (make_safe_digest(email.strip()),)

def user_profile_cache_key(email, realm):
# type: (Text, Realm) -> Text
return u"user_profile:%s:%s" % (make_safe_digest(email.strip()), realm.id,)

def user_profile_by_id_cache_key(user_profile_id):
# type: (int) -> Text
return u"user_profile_by_id:%s" % (user_profile_id,)
Expand Down Expand Up @@ -353,6 +357,7 @@ def delete_user_profile_caches(user_profiles):
for user_profile in user_profiles:
keys.append(user_profile_by_email_cache_key(user_profile.email))
keys.append(user_profile_by_id_cache_key(user_profile.id))
keys.append(user_profile_cache_key(user_profile.email, user_profile.realm))

cache_delete_many(keys)

Expand Down
5 changes: 3 additions & 2 deletions zerver/lib/cache_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
Huddle, huddle_hash_cache_key
from zerver.lib.cache import cache_with_key, cache_set, \
user_profile_by_email_cache_key, user_profile_by_id_cache_key, \
get_remote_cache_time, get_remote_cache_requests, cache_set_many, \
to_dict_cache_key_id
user_profile_cache_key, get_remote_cache_time, get_remote_cache_requests, \
cache_set_many, to_dict_cache_key_id
from importlib import import_module
from django.contrib.sessions.models import Session
import logging
Expand All @@ -38,6 +38,7 @@ def user_cache_items(items_for_remote_cache, user_profile):
# type: (Dict[Text, Tuple[UserProfile]], UserProfile) -> None
items_for_remote_cache[user_profile_by_email_cache_key(user_profile.email)] = (user_profile,)
items_for_remote_cache[user_profile_by_id_cache_key(user_profile.id)] = (user_profile,)
items_for_remote_cache[user_profile_cache_key(user_profile.email, user_profile.realm)] = (user_profile,)

def stream_cache_items(items_for_remote_cache, stream):
# type: (Dict[Text, Tuple[Stream]], Stream) -> None
Expand Down
7 changes: 6 additions & 1 deletion zerver/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from django.dispatch import receiver
from zerver.lib.cache import cache_with_key, flush_user_profile, flush_realm, \
user_profile_by_id_cache_key, user_profile_by_email_cache_key, \
generic_bulk_cached_fetch, cache_set, flush_stream, \
user_profile_cache_key, generic_bulk_cached_fetch, cache_set, flush_stream, \
display_recipient_cache_key, cache_delete, \
get_stream_cache_key, active_user_dicts_in_realm_cache_key, \
bot_dicts_in_realm_cache_key, active_user_dict_fields, \
Expand Down Expand Up @@ -1363,6 +1363,11 @@ def get_user_profile_by_email(email):
# type: (Text) -> UserProfile
return UserProfile.objects.select_related().get(email__iexact=email.strip())

@cache_with_key(user_profile_cache_key, timeout=3600*24*7)
def get_user(email, realm):
# type: (Text, Realm) -> UserProfile
return UserProfile.objects.select_related().get(email__iexact=email.strip(), realm=realm)

@cache_with_key(active_user_dicts_in_realm_cache_key, timeout=3600*24*7)
def get_active_user_dicts_in_realm(realm):
# type: (Realm) -> List[Dict[str, Any]]
Expand Down

0 comments on commit bdf7c6c

Please sign in to comment.