Using Predix Cloud Cache Service

Connect to Predix Cloud Cache Clusters using Redis and Golang

Before You Begin

  • Download the Redis client from the Golang Library for Redis that you want to use for connecting the Predix Cloud Cache clusters to the applications.
  • Ensure that the Redis client supports Transport Level Security (TLS) and is set to nil.

About This Task

The Predix Cloud Cache clusters are enabled with at-rest and in-transit encryptions. To access data from Predix Cloud Cache enabled with these encryptions, you can use a Redis client that is supported with TLS. Using the Redis client with TLS, you can secure the connections between the Predix Cloud Cache clusters and the user applications.

Code Example for Connecting to Predix Cloud Cache Clusters using Redis and Golang

The following code example connects the Predix Cloud Cache clusters to the user applications using Redis and Golang.

package main

import (
"crypto/tls"
"fmt"
"log"
"net/http"
"os"

"github.com/go-redis/redis"
)

func main() {

var port string
port = os.Getenv("PORT")
if port == "" {
port = "8080"
}
client := newClient()

err := ping(client)
if err != nil {
fmt.Println(err)
}
err = set(client)
if err != nil {
fmt.Println(err)
}

err = get(client)
if err != nil {
fmt.Println(err)
}

fmt.Println("Starting Test HTTP Server.........")
err = http.ListenAndServe(":"+port, nil)
if err != nil {
log.Fatal(err)
}

}

//return redis client

func newClient() *redis.Client {
client := redis.NewClient(&redis.Options{
Addr: "host:port",
Password: "password", // no password set -
TLSConfig: &tls.Config{}, //for at transit encryption
})

return client
}

// ping tests connectivity for redis (PONG should be returned)
func ping(client *redis.Client) error {
fmt.Println("executing Ping Pong....")
pong, err := client.Ping().Result()
if err != nil {
return err
}
fmt.Println(pong, err)
// Output: PONG <nil>
fmt.Println("PING PONG operation is done")
return nil
}

// set executes the redis Set command
func set(client *redis.Client) error {
err := client.Set("key", "value", 0).Err()
if err != nil {
return err
}
return nil
}

func get(client *redis.Client) error {
val, err := client.Get("key").Result()
if err != nil {
return (err)
}
fmt.Println("key", val)

val2, err := client.Get("key2").Result()
if err == redis.Nil {
fmt.Println("key2 does not exist")
} else if err != nil {
panic(err)
} else {
fmt.Println("key2", val2)
}
// Output: key value
// key2 does not exist

return nil
}

func helloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:])
}

Connect to Predix Cloud Cache Clusters using Redis and Java

Before You Begin

About This Task

The Predix Cloud Cache clusters are enabled with at-rest and in-transit encryptions. To access data from Predix Cloud Cache enabled with these encryptions, you can use a Java Redis client that is supported with TLS. Using Jedis, you can secure the connections between the Predix Cloud Cache clusters and the user applications.

Code Example for Connecting to Predix Cloud Cache Clusters using Redis and Java

The following code example connects the Predix Cloud Cache clusters to the user applications using Redis and Java.

...

import redis.clients.jedis.Jedis;


public class CloudCacheSample {

public static String HOST_URL = <Value of host from VCAP_SERVICES environment Variable>;
public static String PASSWORD = <Value of password from VCAP_SERVICES environment Variable>;
public static int PORT = 6379;
 
...


 public void redisSample() {
 

  /* prefix 'rediss://' instructs jedis to open a SSL/TLS connection */

  URI jedisHostURI = URI.create("rediss://"+HOST_URL+":"+PORT);
  System.out.println("URI: "+jedisHostURI);

  /* Creating new jedis client for connecting with redis instance */
  Jedis jedisClient = new Jedis(jedisHostURI);
  jedisClient.auth(PASSWORD);
  System.out.println("Connection Made Successfully");
  

  /* Pinging Instance to verify its healthy */
  System.out.println("Server is running: " + jedisClient.ping());



  /* Reading and writing to redis instance*/
  jedisClient.lpush("SampleKey", "Sample Data1");
  jedisClient.lpush("SampleKey", "Sample Data2");
  
  List<String> list = jedisClient.lrange("SampleKey", 0, 1);
  
  for(int i=0; i<list.size(); i++)
  {
   System.out.println("Item Value: "+list.get(i));
  }
  

  /* Closing and exiting gracefully */
  jedisClient.disconnect();
  jedisClient.close();
  
 }

...


}