自定义频率类,频率功能源码剖析,分页功能,排序功能,过滤功能

1.自定义频率类

继承BaseThrottle必须要重写allow_request方法,不然会报错

复制代码
class OurThrottle(SimpleRateThrottle):
    scope = 'ss'

    def get_cache_key(self, request, view):
        return request.META.get('REMOTE_ADDR')


class UserThrottling(BaseThrottle):
    dic = {}

    def __init__(self):
        self.history = None

    def allow_request(self, request, view):
        ip = request.META.get('REMOTE_ADDR')
        import time
        timer = time.time()
        print(timer)
        if ip not in self.dic:
            self.dic[ip] = [timer, ]
            return True
        self.history = self.dic.get(ip)
        print(self.history)
        while self.history and timer - self.history[-1] > 60:
            # while self.history and -timer + self.history[-1] < 60:

            self.history.pop()
        if len(self.history) < 3:
            self.history.insert(0, timer)
            # print(self.history)
            return True
        else:
            return False
复制代码

2.频率功能源码剖析

复制代码
# SimpleRateThrottle
    -源码里执行的频率类的allow_request,读SimpleRateThrottle的allow_request
    
class SimpleRateThrottle(BaseThrottle):
    cache = default_cache
    timer = time.time
    cache_format = 'throttle_%(scope)s_%(ident)s'
    scope = None
    THROTTLE_RATES = api_settings.DEFAULT_THROTTLE_RATES
    def __init__(self):  # 只要类实例化得到对象就会执行,一执行,self.rate就有值了,而且self.num_requests和self.duration
        if not getattr(self, 'rate', None): # 去频率类中反射rate属性或方法,发现没有,返回了None,这个if判断就符合,执行下面的代码
            self.rate = self.get_rate()  #返回了  '3/m'
        #  self.num_requests=3
        #  self.duration=60
        self.num_requests, self.duration = self.parse_rate(self.rate)

    def get_rate(self):
         return self.THROTTLE_RATES[self.scope] # 字典取值,配置文件中咱们配置的字典{'ss': '3/m',},根据ss取到了 '3/m'

    def parse_rate(self, rate):
        if rate is None:
            return (None, None)
        # rate:字符串'3/m'  根据 / 切分,切成了 ['3','m']
        # num=3,period=m
        num, period = rate.split('/')
        # num_requests=3  数字3
        num_requests = int(num)
        # period='m'  ---->period[0]--->'m'
        # {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}[period[0]]
        # duration=60
        duration = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}[period[0]]
        # 3     60
        return (num_requests, duration)

    def allow_request(self, request, view):
        if self.rate is None:
            return True
        # 咱们自己写的,返回什么就以什么做限制  咱们返回的是ip地址
        # self.key=当前访问者的ip地址
        self.key = self.get_cache_key(request, view)
        if self.key is None:
            return True
        # self.history 访问者的时间列表,从缓存中拿到,如果拿不到就是空列表,如果之前有 [时间2,时间1]
        self.history = self.cache.get(self.key, [])
        # 当前时间
        self.now = self.timer()
        while self.history and self.history[-1] <= self.now - self.duration:
            self.history.pop()
        if len(self.history) >= self.num_requests:
            return self.throttle_failure()
        return self.throttle_success()
    
    
  # 总结:以后要再写频率类,只需要继承SimpleRateThrottle,重写get_cache_key,配置类属性scope,配置文件中配置一下就可以了
复制代码

3.分页功能

一共有三种分页功能

复制代码
from rest_framework.pagination import PageNumberPagination,LimitOffsetPagination,CursorPagination

class UserPageNumberPagination(PageNumberPagination):
    page_size = 2  #每页条数
    page_query_param = 'page' #查询第几页参数
    page_size_query_param = 'size'  #每页有几条数据
    max_page_size = 5  #最大不超过五条

class UserLimitOffsetPagination(LimitOffsetPagination):
    default_limit = 3   
    limit_query_param = 'limit'  #这一页显示4条数据
    offset_query_param = 'offset'  #从第几条数据往后开始显示
    max_limit = 5


class UserCursorPagination(CursorPagination):
    cursor_query_param = 'cursor'  
    page_size = 2
    ordering = '-id'  #根据什么进行排序
复制代码

4 排序功能和过滤功能

复制代码
#OrderingFilter是排序功能,然后在ordering_fields中写要排序的字段,最后在路由中加入就行
#SearchFilter是过滤功能,同上

from
rest_framework.filters import OrderingFilter,SearchFilter
filter_backends=[SearchFilter,OrderingFilter]
search_fields=['name']
ordering_fields=['id']
复制代码

 4.1 django-filters过滤

from django_filters.rest_framework.backends import DjangoFilterBackend
    filter_backends = [DjangoFilterBackend]
    filterset_fields = ['id']

 

posted @   shangxin_bai  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示