かんがるーさんの日記

最近自分が興味をもったものを調べた時の手順等を書いています。今は Spring Boot をいじっています。

Spring Boot で書籍の貸出状況確認・貸出申請する Web アプリケーションを作る ( その2 )( Redis ( Windows 版 ) のインストール )

概要

Spring Boot で書籍の貸出状況確認・貸出申請する Web アプリケーションを作る ( その1 )( 概要 ) の続きです。

  • 今回の手順で確認できるのは以下の内容です。
    • Redis ( Windows 版 ) のインストール

参照したサイト・書籍

  1. Redis
    http://redis.io/

  2. MSOpenTech/redis
    https://github.com/MSOpenTech/redis

目次

  1. はじめに
  2. Redis on Windows のダウンロード
  3. Redis on Windows のインストール
  4. 動作確認
  5. Redis にセットしたデータは Redis が停止すると消えるのか?
  6. Redis にセットしたデータはいつまで保存されるのか?

手順

はじめに

Redis の Web サイトの Download ページ を見ると、「The Redis project does not officially support Windows.」という記載がありサポートしていないとのことですが、Microsoft Open Tech group が 64bit 版 Windows に移植しているとも書かれていました。

VirtualBox + VagrantCentOS の仮想サーバを立ててやるか迷いましたが、自分が使用している PC は 64bit 版 Windows なので Windows 版をインストールして進めることにします。

Redis on Windows のダウンロード

  1. 以下の URL から最新バージョン 2.8.21 のインストーラ Redis-x64-2.8.21.msi をダウンロードします。

    https://github.com/MSOpenTech/redis/releases

Redis on Windows のインストール

  1. Redis-x64-2.8.21.msi を実行します。

  2. 「Redis on Windows Setup」ダイアログが表示されます。「Next」ボタンをクリックします。

  3. 「End-User License Agreement」画面が表示されます。「I accept the terms in the License Agreement」チェックボックスをチェックした後、「Next」ボタンをクリックします。

  4. Destination Folder」画面が表示されます。インストール先を C:\Redis\2.8.21\ に変更した後、「Next」ボタンをクリックします。

  5. 「Firewall Exception」画面が表示されます。使用するポート番号が 6379 番と出ています。何も変更せずに「Next」ボタンをクリックします。

  6. 「Ready to install Redis on Windows」画面が表示されます。「Install」ボタンをクリックします。

  7. 「Installing Redis on Windows 」画面が表示され、インストールが実行されます。

  8. インストールが完了すると「Complete the Redis on Windows Setup Wizard」画面が表示されます。「Finish」ボタンをクリックします。

  9. インストーラでインストールすると自動的にサービスとして登録され、開始されています。

    f:id:ksby:20150628105741p:plain

動作確認

コマンドプロンプトを起動し、C:\Redis\2.8.21 の下へ移動します。最初に netstat コマンドで 6379 番のポート番号でサーバが待機しているか確認します。

f:id:ksby:20150628111656p:plain

次に redis-cli コマンドで簡単なデータのセットと取得ができるか確認します。

C:\Redis\2.8.21>redis-cli
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> get "test"
(nil)
127.0.0.1:6379> set "test" "12345678"
OK
127.0.0.1:6379> get "test"
"12345678"
127.0.0.1:6379> keys *
1) "test"
127.0.0.1:6379> del "test"
(integer) 1
127.0.0.1:6379> get "test"
(nil)
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> exit

C:\Redis\2.8.21>

ちなみに redis-cli で使用できるコマンド一覧は help @generichelp @server 等 help の後に @ から始まる文字列を入力すれば出力されます ( help を入力して TAB キーを押すと候補が出てきます )。下の画像は help @generic を実行した結果です。

f:id:ksby:20150628112334p:plain

Redis にセットしたデータは Redis が停止すると消えるのか?

Redis のサービスを停止、開始した場合にデータが保存されるのか消えるのかを確認します。

最初に redis-cli でデータをセットします。

C:\Redis\2.8.21>redis-cli
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set "test" "12345678"
OK
127.0.0.1:6379> get "test"
"12345678"
127.0.0.1:6379> keys *
1) "test"
127.0.0.1:6379> exit

