Using Predix Cache with Java
Get code samples and recommended libraries for using a Predix Cache service with Java.
- Recommended Java Library
- Jedis. See https://github.com/xetorthio/jedis
- Alternative Java Libraries
-
- Lettuce-io. See https://github.com/lettuce-io/lettuce-core
- Lettuce.io and Redis-Sentinel. See https://github.com/lettuce-io/lettuce-core/wiki/Redis-Sentinel
- Redisson. See https://github.com/redisson/redisson
Code Samples Using the Standalone Jedis Library
-
Add the following Jedis Maven dependency:
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> <type>jar</type> <scope>compile</scope> </dependency>
-
Configure the connection to your Redis service using the VCAP_SERVICES environment variable:
// Parse the redis config from the VCAP_SERVICES environment. Assumes there's only one Redis instance bound. String vcapServices = System.getenv("VCAP_SERVICES"); JSONObject vcapJson = new JSONObject(vcapServices); JSONObject credentials = vcapJson.getJSONArray("predix-cache") // .getJSONObject(0) // .getJSONObject("credentials"); // Set up the Jedis pool JedisPoolConfig poolConfig = new JedisPoolConfig(); JedisPool pool = new JedisPool(poolConfig, credentials.getString("host"), credentials.getInt("port"), Protocol.DEFAULT_TIMEOUT, credentials.getString("password"));
-
Test the connection:
// Get a jedis client resource from the pool to set and get key values Jedis jedis = pool.getResource(); jedis.set("foo", "bar"); String value = jedis.get("foo"); // Close the Jedis connector when you're finished with it. jedis.close();
Using Spring Data Redis
The Spring web framework provides a suite of tools for automatically discovering and configuring connections to data services. Include the Spring Data Redis library in your Spring app's classpath to take advantage of these features.
-
Add the following Maven dependency:
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.8.4.RELEASE</version> </dependency>
-
To set up the Redis connection, inject a RedisTemplate Bean with the @Autowired decorator:
@Autowired private RedisTemplate<String, String> redisTemplate;
-
Use the RedisTemplate to test the Redis connection:
// Use the ValueOperations class for simple string value key/value pairs in Redis ValueOperations<String, String> valueOps = redisTemplate.opsForValue(); // Set and get the Redis keys valueOps.set("foo", "bar"); String retrievedValue = valueOps.get("foo");
Jedis for High Availability Service Plans Using Sentinels
Configuring Jedis to use a Redis sentinel connection is similar to the above example for using a Standalone Jedis client. Include the same Maven dependency and parse the credentials from the environment in the same way. However, in this case you need to use a JedisSentinelPool instead of a JedisPool:
// Parse the Credentials from the environment
String vcapServices = System.getenv("VCAP_SERVICES");
JSONObject vcapJson = new JSONObject(vcapServices);
JSONObject credentials = vcapJson.getJSONArray("predix-cache") //
.getJSONObject(0) //
.getJSONObject("credentials");
// Create a set using the Redis hosts from the environment
JSONArray redisNodes = credentials.getJSONArray("ip_list");
int sentinelPort = credentials.getInt("sentinel_port");
HashSet<String> sentinels = new HashSet<>();
for (int i=0; i < redisNodes.length(); i++) {
String sentinel = String.format("%s:%d", cacheNodes.getString(i), sentinelPort);
sentinels.add(sentinel);
}
// Create the redisSentinelPool
String masterName = credentials.getString("master_name");
String password = credentials.getString("password");
JedisSentinelPool pool = new JedisSentinelPool(masterName, sentinels, password);
Test the Predix Cache cluster connection:
Jedis jedis = sentinelPool.getResource();
jedis.set("foo", "bar");
String value = jedis.get("foo");
// Close the Jedis connector when you're finished with it.
jedis.close();