Simple app to add configuration options to a Django project.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: changed thread cache with django cache

+25 -10
+5
HISTORY.rst
··· 3 3 History 4 4 ------- 5 5 6 + 2.3.1 (2022-08-09) 7 + ++++++++++++++++++ 8 + 9 + * Feat: Changed thread cache with Django cache 10 + 6 11 2.3.0 (2022-08-03) 7 12 ++++++++++++++++++ 8 13
+1 -1
options/__init__.py
··· 14 14 "TYPE_CHOICES", 15 15 "CONVERTER", 16 16 ] 17 - __version__ = "2.3.0" 17 + __version__ = "2.3.1" 18 18 19 19 if django.VERSION < (3, 2): 20 20 default_app_config = "options.apps.ConfigurationsConfig"
+12 -8
options/managers.py
··· 1 - from threading import local 2 1 from typing import TYPE_CHECKING, Optional, Sequence, Union 3 2 3 + from django.core.cache import caches 4 4 from django.db import models 5 5 6 6 from options import get_option_model 7 - from options.settings import DEFAULT_EXCLUDE_USER 7 + from options.settings import ( 8 + DEFAULT_EXCLUDE_USER, 9 + DEFAULT_OPTION_CACHE_ALIAS, 10 + DEFAULT_OPTION_CACHE_TIMEOUT, 11 + ) 8 12 9 13 if TYPE_CHECKING: 10 14 from django.contrib.auth.models import User 11 - 12 - _active = local() # Active thread 13 15 14 16 15 17 class OptionQuerySet(models.QuerySet): ··· 26 28 def __get_cached_value( 27 29 self, name: str 28 30 ) -> Optional[Union[int, float, str, Sequence]]: 29 - return getattr(_active, f"{self.cache_prefix}{name}", None) 31 + return caches[DEFAULT_OPTION_CACHE_ALIAS].get(f"{self.cache_prefix}{name}") 30 32 31 33 def __set_cached_value( 32 34 self, name: str, value: Union[int, float, str, Sequence] 33 35 ) -> None: 34 - return setattr(_active, f"{self.cache_prefix}{name}", value) 36 + caches[DEFAULT_OPTION_CACHE_ALIAS].set( 37 + f"{self.cache_prefix}{name}", value, DEFAULT_OPTION_CACHE_TIMEOUT 38 + ) 35 39 36 40 def get_queryset(self) -> "OptionQuerySet": 37 41 return OptionQuerySet(self.model, using=self._db) ··· 75 79 if user is None 76 80 else f"{self.cache_prefix}{name}_{user.pk}" 77 81 ) 78 - return getattr(_active, key, None) 82 + return caches[DEFAULT_OPTION_CACHE_ALIAS].get(key) 79 83 80 84 def __set_cached_value( 81 85 self, ··· 88 92 if user is None 89 93 else f"{self.cache_prefix}{name}_{user.pk}" 90 94 ) 91 - return setattr(_active, key, value) 95 + caches[DEFAULT_OPTION_CACHE_ALIAS].set(key, value, DEFAULT_OPTION_CACHE_TIMEOUT) 92 96 93 97 def get_queryset(self) -> "UserOptionQuerySet": 94 98 return OptionQuerySet(self.model, using=self._db)
+6
options/settings.py
··· 30 30 settings, "SIMPLE_OPTIONS_OPTION_MODEL", "options.Option" 31 31 ) 32 32 33 + # Cache options 34 + DEFAULT_OPTION_CACHE_ALIAS: str = getattr( 35 + settings, "SIMPLE_OPTION_CACHE_ALIAS", "default" 36 + ) 37 + DEFAULT_OPTION_CACHE_TIMEOUT: int = getattr(settings, "SIMPLE_OPTION_CACHE_TIMEOUT", 60) 38 + 33 39 # Swappable UserOption model 34 40 DEFAULT_USER_OPTION_MODEL = getattr( 35 41 settings, "SIMPLE_OPTIONS_USER_OPTION_MODEL", "options.UserOption"
+1 -1
pyproject.toml
··· 1 1 [tool.poetry] 2 2 name = "django-simple-options" 3 - version = "2.3.0" 3 + version = "2.3.1" 4 4 description = "Simple app to add configuration options to a Django project." 5 5 readme = "README.rst" 6 6 authors = ["Marcos Gabarda <hey@marcosgabarda.com>"]