}
return if !$user;
- my $cache_param = { expire => 60,
- no_cache => ($user && $user->{is_admin} && $user->{config}->{system}->{disable_cache}), };
- my $ad_code = $c->ezcache->model('ad_codes', $cache_param)->select(name => $ad_name);
+ my $cache_param = { no_cache => ($user && $user->{is_admin} && $user->{config}->{system}->{disable_cache}), };
+ my $ad_code = $c->ccache->model('ad_codes', $cache_param)->select(name => $ad_name);
if (!$ad_code) {
return;
$conf->{inmemory_ratio} ||= 0.5;
# define expiration terms
- $conf->{expire_terms} ||= {};
+ $conf->{expire_terms} ||= {
+ "model:users" => "short",
+ "model:stories" => "very_short",
+ "model:journals" => "very_short",
+ "model:comments" => "very_short",
+ "model:timeline" => "very_short",
+ "model:submissions" => "very_short",
+ "model:polls" => "middle",
+ "model:ad_codes" => "short",
+ "siteconfig" => "long",
+ "user-boxes" => "middle",
+ "JavaScriptLoader" => "middle",
+ "messages" => "short",
+ "anonymous_user" => "middle",
+ "Preprocessor" => "middle",
+ };
- my $kvs = $app->kvs;
$self->{app} = $app;
- $self->{kvs} = $kvs;
+ $self->{kvs} = $app->kvs;
$self->{inmemory} = $app->cache;
if (!$self->{app}) {
return;
}
-sub cache {
+sub set {
my ($self, $base_key, $variable_key, $value) = @_;
if (!$base_key) {
$self->app->log->error("CompositeCache::cache: no base_key given.");
if (!defined $value) {
$value = $self->model->select(@params);
if (defined $value) {
- $self->app->ccache->cache($base_key, $variable_key, $value);
+ $self->app->ccache->set($base_key, $variable_key, $value);
}
}
return $value;
use Mojo::File qw(path);
use IO::Compress::Gzip qw(gzip);
-use constant KVS_PREFIX => "JSL:";
+use constant CACHE_KEY => "JavaScriptLoader";
use constant DEFAULT_MEM_EXPIRE => 60;
use constant DEFAULT_KVS_EXPIRE => 60 * 60 * 24;
-sub _kvs {
- return shift->{app}->kvs;
-}
-
-sub _cache {
- return shift->{app}->cache;
-}
-
-sub set_cache {
- my ($self, $key, $value) = @_;
- my $mem_expire = $self->{conf}->{mem_expire};
- my $kvs_expire = $self->{conf}->{kvs_expire};
- $mem_expire = DEFAULT_MEM_EXPIRE if !defined $mem_expire;
- $kvs_expire = DEFAULT_KVS_EXPIRE if !defined $kvs_expire;
-
- $self->_kvs->set(KVS_PREFIX . $key, $value, $kvs_expire);
- $self->_cache->set(KVS_PREFIX . $key, $value, $mem_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;
-
- # Memory cache unhit, then try KVS
- $value = $self->_kvs->get(KVS_PREFIX . $key);
- if (defined $value) {
- if ($value->{gz_content} && $self->{compress}) {
- # TODO: KVS crashed gzipped content, so recover
- gzip(\$value->{content}, \my $compressed);
- $value->{gz_content} = $compressed;
- }
- $self->_cache->set(KVS_PREFIX . $key, $value, $expire);
- return $value;
- }
-
- # no cache found
- return;
-}
sub get_content {
my ($self, $basename, $opts) = @_;
$opts ||= {};
if (!$opts->{reload}) {
- $content = $self->get_cache($basename);
+ $content = $self->{app}->ccache->get(CACHE_KEY, $basename);
return $content if defined $content;
}
}
if (defined $content) {
- $self->set_cache($basename, $content);
- $self->set_cache($content->{path}, $content);
+ $self->{app}->ccache->set(CACHE_KEY, $basename, $content);
+ $self->{app}->ccache->set(CACHE_KEY, $content->{path}, $content);
return $content
}
$self->{app}->log->warn("JavaScriptLoader: no cache content: $basename");
my $c = shift;
my $path = $c->stash('content_path');
if ($path) {
- my $content = $self->get_cache($path);
+ my $content = $self->{app}->ccache->get(CACHE_KEY, $path);
if (!$content) {
# no cache, so try to generate
my $c = shift;
my $epoch = $c->epoch->get;
- my $cache_key = "SITECONFIG:" . $epoch;
+ my $cache_key = "siteconfig";
my $config = { memory_expire => 300, # 5 minutes
kbs_expire => 86400 }; # 1 day
- my $value = $c->ezcache->cached_read($cache_key, $config,
- sub { _generate_site_config(@_) },
- $c);
+ my $value = $c->ccache->get($cache_key, $epoch);
+ if (!defined $value) {
+ $value = _generate_site_config($c);
+ $c->ccache->set($cache_key, $epoch, $value);
+ }
my $json = to_json($value);
return "var siteConfig = $json;";
}
my $c = shift;
my $user = $c->stash("user");
- return $c->ezcache->cached_read("user-boxes:$user->{uid}",
- 60,
- sub { _get_sidebar_item(shift) },
- $c);
+ my $value = $c->ccache->get("user-boxes", $user->{uid});
+ if (!defined $value) {
+ $value = _get_sidebar_item($c);
+ $c->ccache->set("user-boxes", $user->{uid}, $value);
+ }
+ return $value;
}
sub _get_sidebar_item {
}
}
else {
- my $model = $c->ezcache->model($box->{model}, { expire => 60 });
+ my $model = $c->ccache->model($box->{model});
$rs = $model->select(@params);
}
use IO::Compress::Gzip qw(gzip);
use Data::Dumper;
-use constant KVS_PREFIX => "PREP:";
+use constant CACHE_KEY => "Preprocessor";
use constant DEFAULT_MEM_EXPIRE => 60;
use constant DEFAULT_KVS_EXPIRE => 60 * 60 * 24;
return $self->add_content($target, $result, $gen_conf->{"content-type"});
}
-sub _kvs {
- return shift->{app}->kvs;
-}
-
-sub _cache {
- return shift->{app}->cache;
-}
-
-sub set_cache {
- my ($self, $key, $value) = @_;
- my $mem_expire = $self->{conf}->{mem_expire};
- my $kvs_expire = $self->{conf}->{kvs_expire};
- $mem_expire = DEFAULT_MEM_EXPIRE if !defined $mem_expire;
- $kvs_expire = DEFAULT_KVS_EXPIRE if !defined $kvs_expire;
-
- $self->_kvs->set(KVS_PREFIX . $key, $value, $kvs_expire);
- $self->_cache->set(KVS_PREFIX . $key, $value, $mem_expire);
-}
-
-sub get_cache {
- my ($self, $key) = @_;
- my $mem_expire = $self->{conf}->{mem_expire};
- $mem_expire = DEFAULT_MEM_EXPIRE if !defined $mem_expire;
-
- my $value = $self->_cache->get(KVS_PREFIX . $key);
- return $value if defined $value;
-
- # Memory cache unhit, then try KVS
- $value = $self->_kvs->get(KVS_PREFIX . $key);
- if (defined $value) {
- if ($value->{gz_content} && $self->{compress}) {
- # TODO: KVS crashed gzipped content, so recover
- gzip(\$value->{content}, \my $compressed);
- $value->{gz_content} = $compressed;
- }
- $self->_cache->set(KVS_PREFIX . $key, $value, $mem_expire);
- return $value;
- }
-
- $value = $self->_generate($key);
- return $value if $value;
-
- $self->{app}->log->error("Preprocessor get_cache: no cache found for key '$key'");
- return;
-}
-
sub _strip_md5 {
my ($self, $path) = @_;
if ($path =~ m/^(.+)_[0-9a-f]+(\.\w+)$/) {
$value->{gz_content} = $compressed;
}
my $rpath = "$dir${base}_$md5$ext";
- $self->set_cache($pathname, $value);
- $self->set_cache($rpath, $value);
+ $self->{app}->ccache->set(CACHE_KEY, $pathname, $value);
+ $self->{app}->ccache->set(CACHE_KEY, $rpath, $value);
return $value;
}
my $c = shift;
my $path = $c->stash('content_path');
if ($path) {
- my $content = $self->get_cache($path);
+ my $content = $self->{app}->ccache->get(CACHE_KEY, $path);
if ($content) {
# check Etag
if ($c->req->headers->if_none_match
sub get_path {
my ($self, $name) = @_;
- my $content = $self->get_cache($name);
+ my $content = $self->{app}->ccache->get(CACHE_KEY, $name);
if ($content) {
return $content->{path};
}
delete $user->{newpasswd_ts};
if ($user->{is_login}) {
- $user->{messages} = $c->kvs->cache("messages:$user->{uid}",
- 60,
- sub { my ($msg, $uid) = @_; $msg->count_unread($uid) },
- $c->model('web_messages'),
- $user->{uid}
- );
+ my $value = $c->ccache->get("messages", "$user->{uid}");
+ if (!defined $value) {
+ $value = $c->model('web_messages')->count_unread($user->{uid});
+ }
+ $user->{messages} = $value;
}
$c->stash(user => $user);
my $user;
if (!$params->{nocache}) {
- $user = $self->{app}->cache->get('anonymous_user');
+ $user = $self->{app}->ccache->get('anonymous_user', 0);
}
if (!$user) {
my $users = $self->{app}->model('users');
$user = $users->anonymous_user;
$self->_add_user_properties($user);
my $expire = 300; # expire time is 300 sec (5 min)
- $self->{app}->cache->set('anonymous_user', $user, $expire);
+ $self->{app}->ccache->set('anonymous_user', 0, $user);
}
return $user;
$app->plugin('Newslash::Plugin::Model', { bypass_startup => $maintenance_mode });
# use Easy Cache ($app->ezcache)
- $app->plugin('Newslash::Plugin::EasyCache');
+ #$app->plugin('Newslash::Plugin::EasyCache');
- # use Easy Cache ($app->ezcache)
+ # use CompositeCache Cache ($app->ccache)
$app->plugin('Newslash::Plugin::CompositeCache');
# use Template::Toolkit 2 render
$comments = $c->model('comments');
}
else {
- $comments = $c->ezcache->model('comments', { expire => 5 });
+ $comments = $c->ccache->model('comments');
}
my $rs = $comments->select($key => $value);
use Newslash::Util::Formatters;
use Newslash::Util::Items;
-use constant CACHE_SEC => 30;
use constant SUB_SECTIONS => qw(
apple askslashdot developers hardware idle interview
it linux mobile opensource review science security slash yro
my $date = $c->format_timestamp(now => "iso8601");
if ($type eq "sradjp") {
- my $stories = $c->ezcache->model('stories', { expire => CACHE_SEC });
+ my $stories = $c->ccache->model('stories');
$items = $stories->select(hide_future => 1,
public_only => 1,
limit => $limit,
} elsif (any { $type eq $_} (SUB_SECTIONS)) {
# get tid
my $topic = $c->model("topics")->select(keyword => $type);
- my $stories = $c->ezcache->model('stories', { expire => CACHE_SEC });
+ my $stories = $c->ccache->model('stories');
$items = $stories->select(hide_future => 1,
public_only => 1,
limit => $limit,
my $subtitle = ":みんなの日記";
my $date = $c->format_timestamp(now => "iso8601");
- my $journals = $c->ezcache->model('journals', { expire => CACHE_SEC });
+ my $journals = $c->ccache->model('journals');
my $items = $journals->select(limit => $limit,
order_by => {create_time => 'desc'},
);
use Data::Dumper;
use Encode qw(encode);
-use constant CACHE_PREFIX => "SITECONFIG:";
+use constant CACHE_PREFIX => "siteconfig";
use constant DEFAULT_MEM_EXPIRE => 60 * 60;
use constant DEFAULT_KVS_EXPIRE => 60 * 60 * 24;
-sub _kvs {
- return shift->app->kvs;
-}
-
-sub _cache {
- return shift->app->cache;
-}
-
-sub site_config {
+ub site_config {
my $c = shift;
my $epoch = $c->stash('epoch');
# try to read from in-memory cache
- my $value = $c->_cache->get(CACHE_PREFIX . $epoch);
-
- if (!$value) {
- # try to read from KVS
- $value = $c->_kvs->get(CACHE_PREFIX . $epoch);
- if ($value) {
- $c->_cache->set(CACHE_PREFIX . $epoch, $value, DEFAULT_MEM_EXPIRE);
- }
- }
+ my $value = $c->ccache->get(CACHE_PREFIX, $epoch);
if (!$value) {
# generate siteconfig
topics => \@topic_texts,
acl2_types => {},
);
- $c->_cache->set(CACHE_PREFIX . $epoch, $value, DEFAULT_MEM_EXPIRE);
- $c->_kvs->set(CACHE_PREFIX . $epoch, $value, DEFAULT_KVS_EXPIRE);
+ $c->set->cache(CACHE_PREFIX, $epoch, $value);
}
#$c->app->log->info($value);
# this query not fast, because uses multiple queries to get config and prefs
# my $authors = $users->select(author => 1);
- my $authors = $c->ezcache->model("users", { expire => 60 })->select(author => 1);
+ my $authors = $c->ccache->model("users")->select(author => 1);
my $mod_reasons = $c->model('moderations')->reasons;
$c->stash(moderate_reasons => $mod_reasons);
$stories = $c->model('stories');
}
else {
- $stories = $c->ezcache->model('stories', { expire => CACHE_SEC });
+ $stories = $c->ccache->model('stories');
}
my $items = $stories->select(hide_future => !$user->{is_admin},
public_only => !$user->{is_admin},
$timeline = $c->model('timeline');
}
else {
- $timeline = $c->ezcache->model('timeline', { expire => CACHE_SEC });
+ $timeline = $c->ccache->model('timeline');
}
my $items = $timeline->select(limit => $limit);
$timeline = $c->model('timeline');
}
else {
- $timeline = $c->ezcache->model('timeline', { expire => CACHE_SEC });
+ $timeline = $c->ccache->model('timeline');
}
my $items = $timeline->select(limit => $limit,
order_by => {popularity => "DESC"},
$comments = $c->model('comments');
}
else {
- $comments = $c->ezcache->model('comments', { expire => CACHE_SEC });
+ $comments = $c->ccache->model('comments');
}
my $items = $comments->select(limit => $limit + 1,
order_by => {create_time => 'DESC'});
$journals = $c->model('journals');
}
else {
- $journals = $c->ezcache->model('journals', { expire => CACHE_SEC });
+ $journals = $c->ccache->model('journals');
}
my $items = $journals->select(limit => $limit + 1,
order_by => {create_time => 'DESC'});
$submissions = $c->model('submissions');
}
else {
- $submissions = $c->ezcache->model('submissions', { expire => CACHE_SEC });
+ $submissions = $c->ccache->model('submissions');
}
my $items = $submissions->select(limit => $limit + 1,
order_by => {create_time => 'DESC'});
$polls = $c->model('polls');
}
else {
- $polls = $c->ezcache->model('polls', { expire => CACHE_SEC });
+ $polls = $c->ccache->model('polls');
}
my $items = $polls->select(limit => $limit + 1,
order_by => {create_time => 'DESC'});
# simple cache
my $testvalue = "testvalue";
- ok($ccache->cache(".testkey", 1, $testvalue), "cache testvalue");
+ ok($ccache->set(".testkey", 1, $testvalue), "cache testvalue");
is($ccache->get(".testkey", 1), $testvalue, "get testvalue");
ok($ccache->del(".testkey", 1), "delete cache");
my $testvalue2 = { foo => "bar", hoge => 1 };
- ok($ccache->cache(".testkey", 2, $testvalue2), "cache hashvalue");
+ ok($ccache->set(".testkey", 2, $testvalue2), "cache hashvalue");
is_deeply($ccache->get(".testkey", 2), $testvalue2, "get hashvalue");
ok($ccache->del(".testkey", 2), "delete cache");
delete $ccache->{inmemory};
my $testvalue3 = { foo => "bar", hoge => 1 };
- ok($ccache->cache(".testkey", 3, $testvalue3), "kvs cache hashvalue");
+ ok($ccache->set(".testkey", 3, $testvalue3), "kvs cache hashvalue");
is_deeply($ccache->get(".testkey", 3), $testvalue3, "kvs get hashvalue");
ok($ccache->del(".testkey", 3), "delete kvs cache");