Creates custom get_or_set function
This commit is contained in:
		| @@ -1,8 +1,8 @@ | |||||||
| """ template filters for status interaction buttons """ | """ template filters for status interaction buttons """ | ||||||
| from django import template | from django import template | ||||||
| from django.core.cache import cache |  | ||||||
|  |  | ||||||
| from bookwyrm import models | from bookwyrm import models | ||||||
|  | from bookwyrm.utils.cache import get_or_set | ||||||
|  |  | ||||||
|  |  | ||||||
| register = template.Library() | register = template.Library() | ||||||
| @@ -11,20 +11,23 @@ register = template.Library() | |||||||
| @register.filter(name="liked") | @register.filter(name="liked") | ||||||
| def get_user_liked(user, status): | def get_user_liked(user, status): | ||||||
|     """did the given user fav a status?""" |     """did the given user fav a status?""" | ||||||
|     return cache.get_or_set( |     return get_or_set( | ||||||
|         f"fav-{user.id}-{status.id}", |         f"fav-{user.id}-{status.id}", | ||||||
|         models.Favorite.objects.filter(user=user, status=status).exists(), |         lambda u, s: models.Favorite.objects.filter(user=u, status=s).exists(), | ||||||
|         259200, |         user, | ||||||
|  |         status, | ||||||
|  |         timeout=259200 | ||||||
|     ) |     ) | ||||||
|  |  | ||||||
|  |  | ||||||
| @register.filter(name="boosted") | @register.filter(name="boosted") | ||||||
| def get_user_boosted(user, status): | def get_user_boosted(user, status): | ||||||
|     """did the given user fav a status?""" |     """did the given user fav a status?""" | ||||||
|     return cache.get_or_set( |     return get_or_set( | ||||||
|         f"boost-{user.id}-{status.id}", |         f"boost-{user.id}-{status.id}", | ||||||
|         status.boosters.filter(user=user).exists(), |         lambda u: status.boosters.filter(user=u).exists(), | ||||||
|         259200, |         user, | ||||||
|  |         timeout=259200, | ||||||
|     ) |     ) | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -33,15 +36,20 @@ def get_user_saved_lists(user, book_list): | |||||||
|     """did the user save a list""" |     """did the user save a list""" | ||||||
|     return user.saved_lists.filter(id=book_list.id).exists() |     return user.saved_lists.filter(id=book_list.id).exists() | ||||||
|  |  | ||||||
|  |  | ||||||
| @register.simple_tag(takes_context=True) | @register.simple_tag(takes_context=True) | ||||||
| def get_relationship(context, user_object): | def get_relationship(context, user_object): | ||||||
|     """caches the relationship between the logged in user and another user""" |     """caches the relationship between the logged in user and another user""" | ||||||
|     user = context["request"].user |     user = context["request"].user | ||||||
|     return cache.get(f"relationship-{user.id}-{user_object.id}") or cache.set( |     return get_or_set( | ||||||
|         get_relationship_name(user, user_object), |         f"relationship-{user.id}-{user_object.id}", | ||||||
|  |         get_relationship_name, | ||||||
|  |         user, | ||||||
|  |         user_object, | ||||||
|         timeout=259200, |         timeout=259200, | ||||||
|     ) |     ) | ||||||
|  |  | ||||||
|  |  | ||||||
| def get_relationship_name(user, user_object): | def get_relationship_name(user, user_object): | ||||||
|     """returns the relationship type""" |     """returns the relationship type""" | ||||||
|     types = { |     types = { | ||||||
|   | |||||||
							
								
								
									
										10
									
								
								bookwyrm/utils/cache.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								bookwyrm/utils/cache.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | |||||||
|  | """ Custom handler for caching """ | ||||||
|  | from django.core.cache import cache | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def get_or_set(cache_key, function, *args, timeout=None): | ||||||
|  |     """Django's built-in get_or_set isn't cutting it""" | ||||||
|  |     value = cache.get(cache_key) | ||||||
|  |     if value is None: | ||||||
|  |         cache.set(cache_key, function(*args), timeout=timeout) | ||||||
|  |     return cache.get(cache_key) | ||||||
		Reference in New Issue
	
	Block a user