How to use Azure Redis Cache with Java

Azure Redis Cache gives you access to a dedicated Redis cache, managed by Microsoft. Your cache is accessible from any application within Microsoft Azure.

This topic shows you how to get started with Azure Redis Cache using Java.

Prerequisites

Jedis - Java client for Redis

This tutorial uses Jedis, but you can use any Java client listed at http://redis.io/clients.

Create a Redis cache on Azure

To create a cache, first sign in to the Azure portal, and click Create a resource > Databases > Redis Cache.

New cache

In New Redis Cache, configure the settings for your new cache.

Setting Suggested value Description
DNS name Globally unique name The cache name must be a string between 1 and 63 characters and contain only numbers, letters, and the - character. The cache name cannot start or end with the - character, and consecutive - characters are not valid.
Subscription Your subscription The subscription under which this new Azure Redis Cache is created.
Resource Group TestResources Name for the new resource group in which to create your cache. By putting all the resources for an app in a group, you can manage them together. For example, deleting the resource group would delete all resources associated with the app.
Location East US Choose a region near to other services that will use your cache.
Pricing tier Basic C0 (250 MB Cache) The pricing tier determines the size, performance, and features available for the cache. For more information, see Azure Redis Cache Overview.
Pin to dashboard Selected Click pin the new cache to your dashboard making it easy to find.

Create cache

Once the new cache settings are configured, click Create.

It can take a few minutes for the cache to be created. To check the status, you can monitor the progress on the dashboard. After the cache has been created, your new cache has a Running status and is ready for use.

Cache created

Retrieve the host name and access keys

Retrieve host name, ports, and access keys using the Azure Portal

When connecting to an Azure Redis Cache instance, cache clients need the host name, ports, and a key for the cache. Some clients may refer to these items by slightly different names. You can retrieve this information in the Azure portal.

To retrieve the access keys using the Azure portal, browse to your cache and click Access keys.

Redis cache keys

To retrieve host name, ports, click Properties.

Redis cache properties

Connect to the cache securely using SSL

The latest builds of jedis provide support for connecting to Azure Redis Cache using SSL. The following example shows how to connect to Azure Redis Cache using the SSL endpoint of 6380. Replace <name> with the name of your cache and <key> with either your primary or secondary key as described in the previous Retrieve the host name and access keys section.

boolean useSsl = true;
/* In this line, replace <name> with your cache name: */
JedisShardInfo shardInfo = new JedisShardInfo("<name>.redis.cache.windows.net", 6380, useSsl);
shardInfo.setPassword("<key>"); /* Use your access key. */

Note

The non-SSL port is disabled for new Azure Redis Cache instances. If you are using a different client that doesn't support SSL, see How to enable the non-SSL port.

Add something to the cache and retrieve it

package com.mycompany.app;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo;

public class App
{
  public static void main( String[] args )
  {
    boolean useSsl = true;
    /* In this line, replace <name> with your cache name: */
    JedisShardInfo shardInfo = new JedisShardInfo("<name>.redis.cache.windows.net", 6380, useSsl);
    shardInfo.setPassword("<key>"); /* Use your access key. */
    Jedis jedis = new Jedis(shardInfo);
    jedis.set("foo", "bar");
    String value = jedis.get("foo");
  }
}

Next steps