OSDN Git Service

Plugin::QuasiStaticContent: now use kvs and cache to store contents
authorhylom <hylom@users.sourceforge.jp>
Thu, 6 Apr 2017 11:29:31 +0000 (20:29 +0900)
committerhylom <hylom@users.sourceforge.jp>
Thu, 6 Apr 2017 11:29:31 +0000 (20:29 +0900)
src/newslash_web/lib/Newslash/Plugin/QuasiStaticContent.pm

index d88717f..2d122aa 100644 (file)
@@ -10,6 +10,9 @@ use IO::Select;
 use POSIX ":sys_wait_h";
 use Mojo::Util qw(dumper);
 
+use constant KVS_PREFIX => "QSC:";
+use constant DEFAULT_MEM_EXPIRE => 60;
+
 sub _debug {
     my ($self, $string) = @_;
     $self->{app}->log->debug($string);
@@ -62,12 +65,54 @@ sub _generate {
                  path => $path,
                  type => $gen_conf->{"content-type"},
                 };
-    $self->{contents}->{$target} = $value;
     my $rpath = "$dir${base}_$md5$ext";
-    $self->{contents}->{$rpath} = $value;
+
+    $self->set_cache($target, $value);
+    $self->set_cache($rpath, $value);
     return 1;
 }
 
+sub _kvs {
+    return shift->{app}->kvs;
+}
+
+sub _cache {
+    return shift->{app}->cache;
+}
+
+sub set_cache {
+    my ($self, $key, $value) = @_;
+    my $expire = $self->{conf}->{mem_expire};
+    $expire = DEFAULT_MEM_EXPIRE if !defined $expire;
+
+    $self->_kvs->set(KVS_PREFIX . $key, $value);
+    $self->_cache->set(KVS_PREFIX . $key, $value, $expire);
+}
+
+sub get_cache {
+    my ($self, $key) = @_;
+    my $expire = $self->{conf}->{mem_expire};
+    $expire = DEFAULT_MEM_EXPIRE if !defined $expire;
+
+    my $value = $self->_cache->get(KVS_PREFIX . $key);
+    return $value if defined $value;
+
+    $self->_kvs->get(KVS_PREFIX . $key);
+    if (defined $value) {
+        $self->_cache->set(KVS_PREFIX . $key, $value, $expire);
+        return $value;
+    }
+    return;
+}
+
+sub _strip_md5 {
+    my ($self, $path) = @_;
+    if ($path == m/^(.+)_[0-9a-f]+(\.\w+)$/) {
+        return $1 . $2;
+    }
+    return $path;
+}
+
 sub static_dir {
     my $self = shift;
     return "/static";
@@ -112,18 +157,17 @@ sub register {
 
     $self->{conf} = {%$conf, %{$app->config->{QuasiStaticContent}}};
     $self->{app} = $app;
-    $self->{contents} = {};
 
     $self->_generate_all;
-    my $contents = $self->{contents};
-
+    #$self->{contents} = {};
+    #my $contents = $self->{contents};
     #$app->log->debug(dumper($contents));
 
     if ($app->config->{TT2Renderer}) {
         my $tt2r = $app->config->{TT2Renderer}->{self};
         $tt2r->add_NS_function('static_content', sub {
                                    my ($c, $name) = @_;
-                                   my $content = $contents->{$name};
+                                   my $content = $self->get_cache($name);
                                    if ($content) {
                                        return $content->{path};
                                    }
@@ -136,7 +180,7 @@ sub register {
                 my $c = shift;
                 my $path = $c->stash('content_path');
                 if ($path) {
-                    my $content = $contents->{$path};
+                    my $content = $self->get_cache($path);
                     if ($content) {
                         $c->res->headers->content_type($content->{type});
                         $c->res->body($content->{content});