Spring で Azure Redis Cache を使用する

Azure Cache for Redis は、Redis ソフトウェアをベースにしたメモリ内データストアを提供します。 Redis では、バックエンドのデータ ストアを多用するアプリケーションのパフォーマンスとスケーラビリティを高めることができます。

このチュートリアルでは、Redis Cache を使用して Spring Boot アプリケーションにデータを格納および取得する方法について説明します。

前提条件

アプリケーションをコーディングする

Redis Cache を使用してデータを格納および取得するには、次の手順を実行してアプリケーションを構成します。

  1. 次の例に示すように、application.properties 構成ファイルで Redis Cache 資格情報を構成します。

    # Specify the DNS URI of your Redis cache.
    spring.data.redis.host=<your-redis-name>.redis.cache.windows.net
    
    # Specify the port for your Redis cache.
    spring.data.redis.port=6379
    
    # Specify the access key for your Redis cache.
    spring.data.redis.password=<your-redis-access-key>
    

    Note

    SSL を有効にする Jedis のような異なる Redis クライアントを使用している場合は、SSL 使用する旨を application.properties ファイルで指定し、ポート 6380 を使用します。 次に例を示します。

    # Specify the DNS URI of your Redis cache.
    spring.data.redis.host=<your-redis-name>.redis.cache.windows.net
    # Specify the access key for your Redis cache.
    spring.data.redis.password=<your-redis-access-key>
    # Specify that you want to use SSL.
    spring.data.redis.ssl.enabled=true
    # Specify the SSL port for your Redis cache.
    spring.data.redis.port=6380
    

    詳細については、「クイック スタート: Java で Azure Cache for Redis を使用する」を参照してください。

  2. スタートアップ クラス ファイルを編集して、次の内容を表示します。 このコードは、データを格納および取得します。

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.data.redis.core.ValueOperations;
    
    @SpringBootApplication
    public class DemoCacheApplication implements CommandLineRunner {
    
        private static final Logger LOGGER = LoggerFactory.getLogger(DemoCacheApplication.class);
    
        @Autowired
        private StringRedisTemplate template;
    
        public static void main(String[] args) {
            SpringApplication.run(DemoCacheApplication.class, args);
        }
    
        @Override
        public void run(String... args) {
            ValueOperations<String, String> ops = this.template.opsForValue();
            String key = "testkey";
            if(!this.template.hasKey(key)){
                ops.set(key, "Hello World");
                LOGGER.info("Add a key is done");
            }
            LOGGER.info("Return the value from the cache: {}", ops.get(key));
        }
    
    }
    
  3. アプリケーションを起動します。 アプリケーションは Redis Cache からデータを取得します。 次の例のようなログが表示されます。

    Add a key is done
    Return the value from the cache: Hello World
    

Azure Spring Apps にデプロイする

Spring Boot アプリケーションがローカルで実行されたので、運用環境に移行します。 Azure Spring Apps では、コードを変更せずに、Spring Boot アプリケーションを Azure に簡単にデプロイできます。 Spring アプリケーションのインフラストラクチャはこのサービスによって管理されるため、開発者はコードに専念できます。 Azure Spring Apps では、包括的な監視と診断、構成管理、サービス検出、CI/CD 統合、ブルー/グリーン デプロイなどを使用して、ライフサイクルを管理できます。 Azure Spring Apps にアプリケーションをデプロイするには、「初めてのアプリケーションを Azure Spring Apps にデプロイする」を参照してください。

次のステップ

Spring および Azure の詳細については、Azure ドキュメント センターで引き続き Spring に関するドキュメントをご確認ください。