madeon (madeon) wrote in changelog,
madeon
madeon
changelog

[livejournal] r22854: LJSUP-13604: Local cache

Committer: sbelyaev
LJSUP-13604: Local cache
A   trunk/cgi-bin/LJ/LocalCache/
A   trunk/cgi-bin/LJ/LocalCache/BerkeleyDB.pm
A   trunk/cgi-bin/LJ/LocalCache.pm
Added: trunk/cgi-bin/LJ/LocalCache/BerkeleyDB.pm
===================================================================
--- trunk/cgi-bin/LJ/LocalCache/BerkeleyDB.pm	                        (rev 0)
+++ trunk/cgi-bin/LJ/LocalCache/BerkeleyDB.pm	2012-09-11 09:34:18 UTC (rev 22854)
@@ -0,0 +1,112 @@
+package LJ::LocalCache::BerkeleyDB;
+use base(LJ::LocalCache);
+
+use strict;
+use warnings;
+
+use BerkeleyDB;
+
+my $connection;
+my $lifetime;
+
+sub __get_instance {
+    my ($class) = @_;
+
+    if ($connection) {
+        return $connection;
+    }
+
+    warn "create connection";
+    unless(-d $LJ::LJ_LOCAL_BDB_HOME) {    
+        mkdir $LJ::LJ_LOCAL_BDB_HOME;
+    }
+
+    my $env = new BerkeleyDB::Env
+                  -Home   => $LJ::LJ_LOCAL_BDB_HOME,
+                  -Flags  => DB_CREATE| DB_INIT_CDB | DB_INIT_MPOOL,
+        or die "cannot open environment: $BerkeleyDB::Error\n";
+
+    die "CDS is not enabled " unless $env->cds_enabled();
+
+    $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";
+
+    return $connection;
+}
+
+sub get {
+    my ($class, $key) = @_;
+
+    my $db = $class->__get_instance();
+
+    my $data = '';
+    my $status = $db->db_get($key, $data);
+    my @parts  = split(/:/, $data, 2);
+
+    if (@parts) {
+        my $expire  = int($parts[0]);
+        my $data    = $parts[1];
+
+        if ($expire > time()) {
+            return $data;
+        }
+
+        my $lock = $db->cds_lock();
+        eval { $db->db_del($key); };
+        $lock->cds_unlock();
+    }
+
+    return undef;
+}
+
+sub get_multi {
+    my ($class, $keys, $not_fetched_keys) = @_;
+
+    my $result = {};
+    foreach my $key (@$keys) {
+        my $res = $class->get($key);
+        if ($res) {
+            $result->{$key} = $res;
+        } elsif ($not_fetched_keys) {
+            push @{$not_fetched_keys}, $key;
+        }
+    }
+    return $result;
+}
+
+sub set {
+    my ($class, $key, $data, $expire) = @_;
+
+    $expire ||= 10 * 60;
+    my $expire_time = time() + $expire; 
+    my $cache_data = "$expire_time:$data";
+
+    my $db = $class->__get_instance();
+    my $lock = $db->cds_lock();
+    eval { $db->db_put($key, $cache_data); };
+    $lock->cds_unlock();
+
+    return 1;
+}
+
+sub replace {
+    my ($class, $key, $value, $expire) = @_;
+    return undef;
+}
+
+sub delete {
+    my ($class, $key) = @_;
+    my $db = $class->__get_instance();
+    my $lock = $db->cds_lock();
+    eval { $db->db_del($key); };
+    $lock->cds_unlock();
+    return 1;
+}
+
+1;
+

Added: trunk/cgi-bin/LJ/LocalCache.pm
===================================================================
--- trunk/cgi-bin/LJ/LocalCache.pm	                        (rev 0)
+++ trunk/cgi-bin/LJ/LocalCache.pm	2012-09-11 09:34:18 UTC (rev 22854)
@@ -0,0 +1,49 @@
+package LJ::LocalCache;
+use strict;
+use warnings;
+
+use LJ::ModuleLoader;
+
+my @SUBCLASSES = LJ::ModuleLoader->module_subclasses(__PACKAGE__);
+
+foreach my $class (@SUBCLASSES) {
+    eval "use $class";
+    if ($@) {
+        die "Error loading package $class: $@" 
+    }
+}
+
+sub get_cache {
+    my ($handler) = @_;
+    $handler ||= $LJ::LOCAL_CACHE_DEFAULT_HANDLER;
+
+    return "LJ::LocalCache::$handler";
+}
+
+sub get {
+    my ($class,$key) = @_;
+    return undef;
+}
+
+sub get_multi {
+    my ($class, $keys, $not_fetched_keys) = @_;
+    return undef;
+}
+
+sub set {
+    my ($class, @keys) = @_;
+    return undef;
+}
+
+sub replace {
+    my ($class, $key, $value, $expire) = @_;
+    return undef;
+}
+
+sub delete {
+    my ($class, $key) = @_;
+    return undef;
+}
+
+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