...
Info |
---|
CAST supports Redis via its NoSQL for Java extension. Details about the support provided for Java source code is discussed below. |
Supported Libraries
...
Operation Image Added
|
Methods Supported | Insert | - lpush
- rpush
- hmset
- sadd
- zadd
- hset
- set
|
Select | - lrange
- smembers
- zscore
- zrevrange
- hget
- hgetAll
- keys
- get
|
Delete | - del
- lrem
- lpop
- rpop
- srem
- spop
- zrem
|
Objects
...
Icon
...
Description
...
Image Removed
...
Java Redis Connection
...
Image Removed
...
Image Removed
...
Image Removed
...
Links
Links are created for transaction and function point needs:
...
Between Redis connection object and Redis collection object
...
Between the caller Java method object and Redis collection object
...
- lrange
- smembers
- zscore
- zrevrange
- hget
- hgetAll
- keys
- get
...
- lpush
- rpush
- hmset
- sadd
- zadd
- hset
- set
...
useDeleteLink
...
- del
- lrem
- lpop
- rpop
- srem
- spop
- zrem
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
...
Code Block |
---|
|
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);
}
}
|
...
Supported Operations
Objects
Icon | Description |
---|
Image Added
| Java Redis Connection |
Image Added
| Java Redis Collection |
Image Added
| Java unknown Redis Connection |
Image Added
| 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
| - lrange
- smembers
- zscore
- zrevrange
- hget
- hgetAll
- keys
- get
- getbit
- getex
- getrange
- mget
- strlen
|
useInsertLink | - lpush
- rpush
- hmset
- sadd
- zadd
- hset
- set
- geoadd
- hmset
- hset
- hsetnx
|
useDeleteLink | - del
- lrem
- lpop
- rpop
- srem
- spop
- zrem
- hdel
- spop
- srem
|
useUpdateLink | - hincrby
- hincrbyfloat
- hincrby
- hincrbyfloat
- decr
- decrby
- incr
- incrby
- incrbyfloat
|
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
Expand |
---|
|
Code Block |
---|
| 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);
}
}
|
Image Added
Code Block |
---|
| 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);
}
};
} |
Image Added
|
Insert Operation
Expand |
---|
title | Insert Operation for various Redis Collections |
---|
|
Code Block |
---|
title | Redis Collection Jedis 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 in a set", dept);
} |
Image Added
Code Block |
---|
title | Lettuce Insert Operation |
---|
| // Inserts field=value to myhash
public void addDeptset_operation(StringRedisHashCommands depthashCommands) {
{
hashCommands.hset("myhash", "field1", "value1");
Jedis jedis = new Jedis(server hashCommands.hset("myhash", "field2", "value2");
jedishashCommands.saddhset("myhash", "departmentsfield3", dept"value3");
} |
Image Removed Image Added
|
Select Operation
Expand |
---|
title | Select Operation for various Redis Collections |
---|
|
Expand |
---|
title | Delete Operation for various Redis Collections
Code Block |
---|
title | Redis Collection Jedis 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");
} |
Image Removed
|
---|
Delete Operation
Image Added
Code Block |
---|
title | Redis Collection 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()) | // retrieves all the data from myhash
public void get_operation(RedisHashCommands hashCommands, KeyValueStreamingChannel channel){
hashCommands.hgetall(channel, "myhash");
} |
Image Added
|
Delete Operation
Expand |
---|
|
Expand |
---|
|
Code Block |
---|
title | Jedis Delete Operation |
---|
| // delete first visitor from visitors set
public Visitor deleteVisitor() {
Jedis jedis = new Jedis(server);
Map<String,Integer> visitors break= get_Visitors();
}Visitor vis = jedis.zrem("visitors", vis.getNamenew Visitor());
for (Map.Entry<String, Integer> returnvisitor vis;
}
//delete department information from cache
public String deleteDeptInfo(String key) {: visitors.entrySet()) {
vis.setName(visitor.getKey());
Jedis jedis = new Jedis(server vis.setId(jedis.zscore("visitors", visitor.getKey()));
String value = jedis.get(key);
break;
}
jedis.del(key.zrem("visitors", vis.getName());
return keyvis;
+}
" =
"//delete +department valueinformation + " successfully deleted";
} |
Image Removed
|
Jedis Commands
Code Block |
---|
title | Jedis Commands CRUD operations |
---|
| public void addActiveNum(Integer userId,double score)from cache
public String deleteDeptInfo(String key) {
intJedis monthjedis = DateUtil.thisMonth()+1new Jedis(server);
String keyvalue = CommonConstant.FQ_ACTIVE_USER_SORT+month;
jedis.get(key);
jedis.del(key);
try {return key + " = " + value + JedisCommands" commandssuccessfully = JedisProviderFactory.getJedisCommands(null);
Double scoreStore = commands.zscore(key,userId.toString());
if(scoreStore == null){
scoreStore = score;
}else {
scoreStore += score;
}
commands.zadd(key,scoreStore,userId.toString());
commands.expire(key,180*24*60*60);
} catch (Exception e) {
logger.error(e);
}finally {
JedisProviderFactory.getJedisProvider(null).release();
}
} |
Image Removed
Code Block |
---|
public static void refreshUserCacheByUid(Integer userId){
String cacheKey = "FqUser.id:"+userId;
try {
JedisCommands commands = JedisProviderFactory.getJedisCommands(null);deleted";
} |
Image Added
Code Block |
---|
title | Lettuce Delete Operation |
---|
| // Deletes field3 and field4 from myhash
public void del_operation(RedisHashCommands hashCommands){
hashCommands.hdel("myhash", "field3", "field4");
} |
Image Added
|
Update Operation
Expand |
---|
|
Code Block |
---|
title | Jedis 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();
}
}
|
Image Added
Code Block |
---|
title | Lettuce Update Operation |
---|
| // Updates myhash field4 by 10
public void update_operation(RedisHashCommands hashCommands){
hashCommands.hincrby("myhash", "field4", 10);
}
|
Image Added
|
Jedis Commands
Expand |
---|
|
Code Block |
---|
| void set_value(){
// Add some fields to the hash
transaction.hset("myhash", "field1", "3");
transaction.hset("myhash", "field2", "2");
transaction.hset("myhash", "field3", "3");
FqUserService fqUserService = SpringUtils.getBean("fqUserServiceImpl");
// Execute the transaction
FqUser fqUser = fqUserServicetransaction.selectByPrimaryKeyexec_(userId);
FqUserCache fqUserCache = new FqUserCache(fqUser);
commands.set(cacheKey,JSON.toJSONString(fqUserCache));
commands.expire(cacheKey,300);
} catch (Exception e) {
logger.error("refreshUserCacheByUid",e);
}finally {} |
Image Added
Code Block |
---|
| void get_value() {
// Retrieve the values after the transaction
System.out.println(transaction.hget("myhash", "field1"));
}
|
Image Added
Code Block |
---|
| void del_value() {
// Perform hdel operation within a transaction
transaction.hdel("myhash", "field3");
// Execute the transaction
transaction.exec_();
}
|
Image Added
Code Block |
---|
| void incr_value() {
// Perform hincrBy operation within a transaction
JedisProviderFactorytransaction.getJedisProvider(null).release();hincrBy("myhash", "field1", 5);
}
|
Image Removed
Code Block |
---|
public void remove(final String key, final String... member){
return exec(new RedisCallback<Long>() {
@Override
public Long doCallback(JedisCommands commands) {
return commands.zrem(key, member // Execute the transaction
transaction.exec_();
}
});
} |
Image Removed Image Added
|
...
Lettuce Commands
Expand |
---|
title | Transaction Command operations |
---|
|
Code Block |
---|
| public void transaction()
set_operation(RedisHashCommands hashCommands){
String user = "user:1000";
String restaurantOrderCount = "restaurant_orders:200";
String restaurantUsers = "restaurant_users:200";hashCommands.hset("myhash", "field1", "value1");
jedishashCommands.set(restaurantOrderCounthset("myhash", "field2", "400value2");
jedis hashCommands.saddhset(restaurantUsers"myhash", "user:302field3", "user:401value3");
}
|
Image Added
Code Block |
---|
| public Transaction transaction = jedis.multi();
Response<Long> countResponse = transaction.incr(restaurantOrderCount);void get_operation(RedisHashCommands hashCommands, KeyValueStreamingChannel channel){
transactionhashCommands.saddhgetall(restaurantUserschannel, user"myhash");
} |
Image Added
Code Block |
---|
| public void Response<Set<String>> userSet = transaction.smembers(restaurantUsers);del_operation(RedisHashCommands hashCommands){
//Execute transaction transactionhashCommands.exec(hdel("myhash", "field3", "field4");
} |
Image Added
Code Block |
---|
| public void update_operation(RedisHashCommands hashCommands){
hashCommands.hincrby("myhash", System.exit(0"field4", 10);
} |
Image Removed Image Added
|
...
Limitations
- Resolution increased for Redis Connection and collection objects
- Crud operation support for Jedis commands and transactions
Future Development
- Support for package redis.clients.jedis.params
Unknown Redis connection/collection objects are created in case of unresolved names.