Skip to content

Latest commit

 

History

History
218 lines (148 loc) · 8.24 KB

cache-java-get-started.md

File metadata and controls

218 lines (148 loc) · 8.24 KB
title description author ms.author ms.date ms.topic ms.service ms.devlang ms.custom
Quickstart: Use Azure Cache for Redis in Java
In this quickstart, you create a new Java app that uses Azure Cache for Redis
KarlErickson
zhihaoguo
01/04/2022
quickstart
cache
java
devx-track-java, devx-track-javaee, mode-api, mvc, devx-track-extended-java

Quickstart: Use Azure Cache for Redis in Java

In this quickstart, you incorporate Azure Cache for Redis into a Java app using the Jedis Redis client. Your cache is a secure, dedicated cache that is accessible from any application within Azure.

Skip to the code on GitHub

Clone the repo Java quickstart on GitHub.

Prerequisites

Create an Azure Cache for Redis

[!INCLUDE redis-cache-create]

[!INCLUDE redis-cache-access-keys]

Set up the working environment

Depending on your operating system, add environment variables for your Host name and Primary access key that you noted previously. Open a command prompt, or a terminal window, and set up the following values:

export REDISCACHEHOSTNAME=<your-host-name>.redis.cache.windows.net
export REDISCACHEKEY=<your-primary-access-key>
set REDISCACHEHOSTNAME=<your-host-name>.redis.cache.windows.net
set REDISCACHEKEY=<your-primary-access-key>

Replace the placeholders with the following values:

  • <your-host-name>: The DNS host name, obtained from the Properties section of your Azure Cache for Redis resource in the Azure portal.
  • <your-primary-access-key>: The primary access key, obtained from the Access keys section of your Azure Cache for Redis resource in the Azure portal.

Understand the Java sample

In this sample, you use Maven to run the quickstart app.

  1. Change to the new redistest project directory.

  2. Open the pom.xml file. In the file, you see a dependency for Jedis:

    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>4.1.0</version>
      <type>jar</type>
      <scope>compile</scope>
    </dependency>
  3. Close the pom.xml file.

  4. Open App.java and see the code with the following code:

    package example.demo;
    
    import redis.clients.jedis.DefaultJedisClientConfig;
    import redis.clients.jedis.Jedis;
    
    /**
     * Redis test
     *
     */
    public class App 
    {
        public static void main( String[] args )
        {
    
            boolean useSsl = true;
            String cacheHostname = System.getenv("REDISCACHEHOSTNAME");
            String cachekey = System.getenv("REDISCACHEKEY");
    
            // Connect to the Azure Cache for Redis over the TLS/SSL port using the key.
            Jedis jedis = new Jedis(cacheHostname, 6380, DefaultJedisClientConfig.builder()
                .password(cachekey)
                .ssl(useSsl)
                .build());
    
            // Perform cache operations using the cache connection object...
    
            // Simple PING command
            System.out.println( "\nCache Command  : Ping" );
            System.out.println( "Cache Response : " + jedis.ping());
    
            // Simple get and put of integral data types into the cache
            System.out.println( "\nCache Command  : GET Message" );
            System.out.println( "Cache Response : " + jedis.get("Message"));
    
            System.out.println( "\nCache Command  : SET Message" );
            System.out.println( "Cache Response : " + jedis.set("Message", "Hello! The cache is working from Java!"));
    
            // Demonstrate "SET Message" executed as expected...
            System.out.println( "\nCache Command  : GET Message" );
            System.out.println( "Cache Response : " + jedis.get("Message"));
    
            // Get the client list, useful to see if connection list is growing...
            System.out.println( "\nCache Command  : CLIENT LIST" );
            System.out.println( "Cache Response : " + jedis.clientList());
    
            jedis.close();
        }
    }

    This code shows you how to connect to an Azure Cache for Redis instance using the cache host name and key environment variables. The code also stores and retrieves a string value in the cache. The PING and CLIENT LIST commands are also executed.

  5. Close the App.java.

Build and run the app

  1. First, if you haven't already, you must set the environment variables as noted previously.

    export REDISCACHEHOSTNAME=<your-host-name>.redis.cache.windows.net
    export REDISCACHEKEY=<your-primary-access-key>
    set REDISCACHEHOSTNAME=<your-host-name>.redis.cache.windows.net
    set REDISCACHEKEY=<your-primary-access-key>

  2. Execute the following Maven command to build and run the app:

    mvn compile
    mvn exec:java -D exec.mainClass=example.demo.App
    mvn compile
    mvn exec:java -D exec.mainClass=example.demo.App

In the following output, you can see that the Message key previously had a cached value. The value was updated to a new value using jedis.set. The app also executed the PING and CLIENT LIST commands.

Cache Command  : Ping
Cache Response : PONG

Cache Command  : GET Message
Cache Response : Hello! The cache is working from Java!

Cache Command  : SET Message
Cache Response : OK

Cache Command  : GET Message
Cache Response : Hello! The cache is working from Java!

Cache Command  : CLIENT LIST
Cache Response : id=777430 addr=             :58989 fd=22 name= age=1 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 ow=0 owmem=0 events=r cmd=client numops=6

Clean up resources

If you continue to use the quickstart code, you can keep the resources created in this quickstart and reuse them.

Otherwise, if you're finished with the quickstart sample application, you can delete the Azure resources created in this quickstart to avoid charges.

Important

Deleting a resource group is irreversible and that the resource group and all the resources in it are permanently deleted. Make sure that you do not accidentally delete the wrong resource group or resources. If you created the resources for hosting this sample inside an existing resource group that contains resources you want to keep, you can delete each resource individually instead of deleting the resource group.

  1. Sign in to the Azure portal and select Resource groups.

  2. In the Filter by name textbox, type the name of your resource group. The instructions for this article used a resource group named TestResources. On your resource group in the result list, select ... then Delete resource group.

    :::image type="content" source="media/cache-java-get-started/azure-cache-redis-delete-resource-group.png" alt-text="Screenshot of the Azure portal that shows the Resource groups page with the Delete resource group button highlighted." lightbox="media/cache-java-get-started/azure-cache-redis-delete-resource-group.png":::

  3. Type the name of your resource group to confirm deletion and then select Delete.

After a few moments, the resource group and all of its contained resources are deleted.

Next steps

In this quickstart, you learned how to use Azure Cache for Redis from a Java application. Continue to the next quickstart to use Azure Cache for Redis with an ASP.NET web app.