日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
如何讓你的DjangoAPI快十倍

在 Django 里寫(xiě) REST API 是簡(jiǎn)單的,如何讓 API 的速度更快呢?本文分享一種方法:用 Redis 作為緩存,可以讓你的 API 的速度提升 10 倍。

這里假定你已經(jīng)安裝了 Redis,并且自己可以按照官方文檔寫(xiě)出一個(gè) Django REST API,對(duì) Django 有一定的基礎(chǔ)。

首先,讓我們安裝一個(gè)插件:

pip install django-redis

然后在配置文件 settings.py 中添加一下內(nèi)容:

CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1", # Local Link provided by the redis-server command
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
}
}

然后在 views.py 中導(dǎo)入 redis 并創(chuàng)建一個(gè) redis 實(shí)例:

from django.core.cache import cache
import time
import redis
from rest_framework.response import Response
redis_instance = redis.StrictRedis(host='127.0.0.1', port=6379, db=1)

通過(guò)在我們的 views.py 中創(chuàng)建一個(gè)列表函數(shù)來(lái)實(shí)現(xiàn) Redis。此視圖功能將檢查數(shù)據(jù)是否在 Redis 中。如果在 Redis 服務(wù)器中找到數(shù)據(jù),則從那里獲取數(shù)據(jù),如果沒(méi)有,則從數(shù)據(jù)庫(kù)中獲取數(shù)據(jù)并將其存儲(chǔ)在 Redis 中以備下次使用,這會(huì)導(dǎo)致速度增加,示例代碼如下:

class MusicianViewSet(viewsets.ModelViewSet):
serializer_class = MusicianSerializer
queryset = Musician.objects.all()
@log_db_queries
def list(self, request):
first_name = self.request.query_params.get('first_name')

if first_name is not None:
cache_key = 'name' + first_name
else:
cache_key = 'name'

if cache_key in cache:
print("redis")
queryset = cache.get(cache_key)
return Response(queryset)
else:
print('db')
queryset = Musician.objects.all()
if first_name is not None:
queryset = queryset.filter(first_name__contains=first_name)

serializer_class = MusicianSerializer(queryset, many=True)
cache.set(cache_key , serializer_class.data, timeout=60*60)
return Response(serializer_class.data)

在這里 timeout 設(shè)置數(shù)據(jù)在 Redis 服務(wù)器中保留多長(zhǎng)時(shí)間的超時(shí),在這段代碼中,它設(shè)置為 1 小時(shí)。1 小時(shí)后,它將自動(dòng)從 Redis 中刪除。

細(xì)心的你可能看到了裝飾器 log_db_queries,它來(lái)測(cè)試 API 的訪問(wèn)速度,具體代碼如下:

def log_db_queries ( f )
from django.db import connection
def new_f ( * args , ** kwargs )
start_time = time.time()
res = f ( * args , ** kwargs )
print ( "\n\n" )
print ( "-"*80 )
print ("db queries log for %s:\n" % (f.__name__))
print ( " TOTAL COUNT : % s " % len ( connection.queries ) )
for q in connection.queries :
print ("%s: %s\n" % (q["time"] , q["sql"]))
end_time = time.time ()
duration = end_time - start_time
print ('\n Total time: {:.3f} ms'.format(duration * 1000.0))
print ("-"*80)
return res
return new_f

這為我們提供了獲取數(shù)據(jù)所需時(shí)間的詳細(xì)視圖,以及數(shù)據(jù)是否來(lái)自數(shù)據(jù)庫(kù)或 Redis。

來(lái)個(gè)使用緩存的前后對(duì)比:

使用前:1219.266 ms:

使用后:134.002 ms:

最后

緩存確實(shí)有助于提高 Django REST API 的速度,而 Redis 又是最佳的緩存工具,可以從這里獲取Django-Redis[1] 的源代碼。


網(wǎng)頁(yè)標(biāo)題:如何讓你的DjangoAPI快十倍
文章源于:http://www.5511xx.com/article/dhcpogc.html