在 Django 里寫 REST API 是簡單的,如何讓 API 的速度更快呢?本文分享一種方法:用 Redis 作為緩存,可以讓你的 API 的速度提升 10 倍。
在 Django 里寫 REST API 是簡單的,如何讓 API 的速度更快呢?本文分享一種方法:用 Redis 作為緩存,可以讓你的 API 的速度提升 10 倍。
這里假定你已經(jīng)安裝了 Redis,并且自己可以按照官方文檔寫出一個 Django REST API,對 Django 有一定的基礎。
首先,讓我們安裝一個插件:
然后在配置文件 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 中導入 redis 并創(chuàng)建一個 redis 實例:
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)
通過在我們的 views.py 中創(chuàng)建一個列表函數(shù)來實現(xiàn) Redis。此視圖功能將檢查數(shù)據(jù)是否在 Redis 中。如果在 Redis 服務器中找到數(shù)據(jù),則從那里獲取數(shù)據(jù),如果沒有,則從數(shù)據(jù)庫中獲取數(shù)據(jù)并將其存儲在 Redis 中以備下次使用,這會導致速度增加,示例代碼如下:
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ù)據(jù)在 Redis 服務器中保留多長時間的超時,在這段代碼中,它設置為 1 小時。1 小時后,它將自動從 Redis 中刪除。
細心的你可能看到了裝飾器 log_db_queries,它來測試 API 的訪問速度,具體代碼如下:
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ù)據(jù)是否來自數(shù)據(jù)庫或 Redis。
來個使用緩存的前后對比:
使用前:1219.266 ms:

使用后:134.002 ms:

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