From af3c10fecad80cdac8665ddcb606c1bd5bc4b717 Mon Sep 17 00:00:00 2001 From: hylom Date: Thu, 6 Oct 2016 18:41:50 +0900 Subject: [PATCH] Plugin::KeyValueStore: implement basic methods --- .../lib/Newslash/Plugin/KeyValueStore.pm | 89 ++++++++++++---------- src/newslash_web/lib/Newslash/Web.pm | 5 ++ 2 files changed, 55 insertions(+), 39 deletions(-) diff --git a/src/newslash_web/lib/Newslash/Plugin/KeyValueStore.pm b/src/newslash_web/lib/Newslash/Plugin/KeyValueStore.pm index 9c30abf1..aa52aebe 100644 --- a/src/newslash_web/lib/Newslash/Plugin/KeyValueStore.pm +++ b/src/newslash_web/lib/Newslash/Plugin/KeyValueStore.pm @@ -1,61 +1,72 @@ package Newslash::Plugin::KeyValueStore; use Mojo::Base 'Mojolicious::Plugin'; +use Mojo::JSON qw(decode_json encode_json); use Data::Dumper; -use DBI; +use Redis; -my ($class, $options) = @_; - bless {options => $options}, $class; +sub new { + my ($class, $options) = @_; + bless {options => $options}, $class; } -sub connect_db { +sub _connect { my $self = shift; - my $options = shift || {}; - - my $DB_HOST = $self->{options}->{host}; - my $DB_NAME = $self->{options}->{name}; - my $DB_USER = $self->{options}->{user}; - my $DB_PASSWORD = $self->{options}->{password}; + my $r = Redis->new(server => $self->{options}->{host}); + $r->connect; + return $r; +} - my $settings = 'mysql_read_default_group=libmysqlclient;mysql_read_default_file=/etc/mysql/my.cnf'; - my $attr = { - mysql_enable_utf8 => 1, - Callbacks => { # hack to use utf8mb4. see http://d.hatena.ne.jp/hirose31/20141028/1414496347 - connected => sub { - shift->do('SET NAMES utf8mb4'); - return; - } - }, - %$options, - }; +sub hset { + my ($self, $key, $field, $value) = @_; + my $r = $self->_connect; + my $result = $r->hset($key, $field, encode_json($value)); + return $result; +} - my $dbh = DBI->connect("DBI:mysql:$DB_NAME:$DB_HOST;$settings", $DB_USER, $DB_PASSWORD, $attr); - return $dbh; +sub hget { + my ($self, $key, $field) = @_; + my $r = $self->_connect; + my $result = $r->hget($key, $field); + if ($result) { + return decode_json($result); + } else { + return undef; + } } -sub _create_table { - my $self = @_; - my $DB_TABLE = $self->{options}->{table}; - my $sql = <<"EOSQL"; -CREATE TABLE IF NOT EXISTS $DB_TABLE ( - name varchar(255) NOT NULL, - key varchar(255) NOT NULL, - value mediumtext NOT NULL -PRIMARY KEY (name, key) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC -EOSQL +sub hdel { + my $self = shift; + my $key = shift; + my $r = $self->_connect; + my $result = $r->hdel($key, @_); + return $result; +} sub set { - my ($self, $name, $key, $value) = @_; + my ($self, $key, $value) = @_; + my $r = $self->_connect; + my $result = $r->set($key, encode_json($value)); + return $result; } sub get { - my ($self, $name, $key) = @_; + my ($self, $key) = @_; + my $r = $self->_connect; + my $result = $r->get($key); + if ($result) { + return decode_json($result); + } else { + return undef; + } } -sub clear { - my ($self) = @_; -}} +sub del { + my ($self, $key) = @_; + my $r = $self->_connect; + my $result = $r->del($key); + return $result; +} sub register { my ($self, $app, $conf) = @_; diff --git a/src/newslash_web/lib/Newslash/Web.pm b/src/newslash_web/lib/Newslash/Web.pm index 85eb227c..bc3226fb 100644 --- a/src/newslash_web/lib/Newslash/Web.pm +++ b/src/newslash_web/lib/Newslash/Web.pm @@ -20,6 +20,11 @@ sub startup { # secret key for hasing $app->secrets([$app->config->{System}->{secret_key},]); + # use KeyValue Store + my $kvs_opts = $app->config->{KeyValueStore}; + $app->plugin('Newslash::Plugin::KeyValueStore'); + $app->helper(kvs => sub { state $kvs = Newslash::Plugin::KeyValueStore->new($kvs_opts)}); + # add Model my $model_opts = $app->config->{Database}; $model_opts->{logger} = $app->log; -- 2.11.0