Skip to content

Commit

Permalink
connect to redis servers through unix sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
beniwohli committed Jan 19, 2012
1 parent 74bef1c commit ed1160d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
31 changes: 31 additions & 0 deletions src/redisboard/migrations/0003_auto__chg_field_redisserver_port.py
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,31 @@
# encoding: utf-8
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models

class Migration(SchemaMigration):

def forwards(self, orm):

# Changing field 'RedisServer.port'
db.alter_column('redisboard_redisserver', 'port', self.gf('django.db.models.fields.IntegerField')(null=True))


def backwards(self, orm):

# Changing field 'RedisServer.port'
db.alter_column('redisboard_redisserver', 'port', self.gf('django.db.models.fields.IntegerField')())


models = {
'redisboard.redisserver': {
'Meta': {'unique_together': "(('hostname', 'port'),)", 'object_name': 'RedisServer'},
'hostname': ('django.db.models.fields.CharField', [], {'max_length': '250'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'password': ('django.db.models.fields.CharField', [], {'max_length': '250', 'null': 'True', 'blank': 'True'}),
'port': ('django.db.models.fields.IntegerField', [], {'default': '6379', 'null': 'True', 'blank': 'True'})
}
}

complete_apps = ['redisboard']
24 changes: 19 additions & 5 deletions src/redisboard/models.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.db import models from django.db import models
from django.core.validators import MaxValueValidator, MinValueValidator from django.core.validators import MaxValueValidator, MinValueValidator
from django.core.exceptions import ValidationError
from django.conf import settings from django.conf import settings


from .utils import cached_property from .utils import cached_property
Expand Down Expand Up @@ -41,20 +42,31 @@ class Meta:
("can_inspect", "Can inspect redis servers"), ("can_inspect", "Can inspect redis servers"),
) )


hostname = models.CharField(_("Hostname"), max_length=250) hostname = models.CharField(_("Hostname"), max_length=250, help_text=_('this can also be the absolute path to a redis socket'))
port = models.IntegerField(_("Port"), validators=[ port = models.IntegerField(_("Port"), validators=[
MaxValueValidator(65535), MinValueValidator(1) MaxValueValidator(65535), MinValueValidator(1)
], default=6379) ], default=6379, blank=True, null=True)
password = models.CharField(_("Password"), max_length=250, password = models.CharField(_("Password"), max_length=250,
null=True, blank=True) null=True, blank=True)


def clean(self):
if not self.hostname.startswith('/') and not self.port:
raise ValidationError(_('Please provide either a hostname AND a port or the path to a redis socket'))



@cached_property @cached_property
def connection(self): def connection(self):
if self.hostname.startswith('/'):
unix_socket_path = self.hostname
hostname = None
else:
hostname = self.hostname
unix_socket_path = None
return redis.Redis( return redis.Redis(
host = self.hostname, host = hostname,
port = self.port, port = self.port,
password = self.password password = self.password,
unix_socket_path=unix_socket_path,
) )


@connection.deleter @connection.deleter
Expand Down Expand Up @@ -98,4 +110,6 @@ def stats(self):




def __unicode__(self): def __unicode__(self):
return "%s:%s" % (self.hostname, self.port) if self.port:
return "%s:%s" % (self.hostname, self.port)
return self.hostname

0 comments on commit ed1160d

Please sign in to comment.