2016-07-01 4 views
2

Django REST 프레임 워크에서 TokenAuthentication에 memcached를 사용하는 방법이 있습니까?Django REST 프레임 워크 - TokenAuthentication - 캐시 사용

사용자 용 토큰은 오랜 기간 (예 : 몇 달) 동안 동일하게 유지되므로 내 서버로 들어오는 각 요청에 대해 데이터베이스에 도달하지 않도록하고 캐시 된 토큰을 사용하여 사용자 개체를 가져 오는 것이 좋습니다.

이것을 달성하기위한 깔끔한 방법이 있습니까?

감사

답변

2

대신 DB 당신의 memcached를 안타 custom authentication class를 만들 수 있습니다

class ExampleApiView(APIView): 
    authentication_classes = (CustomTokenAuthentication,) 

    def get(self, request, *args, **kwargs): 

     ... 
:

class ExampleAuthentication(authentication.BaseAuthentication): 
    def authenticate(self, request): 
     token = request... # get your token here 
     if not token: 
      return None 

     try: 
      # user = User.objects.get(username=username) -> This is the original code from the link above 
      user = ... # get your user based in token here 
     except User.DoesNotExist: # some possible error 
      raise exceptions.AuthenticationFailed('No such user') 

     return (user, None) 

이 그럼 당신은 즉,보기에 따라 자신의 인증 클래스를 사용할 수 있습니다

관련 문제