前回の記事で、Azure Cache for Redis内にセッションデータを格納してみたが、Azure Cache for Redisのコンソールでセッションデータを確認したところ、日本語がエンコードされていた。
ただ、Redisにアクセスするクライアントライブラリを利用してセッションデータを確認すれば、エンコードされていた日本語データの中身も確認することができる。
今回は、Redisにアクセスするクライアントライブラリによってセッションデータを取得し表示してみたので、そのサンプルプログラムを共有する。
前提条件
下記記事の実装が完了していること。
サンプルプログラムの作成
まずは、STS上で、下記記事の「Mavenプロジェクトの作成」に従って、グループ Id、アーティファクト Idを変えて、Mavenプロジェクトを作成する。
作成したサンプルプログラムの構成は、以下の通り。

pom.xmlの内容は以下の通りで、Redisにアクセスするクライアントライブラリを追加している。
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.demo</groupId>
<artifactId>demoGetRedisVal</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 文字コードとJavaのバージョンの設定 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<!-- プラグインの設定 -->
<build>
<plugins>
<!-- Javaファイルのコンパイラの設定 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<!-- プロジェクトと依存するライブラリを1つにまとめる設定 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<!-- メインプログラムとして実行するクラスの指定 -->
<mainClass>test.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<!-- ライブラリ依存関係の設定 -->
<dependencies>
<!-- Redisにアクセスするクライアントライブラリの設定 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
</project>また、メインクラスの内容は以下の通りで、JedisクラスのhgetAllメソッドで、Azure Cache for Redisの設定値を取得し表示している。なお、JedisクラスのhgetAllメソッドの引数は、セッションデータ作成後に指定している。
package test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo;
public class Main {
public static void main(String[] args) {
// Azure Cache for Redisへの接続先
String cacheHostname = "azurePurinRedis.redis.cache.windows.net";
int cacheHostport = 6380;
boolean useSsl = true;
String cachekey = "(Azure Cache for Redisのパスワード)";
// Azure Cache for Redisに接続する
JedisShardInfo shardInfo
= new JedisShardInfo(cacheHostname, cacheHostport, useSsl);
shardInfo.setPassword(cachekey);
Jedis jedis = new Jedis(shardInfo);
// Azure Cache for Redisの設定値
System.out.println("Cached List : "
+ jedis.hgetAll("spring:session:sessions:bd039cf2-dd6b-4e43-bf15-6f869376fa99"));
// Azure Cache for Redisから切断する
jedis.close();
}
}
サンプルプログラムの実行結果
サンプルプログラムの実行結果は、以下の通り。
1) Azure Portalにログインし、Azure App Serviceを利用した画面を表示し、データ検索を行う。


2) Azure Redisをコンソールで「keys *」コマンドを入力後、「hgetall (セッションデータのキー値)」というコマンドを入力すると、以下のように、日本語データがエンコードされて表示される。

3) 今回作成したサンプルプログラムのMainクラスを実行すると、以下のように、セッションデータの日本語が表示できることが確認できる。


要点まとめ
- Azure Cache for Redis内の日本語データを確認するには、Redisにアクセスするクライアントライブラリに含まれている、JedisクラスのhgetAllメソッドを利用すればよい。





