Make sure the redis db doesn't grow out of control.

This commit is contained in:
A. Svensson 2015-02-25 18:22:30 +01:00
parent 9549d6b0a9
commit 5b0d097f88
2 changed files with 6 additions and 2 deletions

View File

@ -106,6 +106,9 @@ class Command(BaseCommand):
else: else:
servers_handled.append(data['title']) servers_handled.append(data['title'])
# Keep the amount of data down in redis
history.trim_points(server)
# TODO: do bulk insert instead! # TODO: do bulk insert instead!
server, created = Server.objects.update_or_create( server, created = Server.objects.update_or_create(
title=data['title'], title=data['title'],
@ -118,7 +121,6 @@ class Command(BaseCommand):
# Update the player history # Update the player history
history.add_point(server, now, data['player_count']) history.add_point(server, now, data['player_count'])
history.trim_points(server)
Server.remove_old_servers() Server.remove_old_servers()

View File

@ -42,8 +42,10 @@ class PlayerHistory(object):
self.redis.lpush(server, '{},{}'.format(time, players)) self.redis.lpush(server, '{},{}'.format(time, players))
def trim_points(self, server): def trim_points(self, server):
'''Trim away too old points in the player history.''' '''Trim away too old points and servers in the player history.'''
self.redis.ltrim(server, 0, self.max_points) self.redis.ltrim(server, 0, self.max_points)
# let the list expire after a week without updates
self.redis.expire(server, 604800)
def get_points(self, server): def get_points(self, server):
'''Get a range of points from the player history.''' '''Get a range of points from the player history.'''