join-api/user_app/caching.py
2025-03-22 18:25:00 +01:00

19 lines
No EOL
458 B
Python

from django.core.cache import cache
from .models import User
from django.core.exceptions import ObjectDoesNotExist
def get_cached_user(user_id):
cache_key = f"user_{user_id}"
cached_user = cache.get(cache_key)
if cached_user is not None:
return cached_user
try:
user = User.objects.get(id=user_id)
except ObjectDoesNotExist:
return None
cache.set(cache_key, user, timeout=3600)
return user