package CGI::Session::Driver::tokyotyrant;

use strict;
use Carp;
use CGI::Session::Driver;
use TokyoTyrant;

@CGI::Session::Driver::tokyotyrant::ISA       = qw( CGI::Session::Driver );
$CGI::Session::Driver::mysql::VERSION   = '0.01';

sub init {
    my $self = shift;

    if (!$self->{Host} && !$self->{Port}) {
	return $self->set_error("init(): invalid params");
    }
    return 1;
}

sub store {
    my $self = shift;
    my ($sid, $datastr) = @_;
    croak "store(): usage error" unless $sid && $datastr;

    my %db = undef;
    if (!tie(%db, 'TokyoTyrant::RDB', $self->{Host}, $self->{Port})) {
	return $self->set_error("store(): connection failed");
    }
    $db{$sid} = $datastr;
    untie %db;
    return 1;
}

sub retrieve {
    my $self = shift;
    my ($sid) = @_;
    croak "retrieve(): usage error" unless $sid;
    my %db = undef;
    if (!tie(%db, 'TokyoTyrant::RDB', $self->{Host}, $self->{Port})) {
	return $self->set_error("store(): connection failed");
    }
    my $datastr = $db{$sid};
    untie %db;
    return $datastr || 0;
}

sub remove {
    my $self = shift;
    my ($sid) = @_;
    croak "remove(): usage error" unless $sid;
    
    my %db = undef;
    if (!tie(%db, 'TokyoTyrant::RDB', $self->{Host}, $self->{Port})) {
	return $self->set_error("store(): connection failed");
    }
    delete $db{$sid};
    untie %db;
    return 1;
}

sub DESTROY {}


1;

__END__;
