madeon (madeon) wrote in changelog,
madeon
madeon
changelog

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

Committer: sbelyaev
LJSUP-13604: Local cache [internal]
U   trunk/cgi-bin/LJ/Lang.pm
U   trunk/cgi-bin/LJ/LocalCache/BerkeleyDB.pm
Modified: trunk/cgi-bin/LJ/Lang.pm
===================================================================
--- trunk/cgi-bin/LJ/Lang.pm	2012-09-14 08:04:10 UTC (rev 22894)
+++ trunk/cgi-bin/LJ/Lang.pm	2012-09-14 08:33:57 UTC (rev 22895)
@@ -446,7 +446,6 @@
 
     if ($text) {
         LJ::MemCache::set( "ml.${lncode}.${dmid}.${itcode}", $text );
-        LJ::LocalCache::get_cache()->set( "ml.${lncode}.${dmid}.${itcode}", $text );
     }
 
     my @langids;
@@ -740,7 +739,7 @@
     return \%strings unless %memkeys;
 
     my $mem = {};
-    if (LJ::is_enabled('local_cache')) {
+    if (LJ::is_enabled('local_cache') &&  LJ::is_web_context()) {
         my @keys_memcache = ();
         my @keys = keys %memkeys;
 
@@ -748,7 +747,7 @@
            $mem = LJ::MemCache::get_multi( @keys_memcache ) || {};
 
         foreach my $key (keys %$mem) {
-            LJ::LocalCache::get_cache()->set($key, $mem->{$key});
+            LJ::LocalCache::get_cache()->set($key, $mem->{$key}, 30*60);
         }
 
         foreach my $key (keys %$mem_local) {
@@ -822,13 +821,11 @@
 
         if ($text) {
             LJ::MemCache::set( $cache_key, $text );
-            LJ::LocalCache::get_cache()->set($cache_key, $text);
         } else {
             ## Do not cache empty values forever - they may be inserted later.
             ## This is a hack, what we actually need is a mechanism to delete
             ## the entire language tree for a given $code if it's updated.
             LJ::MemCache::set( $cache_key, $text, 24 * 3600 );
-            LJ::LocalCache::get_cache()->set( $cache_key, $text, 24 * 3600 );
         }
     }
 

Modified: trunk/cgi-bin/LJ/LocalCache/BerkeleyDB.pm
===================================================================
--- trunk/cgi-bin/LJ/LocalCache/BerkeleyDB.pm	2012-09-14 08:04:10 UTC (rev 22894)
+++ trunk/cgi-bin/LJ/LocalCache/BerkeleyDB.pm	2012-09-14 08:33:57 UTC (rev 22895)
@@ -5,6 +5,7 @@
 use warnings;
 
 use BerkeleyDB;
+use File::Path;
 
 my $connection;
 my $lifetime;
@@ -16,10 +17,6 @@
     }
 }
 
-sub DESTROY {
-    __reset_connection();
-}
-
 sub __get_instance {
     my ($class) = @_;
 
@@ -29,28 +26,49 @@
         return $connection;
     }
 
+    return unless LJ::is_web_context();
     unless(-d $LJ::LJ_LOCAL_BDB_HOME) {
-        mkdir $LJ::LJ_LOCAL_BDB_HOME;
+        mkdir $LJ::LJ_LOCAL_BDB_HOME, 0755;
     }
 
+    my $db_file_name = "$LJ::LJ_LOCAL_BDB_HOME/$LJ::LJ_LOCAL_BDB_NAME";
+    if (-e $db_file_name) {
+        my $status = BerkeleyDB::db_verify( -Filename => $db_file_name, 
+                                            -Outfile => $db_file_name);
+
+        if ($status) {
+            rmtree($LJ::LJ_LOCAL_BDB_HOME);
+            mkdir $LJ::LJ_LOCAL_BDB_HOME, 0755;
+        }
+    }   
+
     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";
+        or die "Environment error: $BerkeleyDB::Error\n";
 
     return unless $env;
+    
     die "CDS is not enabled " unless $env->cds_enabled();
+   
+    my $status = $env->set_timeout(1, DB_SET_LOCK_TIMEOUT);
+    if ($status) {
+        die "set_timeout error $status" if $status;
+    } 
 
     $connection = new BerkeleyDB::Hash
-                    -Filename => $LJ::LJ_LOCAL_BDB_NAME,
-                    -Flags    => DB_CREATE,
-                    -Nelem    => 400,
-                    -Env      => $env,
-                or die "Cannot open file " . $LJ::LJ_LOCAL_BDB_NAME .
-                        ": $! $BerkeleyDB::Error\n";
+                               -Filename => $LJ::LJ_LOCAL_BDB_NAME,
+                               -Flags    => DB_CREATE,
+                               -Nelem    => 400,
+                               -Env      => $env;
 
+    if (!$connection) {
+        rmtree($LJ::LJ_LOCAL_BDB_HOME);
+        return;
+    }
+
     return $connection;
 }
 
@@ -62,6 +80,10 @@
 
     my $data = '';
     my $status = $db->db_get($key, $data);
+    if ($status) {
+        return undef;
+    }
+
     my @parts  = split(/:/, $data, 2);
 
     if (@parts) {
@@ -71,15 +93,6 @@
         if ($expire > time()) {
             return $data;
         }
-
-        my $lock = $db->cds_lock();
-        unless ($lock) {
-            __reset_connection();
-            return;
-        }
-
-        eval { $db->db_del($key); };
-        $lock->cds_unlock();
     }
 
     return undef;
@@ -88,6 +101,9 @@
 sub get_multi {
     my ($class, $keys, $not_fetched_keys) = @_;
 
+    my $db = $class->__get_instance();
+    return unless $db;
+
     my $result = {};
     foreach my $key (@$keys) {
         my $res = $class->get($key);
@@ -110,21 +126,17 @@
     my $db = $class->__get_instance();
     return unless $db;
 
-    my $lock = $db->cds_lock();
-    unless ($lock) {
-        __reset_connection();
+    my $status = $db->db_put($key, $cache_data);
+    if ($status) {
         return 0;
-    }   
+    }
 
-    eval { $db->db_put($key, $cache_data); };
-    $lock->cds_unlock();
-
     return 1;
 }
 
 sub replace {
     my ($class, $key, $value, $expire) = @_;
-    return 0;
+    return $class->set($key, $value, $expire);
 }
 
 sub delete {
@@ -132,14 +144,10 @@
     my $db = $class->__get_instance();
     return unless $db;
 
-    my $lock = $db->cds_lock();
-    unless ($lock) {
-        __reset_connection();
-        return  0;
+    my $status = $db->db_del($key);
+    if ($status) {
+        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