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);
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";
$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};
}
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});