C:\Redis\2.8.21>

Redis サービスを再起動します。

redis-cli でデータが存在するのか消えているのか確認します。

C:\Redis\2.8.21>redis-cli
127.0.0.1:6379> get "test"
(nil)
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> exit

C:\Redis\2.8.21>

消えていました。C:\Redis\2.8.21 の下の redis.windows.conf を見ると以下の設定が記述されており、更新されたキーの数により自動で保存される秒数が変わるようです。

# Save the DB on disk:
#
#   save <seconds> <changes>
#
#   Will save the DB if both the given number of seconds and the given
#   number of write operations against the DB occurred.
#
#   In the example below the behaviour will be to save:
#   after 900 sec (15 min) if at least 1 key changed
#   after 300 sec (5 min) if at least 10 keys changed
#   after 60 sec if at least 10000 keys changed
#
#   Note: you can disable saving completely by commenting out all "save" lines.
#
#   It is also possible to remove all the previously configured save
#   points by adding a save directive with a single empty string argument
#   like in the following example:
#
#   save ""

save 900 1
save 300 10
save 60 10000

1つキーが変更されたら1秒で保存するよう設定し、本当に保存されるのか確認してみます。save 900 1save 1 1 へ変更した後、Redis サービスを再起動します。

再度 redis-cli でデータをセットします。

C:\Redis\2.8.21>redis-cli
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set "test" "12345678"
OK
127.0.0.1:6379> get "test"
"12345678"
127.0.0.1:6379> keys *
1) "test"
127.0.0.1:6379> exit

C:\Redis\2.8.21>

Redis サービスを再起動します。

redis-cli でデータが存在するのか消えているのか確認します。

C:\Redis\2.8.21>redis-cli
127.0.0.1:6379> keys *
1) "test"
127.0.0.1:6379> get "test"
"12345678"
127.0.0.1:6379> del "test"
(integer) 1
127.0.0.1:6379> exit

C:\Redis\2.8.21>

今度は再起動後もデータが残っていました。C:\Redis\2.8.21 の下の redis.windows.conf の設定は元の save 900 1 に戻して、Redis サービスを再起動しておきます。

help @server を実行すると出力されますが、saveshutdown save というコマンドが用意されています。ファイルに必ず反映させたい場合にはこれらのコマンドを実行します。

Redis にセットしたデータはいつまで保存されるのか?

ttl コマンドで有効期限を確認できます。

C:\Redis\2.8.21>redis-cli
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set "test" "12345678"
OK
127.0.0.1:6379> ttl "test"
(integer) -1
127.0.0.1:6379> exit

C:\Redis\2.8.21>

単にセットしたデータの有効期限は -1 と表示されましたので無期限です。削除しない限り残り続けます。

expire コマンドで有効期限を秒数で設定できますので、設定してみます。

C:\Redis\2.8.21>redis-cli
127.0.0.1:6379> set "test" "12345678"
OK
127.0.0.1:6379> keys *
1) "test"
127.0.0.1:6379> ttl "test"
(integer) -1
127.0.0.1:6379> expire "test" 30
(integer) 1
127.0.0.1:6379> ttl "test"
(integer) 27
127.0.0.1:6379> ttl "test"
(integer) 25
127.0.0.1:6379> ttl "test"
(integer) 24
127.0.0.1:6379> keys *
1) "test"
127.0.0.1:6379> ttl "test"
(integer) 13
127.0.0.1:6379> ttl "test"
(integer) 12
127.0.0.1:6379> keys *
1) "test"
127.0.0.1:6379> ttl "test"
(integer) 5
127.0.0.1:6379> ttl "test"
(integer) 3
127.0.0.1:6379> ttl "test"
(integer) 1
127.0.0.1:6379> ttl "test"
(integer) -2
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> get "test"
(nil)
127.0.0.1:6379> exit

C:\Redis\2.8.21>

30 秒で設定すると ttl コマンドで表示される値が 1 秒おきに減っていき、30 秒を過ぎると -2 が表示され、キーも削除されていました。

履歴

2015/06/28
初版発行。