madeon (madeon) wrote in changelog,
madeon
madeon
changelog

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

Committer: sbelyaev
LJSUP-13604: Local cache [internal]
A   trunk/cgi-bin/LJ/LocalCache/Redis.pm
U   trunk/cgi-bin/LJ/LocalCache.pm
Added: trunk/cgi-bin/LJ/LocalCache/Redis.pm
===================================================================
--- trunk/cgi-bin/LJ/LocalCache/Redis.pm	                        (rev 0)
+++ trunk/cgi-bin/LJ/LocalCache/Redis.pm	2012-09-14 14:15:04 UTC (rev 22899)
@@ -0,0 +1,102 @@
+package LJ::LocalCache::Redis;
+use base(LJ::LocalCache);
+
+use strict;
+use warnings;
+
+my $local_connection;
+my $master_connection;
+
+sub __get_read_connection {
+    if ($local_connection) {
+        return $local_connection;
+    }
+     
+    $local_connection = eval { Redis->new(encoding => undef) };
+    if ($@ && $LJ::IS_DEV_SERVER) {
+        warn "get read connection error: $@";
+    }
+
+    return $local_connection;
+}
+
+sub __get_write_conneciton {
+    if ($master_connection) {
+        return $master_connection;
+    }   
+
+    $master_connection = eval { Redis->new(
+        server => $LJ::MASTER_REDIS_LIGTH_CACHE,
+        debug  => 0,
+        encoding => undef); 
+    };
+    
+    if ($@ && $LJ::IS_DEV_SERVER) {
+        warn "get write conenction error: $@" if $LJ::IS_DEV_SERVER;
+        return;
+    }
+
+    return $master_connection;
+}
+
+sub get {
+    my ($class,$key) = @_;
+    my $connection = __get_read_connection();
+    if (!$connection) {
+        return;
+    }
+
+    return $connection->get($key);
+}
+
+sub get_multi {
+    my ($class, $keys, $not_fetched_keys) = @_;
+
+    my $connection = __get_read_connection();
+    if (!$connection) {
+        @$not_fetched_keys = @$keys;
+        return;
+    }
+
+    my @data = $connection->mget(@$keys);
+    
+    my $result;
+    foreach my $key (@$keys) {
+        my $value = shift @data;
+
+        if ($value) {
+            $result->{$key} = $value; 
+        } else {
+            push @{$not_fetched_keys}, $key;
+        }
+    }
+
+   return $result;
+}
+
+sub set {
+    my ($class, $key, $value, $expire) = @_;
+    my $connection = __get_write_conneciton();
+    if (!$connection || !$value) {
+        return 0;
+    }
+
+    my $result = $connection->set( $key, 
+                                   $value );
+    $connection->expire($key, $expire);
+    return $result;
+}
+
+sub replace {
+    my ($class, $key, $value, $expire) = @_;
+    return $class->set($key);
+}
+
+sub delete {
+    my ($class, $key) = @_;
+    return undef;
+}
+
+1;
+
+

Modified: trunk/cgi-bin/LJ/LocalCache.pm
===================================================================
--- trunk/cgi-bin/LJ/LocalCache.pm	2012-09-14 13:06:18 UTC (rev 22898)
+++ trunk/cgi-bin/LJ/LocalCache.pm	2012-09-14 14:15:04 UTC (rev 22899)
@@ -43,7 +43,7 @@
 }
 
 sub set {
-    my ($class, @keys) = @_;
+    my ($class, $key, $value, $expire) = @_;
     return undef;
 }
 

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