Add management commands
This commit is contained in:
parent
f4a0edd091
commit
ec6acaf8ea
2 changed files with 54 additions and 0 deletions
33
gallery/management/commands/list_sessions.py
Normal file
33
gallery/management/commands/list_sessions.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
import json
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.management.base import BaseCommand
|
||||
from django_redis import get_redis_connection
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "List active Django sessions from Redis"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--details', action='store_true', help='Print session data')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
cache_prefix = getattr(settings, 'SESSION_CACHE_ALIAS', 'default')
|
||||
redis_conn = get_redis_connection(cache_prefix)
|
||||
|
||||
# Redis-avaimet istuntoja varten (django-redis käyttää 'session:' prefixiä)
|
||||
keys = redis_conn.keys('session:*')
|
||||
count = len(keys)
|
||||
|
||||
self.stdout.write(f"Aktiivisia sessioita Redisissä: {count}")
|
||||
|
||||
if options['details'] and count > 0:
|
||||
self.stdout.write("\nSession ID:t ja sisällöt:\n" + "-"*40)
|
||||
for key in keys:
|
||||
session_data = redis_conn.get(key)
|
||||
try:
|
||||
decoded = session_data.decode("utf-8")
|
||||
except Exception:
|
||||
decoded = str(session_data)
|
||||
|
||||
self.stdout.write(f"{key.decode('utf-8')}: {decoded}\n")
|
Loading…
Add table
Add a link
Reference in a new issue