madeon (madeon) wrote in changelog,
madeon
madeon
changelog

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

Committer: sbelyaev
LJSUP-13604: Local cache [internal]
U   trunk/cgi-bin/LJ/LocalCache/BerkeleyDB.pm
Modified: trunk/cgi-bin/LJ/LocalCache/BerkeleyDB.pm
===================================================================
--- trunk/cgi-bin/LJ/LocalCache/BerkeleyDB.pm	2012-09-11 12:35:50 UTC (rev 22861)
+++ trunk/cgi-bin/LJ/LocalCache/BerkeleyDB.pm	2012-09-11 13:46:57 UTC (rev 22862)
@@ -9,10 +9,23 @@
 my $connection;
 my $lifetime;
 
+sub __reset_connection {
+    if ($connection) {
+        $connection->db_close();
+        $connection = undef;
+    }
+}
+
+sub DESTROY {
+    __reset_connection();
+}
+
 sub __get_instance {
     my ($class) = @_;
 
     if ($connection) {
+        die "CDS is not enabled " 
+            unless $connection->cds_enabled();
         return $connection;
     }
 
@@ -23,8 +36,11 @@
     my $env = new BerkeleyDB::Env
                   -Home   => $LJ::LJ_LOCAL_BDB_HOME,
                   -Flags  => DB_CREATE| DB_INIT_CDB | DB_INIT_MPOOL,
+                  -LockDetect => DB_LOCK_OLDEST,
+                  -ErrFile => *STDERR
         or die "cannot open environment: $BerkeleyDB::Error\n";
 
+    return unless $env;
     die "CDS is not enabled " unless $env->cds_enabled();
 
     $connection = new BerkeleyDB::Hash
@@ -42,6 +58,7 @@
     my ($class, $key) = @_;
 
     my $db = $class->__get_instance();
+    return unless $db;
 
     my $data = '';
     my $status = $db->db_get($key, $data);
@@ -56,6 +73,11 @@
         }
 
         my $lock = $db->cds_lock();
+        unless ($lock) {
+            __reset_connection();
+            return;
+        }
+
         eval { $db->db_del($key); };
         $lock->cds_unlock();
     }
@@ -86,7 +108,14 @@
     my $cache_data = "$expire_time:$data";
 
     my $db = $class->__get_instance();
+    return unless $db;
+
     my $lock = $db->cds_lock();
+    unless ($lock) {
+        __reset_connection();
+        return 0;
+    }   
+
     eval { $db->db_put($key, $cache_data); };
     $lock->cds_unlock();
 
@@ -95,13 +124,20 @@
 
 sub replace {
     my ($class, $key, $value, $expire) = @_;
-    return undef;
+    return 0;
 }
 
 sub delete {
     my ($class, $key) = @_;
     my $db = $class->__get_instance();
+    return unless $db;
+
     my $lock = $db->cds_lock();
+    unless ($lock) {
+        __reset_connection();
+        return  0;
+    }
+
     eval { $db->db_del($key); };
     $lock->cds_unlock();
     return 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