def get_profile_through_cache(cls, user_id): key = USER_PROFILE_PATTERN.format(user_id=user_id) profile = cache.get(key) if profile is not None: return profile profile, _ = UserProfile.objects.get_or_create(user_id=user_id) key = USER_PROFILE_PATTERN.format(user_id=user_id) cache.set(key, profile) return profile
def get_profile_through_cache(cls, user_id): key = USER_PROFILE_PATTERN.format(user_id=user_id) # read from cache first profile = cache.get(key) # cache hit return if profile is not None: return profile # cache miss, read from db profile, _ = UserProfile.objects.get_or_create(user_id=user_id) return profile
def get_profile_through_cache(cls, user_id): key = USER_PROFILE_PATTERN.format(user_id=user_id) # read from cache first profile = cache.get(key) # return value if cache hit if profile is not None: return profile # read from DB if cache miss profile, _ = UserProfile.objects.get_or_create(user_id=user_id) cache.set(key, profile) return profile
def get_profile_through_cache(cls, user_id): key = USER_PROFILE_PATTERN.format(user_id=user_id) # try hit cache profile = cache.get(key) # if hit, return to user if profile is not None: return profile # if miss, get from DB # since using get_or_create, not need to try-catch profile, _ = UserProfile.objects.get_or_create(user_id=user_id) cache.set(key, profile) return profile
def get_profile_through_cache(cls, user_id): # didn't use the memcachedHelper because it's using userID instead of the UserProfileId key = USER_PROFILE_PATTERN.format(user_id=user_id) # read from cache first profile = cache.get(key) # cache hit return if profile is not None: return profile # cache miss, read from db profile, _ = UserProfile.objects.get_or_create(user_id=user_id) cache.set(key, profile) return profile
def get_profile_through_cache(cls, user_id): key = USER_PROFILE_PATTERN.format(user_id=user_id) # read from cache first profile = cache.get(key) # cache hit return if profile is not None: return profile # cache miss, read from db # due to history, the user profile is added late. # Some users don't have profile, so using get_or_create method to handle no user profile cases. profile, _ = UserProfile.objects.get_or_create(user_id=user_id) cache.set(key, profile) return profile
def invalidate_profile(cls, user_id): key = USER_PROFILE_PATTERN.format(user_id=user_id) cache.delete(key)