NewsBlur/utils/archive/bootstrap_redis_sessions.py

21 lines
733 B
Python
Raw Permalink Normal View History

import math
2024-04-24 09:50:42 -04:00
import redis
from django.conf import settings
from django.contrib.sessions.models import Session
sessions_count = Session.objects.count()
2020-06-15 05:09:10 -04:00
print((" ---> %s sessions in Django" % sessions_count))
2013-03-20 22:00:03 -07:00
batch_size = 1000
r = redis.Redis(connection_pool=settings.REDIS_SESSION_POOL)
2024-04-24 09:43:56 -04:00
for batch in range(int(math.ceil(sessions_count / batch_size)) + 1):
start = batch * batch_size
end = (batch + 1) * batch_size
2020-06-15 05:09:10 -04:00
print((" ---> Loading sessions #%s - #%s" % (start, end)))
pipe = r.pipeline()
for session in Session.objects.all()[start:end]:
_ = pipe.set(session.session_key, session.session_data)
_ = pipe.expireat(session.session_key, session.expire_date.strftime("%s"))
2024-04-24 09:43:56 -04:00
_ = pipe.execute()