如何让你的 Django API 快十倍

开发 前端
在 Django 里写 REST API 是简单的,如何让 API 的速度更快呢?本文分享一种方法:用 Redis 作为缓存,可以让你的 API 的速度提升 10 倍。

在 Django 里写 REST API 是简单的,如何让 API 的速度更快呢?本文分享一种方法:用 Redis 作为缓存,可以让你的 API 的速度提升 10 倍。

这里假定你已经安装了 Redis,并且自己可以按照官方文档写出一个 Django REST API,对 Django 有一定的基础。

首先,让我们安装一个插件:

pip install django-redis

然后在配置文件 settings.py 中添加一下内容:

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 并创建一个 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 中创建一个列表函数来实现 Redis。此视图功能将检查数据是否在 Redis 中。如果在 Redis 服务器中找到数据,则从那里获取数据,如果没有,则从数据库中获取数据并将其存储在 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 设置数据在 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

这为我们提供了获取数据所需时间的详细视图,以及数据是否来自数据库或 Redis。

来个使用缓存的前后对比:

使用前:1219.266 ms:

图片

使用后:134.002 ms:

图片

最后

缓存确实有助于提高 Django REST API 的速度,而 Redis 又是最佳的缓存工具,可以从这里获取Django-Redis[1] 的源代码。

责任编辑:武晓燕 来源: Python七号
相关推荐

2024-03-08 07:58:13

QPShttpsync

2023-06-13 13:52:00

Java 7线程池

2023-09-07 11:29:36

API开发

2023-03-07 08:34:01

2017-09-26 14:56:57

MongoDBLBS服务性能

2021-08-17 14:30:09

Windows电脑微软

2021-06-02 22:54:34

技巧 Git Clone项目

2018-01-29 05:38:20

5G4G运营商

2020-09-16 16:07:34

Chrome插件浏览器

2018-09-27 15:42:15

Python编程语言技术

2016-07-07 15:38:07

京东

2020-07-21 15:40:55

NginxJava服务器

2019-03-27 13:45:44

MySQL优化技巧数据库

2009-12-15 21:49:05

2011-02-28 10:01:00

芯片有机塑料

2016-08-09 21:18:31

5G4G5G网络

2021-06-07 11:40:26

Python命令代码

2021-09-08 08:00:00

PyPolars数据开源

2022-04-13 09:44:16

5G万兆网络数字化

2023-02-23 10:21:17

ChatGPT人工智能
点赞
收藏

51CTO技术栈公众号