+ thread-safe cache for GlobalSettings
This commit is contained in:
parent
d7ddd9ed4b
commit
5bfae66ad5
|
|
@ -1,10 +1,8 @@
|
|||
import time
|
||||
from decimal import Decimal
|
||||
from datetime import datetime
|
||||
import random
|
||||
import string
|
||||
import uuid
|
||||
from io import BytesIO
|
||||
from threading import local as LocalContext
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.admin import display
|
||||
|
|
@ -23,7 +21,7 @@ from store.utils import create_preview, concat_not_null_values
|
|||
|
||||
|
||||
class GlobalSettings(models.Model):
|
||||
cached = None
|
||||
context = LocalContext()
|
||||
|
||||
# currency
|
||||
yuan_rate = models.DecimalField('Курс CNY/RUB', max_digits=10, decimal_places=2, default=0)
|
||||
|
|
@ -40,18 +38,18 @@ class GlobalSettings(models.Model):
|
|||
# Store only one instance of GlobalSettings
|
||||
self.__class__.objects.exclude(id=self.id).delete()
|
||||
super().save(*args, **kwargs)
|
||||
GlobalSettings.cached = self
|
||||
GlobalSettings.context.cached = self
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f'GlobalSettings for {self.id}'
|
||||
|
||||
@classmethod
|
||||
def load(cls) -> 'GlobalSettings':
|
||||
if cls.cached is not None:
|
||||
return cls.cached
|
||||
if hasattr(cls.context, 'cached'):
|
||||
return cls.context.cached
|
||||
|
||||
obj, _ = cls.objects.get_or_create(id=1)
|
||||
cls.cached = obj
|
||||
cls.context.cached = obj
|
||||
return obj
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user