• Welcome to the world's largest Chinese hacker forum

    Welcome to the world's largest Chinese hacker forum, our forum registration is open! You can now register for technical communication with us, this is a free and open to the world of the BBS, we founded the purpose for the study of network security, please don't release business of black/grey, or on the BBS posts, to seek help hacker if violations, we will permanently frozen your IP and account, thank you for your cooperation. Hacker attack and defense cracking or network Security

    business please click here: Creation Security  From CNHACKTEAM

Recommended Posts

Luffy /6/ 接口缓存

这里将banner图片作为缓存放入redis

视图

BannerView类(GenericViewSet,ListModelMixin):

# qs对象可以像列表一样,切片

查询集=横幅。对象。filter(is _ delete=False,is_show=True).order _ by(' orders ')[:设置. BANNER_COUNT]

serializer _ class=横幅序列化程序

定义列表(self,request,*args,**kwargs): #重写目录设置缓存

banner _ list=cache。get('横幅缓存列表')

如果不是banner_list: #如果缓存中没有缓存,那么第一次走数据库,并放入缓存

响应=超级()。列表(请求,*参数,*夸脱)

print(response.data,'走了数据库)

cache.set('横幅_缓存_列表response.data)

else: #缓存中有横幅,走缓存

打印('走了缓存)

响应=响应(数据=横幅_列表)

返回响应

路由

从django.urls导入路径,包括

从rest_framework .路由器导入简单路由器

从。视图导入BannerView

路由器=简单路由器()

router.register('banner ',BannerView,' banner ')

urlpatterns=[

路径('',包含(router.urls)),

]

image-20220502180921612

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now