- Created by user-1a1b1, last modified by N Padmavathi on Jul 07, 2023
CAST supports Redis via its NoSQL for Java extension. Details about the support provided for Java source code is discussed below.
Supported Libraries
Library | Version | Supported |
---|---|---|
Jedis | Up to: 4.3.2 | |
JedisPool | ||
JedisSentinePool | ||
JedisCommands | ||
JedisTransactions | ||
JedisTransactionBase | ||
Lettuce | Up to: 6.2.2 | |
RedisClient | ||
RedisCommands | ||
RedisAsyncCommands |
Supported Operations
Operation | Methods Supported |
---|---|
Insert | Insert Operation APIs Jedis Commands Base Jedis Command
Transaction Commands
JedisCommands Commands
Lettuce Commands
|
Select | Select Operation APIs Jedis Commands Base Jedis Commands
Transaction Commands
JedisCommands Commands
Lettuce Commands
|
Delete | Delete Operation APIs Jedis Commands Base Jedis Commands
Transaction Commands
JedisCommands Commands
Lettuce Commands
|
Update | Update Operation APIs Jedis Commands Base Jedis Commands
Transaction Commands
JedisCommands Commands
Lettuce Commands
|
Objects
Icon | Description |
---|---|
Java Redis Connection | |
Java Redis Collection | |
Java unknown Redis Connection | |
Java Unknown Redis Collection |
Links
Links are created for transaction and function point needs:
Link type | When is this created? | Methods Supported |
---|---|---|
belongsTo | From Redis collection object to Redis connection object | |
useSelectLink | Between the caller Java method object and Redis collection object |
|
useInsertLink |
| |
useDeleteLink |
| |
useUpdateLink |
|
What results can you expect?
Once the analysis/snapshot generation has completed, you can view the results in the normal manner (for example via CAST Enlighten). Some examples are shown below.
Redis Connection
public class RedisService { public static String server = "localhost"; public Boolean addEmployee(Employee employee) { Jedis jedis = new Jedis(server); } # for unknown public void addDeptInfo(String key, String data) { Jedis jedis = new Jedis(RedisProperties.connection); jedis.set(key, data); } }
public static void main(String[] args) { RedisClient client = RedisClient.create("redis://localhost"); StatefulRedisConnection<String, String> connection = client.connect(); RedisHashCommands<String, String> hashCommands = connection.sync().commands(); KeyValueStreamingChannel<String, String> channel = new KeyValueStreamingChannel<String, String>() { @Override public void onKeyValue(String key, String value) { System.out.println(key + " => " + value); } }; }
Insert Operation
//adding employee in a redis list public Boolean addEmployee(Employee employee) { Jedis jedis = new Jedis(server); Employee emp = new Employee(); emp=getEmployee(employee.getEmployeeId()); jedis.lpush("employeelist", employee.toString()); return true; } //add departments in a set public void addDept(String dept) { Jedis jedis = new Jedis(server); jedis.sadd("departments", dept); }
// Inserts field=value to myhash public void set_operation(RedisHashCommands hashCommands){ hashCommands.hset("myhash", "field1", "value1"); hashCommands.hset("myhash", "field2", "value2"); hashCommands.hset("myhash", "field3", "value3"); }
Select Operation
//get all employees from employee list public List<Employee> getAllEmployee(){ List<Employee> employeeList = new ArrayList<>(); Jedis jedis = new Jedis(server); List<String> employees = jedis.lrange("employeelist", 0, jedis.llen("employeelist")); for (String employee: employees) { String[] emp1=employee.split(","); Employee emp = new Employee(Integer.parseInt(emp1[0]), emp1[1], emp1[2]); employeeList.add(emp); } return employeeList; } //get all departments from set public Set<String> getDepts() { Jedis jedis = new Jedis(server); return jedis.smembers("departments"); }
// retrieves all the data from myhash public void get_operation(RedisHashCommands hashCommands, KeyValueStreamingChannel channel){ hashCommands.hgetall(channel, "myhash"); }
Delete Operation
// delete first visitor from visitors set public Visitor deleteVisitor() { Jedis jedis = new Jedis(server); Map<String,Integer> visitors = get_Visitors(); Visitor vis = new Visitor(); for (Map.Entry<String, Integer> visitor : visitors.entrySet()) { vis.setName(visitor.getKey()); vis.setId(jedis.zscore("visitors", visitor.getKey())); break; } jedis.zrem("visitors", vis.getName()); return vis; } //delete department information from cache public String deleteDeptInfo(String key) { Jedis jedis = new Jedis(server); String value = jedis.get(key); jedis.del(key); return key + " = " + value + " successfully deleted"; }
// Deletes field3 and field4 from myhash public void del_operation(RedisHashCommands hashCommands){ hashCommands.hdel("myhash", "field3", "field4"); }
Update Operation
public class Example { public static void main(String[] args) { // Create a new Jedis instance connected to Redis Jedis jedis = new Jedis("localhost"); // Set the initial value of a key "mycounter" to 10 jedis.set("mycounter", "10"); // Decrement the value of "mycounter" by 3 using decrBy method Long newCounterValue = jedis.decrBy("mycounter", 3); // Retrieving the elements of mycounter System.out.println("Elements in mycounter: " + jedis.smembers("mycounter")); // Close the Jedis instance jedis.close(); } }
// Updates myhash field4 by 10 public void update_operation(RedisHashCommands hashCommands){ hashCommands.hincrby("myhash", "field4", 10); }
Jedis Commands
void set_value(){ // Add some fields to the hash transaction.hset("myhash", "field1", "3"); transaction.hset("myhash", "field2", "2"); transaction.hset("myhash", "field3", "3"); // Execute the transaction transaction.exec_(); }
void get_value() { // Retrieve the values after the transaction System.out.println(transaction.hget("myhash", "field1")); }
void del_value() { // Perform hdel operation within a transaction transaction.hdel("myhash", "field3"); // Execute the transaction transaction.exec_(); }
void incr_value() { // Perform hincrBy operation within a transaction transaction.hincrBy("myhash", "field1", 5); // Execute the transaction transaction.exec_(); }
Lettuce Commands
public void set_operation(RedisHashCommands hashCommands){ hashCommands.hset("myhash", "field1", "value1"); hashCommands.hset("myhash", "field2", "value2"); hashCommands.hset("myhash", "field3", "value3"); }
public void get_operation(RedisHashCommands hashCommands, KeyValueStreamingChannel channel){ hashCommands.hgetall(channel, "myhash"); }
public void del_operation(RedisHashCommands hashCommands){ hashCommands.hdel("myhash", "field3", "field4"); }
public void update_operation(RedisHashCommands hashCommands){ hashCommands.hincrby("myhash", "field4", 10); }
Limitations
Unknown Redis connection/collection objects are created in case of unresolved names.
- No labels