Permalink
Browse files

Using redis pipeline for speeeeed in django db sessions -> redis sess…

…ions.
  • Loading branch information...
samuelclay committed Mar 21, 2013
1 parent 095ceef commit bb6aac3b35c01e4ab269268140d91854898bc82d
Showing with 5 additions and 3 deletions.
  1. +5 −3 utils/bootstrap_redis_sessions.py
@@ -5,13 +5,15 @@
sessions_count = Session.objects.count()
print " ---> %s sessions in Django" % sessions_count
batch_size = 100
batch_size = 10
r = redis.Redis(connection_pool=settings.REDIS_SESSION_POOL)
for batch in range(int(math.ceil(sessions_count / batch_size))+1):
start = batch * batch_size
end = (batch + 1) * batch_size
print " ---> Loading sessions #%s - #%s" % (start, end)
pipe = r.pipeline()
for session in Session.objects.all()[start:end]:
_ = r.set(session.session_key, session.session_data)
_ = r.expire(session.session_key, session.expire_date.strftime("%s"))
_ = pipe.set(session.session_key, session.session_data)
_ = pipe.expireat(session.session_key, session.expire_date.strftime("%s"))
_ = pipe.execute()

0 comments on commit bb6aac3

Please sign in to comment.