madeon (madeon) wrote in changelog,
madeon
madeon
changelog

[livejournal] r22902: LJSUP-13604: Local cache [internal]

Committer: sbelyaev
LJSUP-13604: Local cache [internal]
U   trunk/cgi-bin/LJ/LocalCache/Redis.pm
Modified: trunk/cgi-bin/LJ/LocalCache/Redis.pm
===================================================================
--- trunk/cgi-bin/LJ/LocalCache/Redis.pm	2012-09-17 10:50:27 UTC (rev 22901)
+++ trunk/cgi-bin/LJ/LocalCache/Redis.pm	2012-09-17 11:12:04 UTC (rev 22902)
@@ -4,6 +4,7 @@
 use strict;
 use warnings;
 
+use utf8;
 use Redis;
 
 my $local_connection;
@@ -59,9 +60,10 @@
         return;
     }
 
-    my $recv_data = $connection->get($key);
+    my $value = $connection->get($key);
 
-    return utf8::decode($recv_data);
+    return unless $value;
+    return utf8::encode($value);
 }
 
 sub get_multi {
@@ -109,9 +111,109 @@
 
 sub delete {
     my ($class, $key) = @_;
-    return undef;
+    my $connection = __get_write_conneciton();
+
+    if (!$connection) {
+        return 0;
+    }
+
+    return $connection->del($key);
 }
 
+sub incr {
+    my ($class, $key, $value) = @_;
+    my $connection = __get_write_connection();
+
+    if (!$connection) {
+        return 0;
+    }
+
+    if ($value) {
+        $value = int($value);
+        return 0 unless $value;
+        return $connection->incrby($key, $value);
+    }
+    return $connection->incr($key);
+}
+
+sub decr {
+    my ($class, $key, $value) = @_;
+    my $connection = __get_write_connection();
+
+    if (!$connection) {
+        return 0;
+    }
+
+    if ($value) {
+        $value = int($value);
+        return 0 unless $value;
+        return $connection->decrby($key, $value);
+    }
+
+    return $connection->decr($key);
+}
+
+sub exists {
+    my ($class, $key) = @_;
+    my $connection = __get_read_connection();
+
+    if (!$connection) {
+        return 0;
+    }
+
+    return $connection->exists($key);
+}
+
+sub rpush {
+    my ($class, $key, $value) = @_;
+    my $connection = __get_write_connection();
+
+    if (!$connection) {
+        return 0;
+    }
+
+    $value = utf8::decode($value);
+    return $connection->rpush($key, $value);    
+}
+
+sub lpush {
+    my ($class, $key, $value) = @_;
+    my $connection = __get_write_connection();
+
+    if (!$connection) {
+        return 0;
+    }
+
+    $value = utf8::decode($value);
+    return $connection->lpush($key, $value);
+}
+
+sub lpop {
+    my ($class, $key) = @_;
+    my $connection = __get_read_connection();
+
+    if (!$connection) {
+        return undef;
+    }
+
+    my $value = $connection->lpop($key);
+    return unless $value;
+    return unf8::encode($value);
+}
+
+sub rpop {
+    my ($class, $key) = @_;
+    my $connection = __get_read_connection();
+
+    if (!$connection) {
+        return undef;
+    }
+
+    my $value = $connection->rpop($key);
+    return unless $value;
+    return unf8::encode($value);
+}
+
 1;
 
 

Tags: livejournal, madeon, pm, sbelyaev
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments