add redis cache, Acquisition & django-esi

This commit is contained in:
2023-10-29 12:12:25 +01:00
parent f92d518468
commit f860a5bf1d
10 changed files with 95 additions and 54 deletions

View File

@@ -1,33 +0,0 @@
import datetime
import os
import redis
import pickle
from esy.client import ESIClient
from esy.auth import ESIAuthenticator
class ESICache(object):
def __init__(self, **kwargs):
self._r = redis.Redis(**kwargs)
# self._r = redis.StrictRedis(host=redis_url, port=redis_port, db=db)
def get(self, key):
# return pickle.loads(self._r[key])
return pickle.loads(self._r.get(key))
def set(self, key, data, cached_until: datetime.datetime):
self._r.set(key, pickle.dumps(data), ex=cached_until - datetime.datetime.now(datetime.timezone.utc))
def __contains__(self, item):
# return item in self._r
return self._r.exists(item)
esi_client_id = os.getenv('ESI_CLIENT_ID')
esi_secret_key = os.getenv('ESI_SECRET_KEY')
esi_cache = ESICache(host=os.getenv("REDIS_URL"), port=int(os.getenv("REDIS_PORT")), db="0",
password=os.getenv("REDIS_PASSWD"))
esi_client = ESIClient.get_client(user_agent=os.getenv('ESI_USER_AGENT'), cache=esi_cache)
esi_auth = ESIAuthenticator()

View File

@@ -46,6 +46,7 @@ REST_FRAMEWORK = {
INSTALLED_APPS = [
'api',
'sde',
'esi',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
@@ -87,6 +88,13 @@ TEMPLATES = [
WSGI_APPLICATION = 'mabras.wsgi.application'
CACHES = {
"default": {
"BACKEND": "django.core.cache.backends.redis.RedisCache",
"LOCATION": f"redis://{os.getenv('REDIS_URL')}:{os.getenv('REDIS_PORT')}/{os.getenv('REDIS_DB')}",
}
}
# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
@@ -142,3 +150,9 @@ STATIC_URL = 'static/'
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
ESI_SSO_CLIENT_ID = os.getenv("ESI_CLIENT_ID")
ESI_SSO_CLIENT_SECRET = os.getenv("ESI_SECRET_KEY")
ESI_SSO_CALLBACK_URL = os.getenv("ESI_CALLBACK_URL")
ESI_USER_AGENT = os.getenv("ESI_USER_AGENT")
ESI_USER_CONTACT_EMAIL = os.getenv("ESI_USER_AGENT")

View File

@@ -17,14 +17,13 @@ Including another URLconf
from django.urls import include, path
from rest_framework.schemas import get_schema_view
from django.views.generic import TemplateView
from sde.urls import urlpatterns as sde_urls
from api.urls import urlpatterns as api_urls
urlpatterns = [
path('api/', include(api_urls)),
path('sde/', include(sde_urls)),
path('api/', include("api.urls")),
path('sde/', include("sde.urls")),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
path('sso/', include('esi.urls', namespace='esi')),
path('openapi/', get_schema_view(
title="Mabras",
description="API for EvEal",