// Note; Install Redis in localhost and start the redis server.
package com.demo.redis;import io.lettuce.core.*;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
public class ConnectToRedis {
public static void main(String[] args) {
RedisClient redisClient = RedisClient.create("redis://localhost:6379");
StatefulRedisConnection<String, String> connection = redisClient.connect();
RedisCommands<String, String> syncCommands = connection.sync();
syncCommands.set("key", "Hello, Redis!");
System.out.println("The stored value " + syncCommands.get("key")) ;
connection.close();
try {
redisClient.shutdown();
}catch(Exception e) {
}
}
private RedisClient redisClient;
private StatefulRedisConnection<String, String> connection ;
private RedisCommands<String, String> syncCommands ;
public void startredisClient() {
redisClient = RedisClient.create("redis://localhost:6379");
connection = redisClient.connect();
syncCommands = connection.sync();
}
public void stopredisClient() {
connection.close();
try {
redisClient.shutdown();
}catch(Exception e) {
}
}
public void SetValues(String key, String value) {
syncCommands.set(key, value);
}
public String getValue(String key) {
String value = syncCommands.get(key);
return value ;
}
}
No comments:
Post a Comment