fix :exam 修改缓存文件

This commit is contained in:
zty 2024-07-29 13:15:52 +08:00
parent 1d913c99f4
commit aa37c99ac4
2 changed files with 13 additions and 16 deletions

View File

@ -27,7 +27,7 @@ from apps.exam.parse_word import interpret_text
import os import os
import shutil import shutil
from django.db.models import Q from django.db.models import Q
from django_redis import get_redis_connection from django.core.cache import cache
import uuid import uuid
# Create your views here. # Create your views here.
@ -689,8 +689,7 @@ class ExamRecordViewSet(ListModelMixin, DestroyModelMixin, RetrieveModelMixin, G
current_date = now_data.strftime('%Y-%m-%d') current_date = now_data.strftime('%Y-%m-%d')
cer_number = now_data.strftime('%Y%m%d') cer_number = now_data.strftime('%Y%m%d')
# redis 连接并获取锁 # redis 连接并获取锁
redis_conn = get_redis_connection("redis") cache.set("certificate", str(uuid.uuid4()), timeout=60)
identifier = acquire_lock(redis_conn, 'certificate', 10)
# 查询证明编号创建时间为最后一个 # 查询证明编号创建时间为最后一个
cer = Certificate.objects.latest('证书编号') cer = Certificate.objects.latest('证书编号')
if cer: if cer:
@ -712,12 +711,10 @@ class ExamRecordViewSet(ListModelMixin, DestroyModelMixin, RetrieveModelMixin, G
serializer = CertificateSerializer(data=data_dict) serializer = CertificateSerializer(data=data_dict)
serializer.is_valid(raise_exception=True) serializer.is_valid(raise_exception=True)
serializer.save() serializer.save()
val = cache.get("certificate")
if val: # 如果存在,则删除
# 释放锁 # 释放锁
if identifier: cache.delete("certificate")
release_lock(redis_conn, 'certificate', identifier)
else:
print("没有找到锁")
er.took = (now - er.create_time).total_seconds() er.took = (now - er.create_time).total_seconds()
er.end_time = now er.end_time = now
er.belong_dept=request.user.dept er.belong_dept=request.user.dept

View File

@ -196,14 +196,14 @@ AUTHENTICATION_BACKENDS = (
# 缓存配置 # 缓存配置
CACHES = { CACHES = {
"redis": { # "redis": {
"BACKEND": "django_redis.cache.RedisCache", # "BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1", # "LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": { # "OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient", # "CLIENT_CLASS": "django_redis.client.DefaultClient",
# "PICKLE_VERSION": -1 # # "PICKLE_VERSION": -1
} # }
}, # },
'default': { 'default': {
'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache', 'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
'LOCATION': os.path.join(BASE_DIR, 'temp'), 'LOCATION': os.path.join(BASE_DIR, 'temp'),