Mcache
view release on metacpan or search on metacpan
lib/Mcache.pm view on Meta::CPAN
my $classname = shift;
my $self = {};
bless($self,$classname);
$self->_init(@_);
return $self;
}
sub _init {
my $self = shift;
if (@_) {
}
}
sub DESTROY {
}
###################
sub add {
my $self = shift;
my @in = @_;
my $key;
if ($in[1]) {
if ($in[2]) {
$key = "$in[2]";
}
else {
# generate unique KEY
$key = "$in[1]"."-".rand(1000000)."-".time."-".rand(3141592652);
}
$self->{$in[1]}->{"$key"} = "\"key\": \"$key\", ".$in[0];
my $str = "\"list\": [";
my $count = 0;
my $rndid = rand(314159265);
foreach my $d (sort keys %{$self->{$in[1]}}) {
if ($d eq 'CACHE') {next;}
if ($d eq 'STAT') {next;}
if ($d eq 'ID') {next;}
$str = $str." {".$self->{$in[1]}->{$d}."},";
++$count;
$rndid = $rndid + rand(314159265);
}
chop($str);
$str = $str."], \"count\": \"$count\"";
$self->{$in[1]}->{'CACHE'} = $str;
$self->{$in[1]}->{'STAT'} = $count;
$self->{$in[1]}->{'ID'} = $rndid;
undef $str;
undef $rndid;
undef @in;
return $key;
}
else {
return 0;
}
}
sub update {
my $self = shift;
my @in = @_;
my $key;
if ($in[2]) {
$key = "$in[2]";
if (exists $self->{$in[1]}->{$in[2]}) {
$self->{$in[1]}->{$in[2]} = "\"key\": \"$key\", ".$in[0];
my $str = "\"list\": [";
my $count = 0;
my $rndid = rand(314159265);
foreach my $d (sort keys %{$self->{$in[1]}}) {
if ($d eq 'CACHE') {next;}
if ($d eq 'STAT') {next;}
if ($d eq 'ID') {next;}
$str = $str." {".$self->{$in[1]}->{$d}."},";
++$count;
$rndid = $rndid + rand(314159265);
}
chop($str);
$str = $str."], \"count\": \"$count\"";
$self->{$in[1]}->{'CACHE'} = $str;
$self->{$in[1]}->{'STAT'} = $count;
$self->{$in[1]}->{'ID'} = $rndid;
undef $str;
undef $rndid;
undef @in;
}
else {
# add(@in);
}
}
}
sub del {
my $self = shift;
my @in = @_;
if ($in[1]) {
if (exists $self->{$in[0]}->{$in[1]}) {
delete($self->{$in[0]}->{$in[1]});
# print "del: $in[1]\n";
}
else {
# print "no del\n";
}
my $str = "\"list\": [";
my $count = 0;
my $rndid = rand(314159265);
foreach my $d (sort keys %{$self->{$in[0]}}) {
if ($d eq 'CACHE') {next;}
if ($d eq 'STAT') {next;}
if ($d eq 'ID') {next;}
$str = $str." {(-".$d."-)".$self->{$in[0]}->{$d}."},";
++$count;
$rndid = $rndid + rand(314159265);
}
chop($str);
$str = $str."], \"count\": \"$count\"";
( run in 0.936 second using v1.01-cache-2.11-cpan-7fcb06a456a )