- Created by user-1a1b1, last modified by N Padmavathi on Jul 07, 2023
CAST supports Redis via its NoSQL for Java extension. Details about how this support is provided for Java with Spring Data source code is discussed below.
Supported Libraries
Library | Version | Supported |
---|---|---|
RedisConnectionFactory | Up to: 2.7.7 | |
RedisTemplate | ||
StringRedisTemplate | ||
JedisConnectionFactory | ||
CrudRepository * | ||
JpaRepository * |
* CrudRepository and JpaRepository are supported only if the repository is created using @RedisHash domain.
Supported Operations
Operation | Methods Supported |
---|---|
Insert | Insert Operation APIs
|
Select | Select Operation APIs
|
Delete | Delete Operation APIs
|
Update | Update Operation APIs
|
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 |
---|---|---|
parentLink | Between Redis connection object and Redis collection object | |
useLink | Between the caller Java method object and Redis collection object | |
useSelectLink |
| |
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 Connections and Collections
@Autowired private ServiceConfigResource serviceConfigResource; RedisConnectionFactory switchRedisMaster() { return getRedisConnectionFactory(serviceConfigResource.getSwitchRedisMaster(), serviceConfigResource.getDisRedisPort()); }
@Configuration @RefreshScope @Getter public class ServiceConfigResource { @Value("${disredis.host.disRedisHost1}") private String switchRedisMaster; @Value("${disredis.host.disRedisHost2}") private String switchRedisSlave1; @Value("${disredis.host.disRedisHost3}") private String switchRedisSlave2; @Value("${disredis.port.disRedisPort1}") private int disRedisPort; @Value("${useRoomSharedUsedTable}") private int useRoomSharedUsedTable; }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:redis="http://www.springframework.org/schema/redis" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/redis http://www.springframework.org/schema/redis/spring-redis.xsd "> <!-- Jedis Connection --> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="localhost" p:port="6379" /> <!-- Redis Template --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> <property name="valueSerializer"> <bean id="redisJsonSerializer" class="org.springframework.data.redis.serializer.JacksonJsonRedisSerializer"> <constructor-arg type="java.lang.Class" value="redis.User"/> </bean> </property> </bean> <bean class="redis.UserRepository"/> </beans>
public ReactiveRedisConnectionFactory build() { LettuceClientConfiguration clientConfig; LettuceClientConfiguration.LettuceClientConfigurationBuilder builder = LettuceClientConfiguration.builder(); if (ssl) { builder.commandTimeout(Duration.ofSeconds(timeout)).useSsl(); } clientConfig = builder.build(); RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(); configuration.setHostName(host); configuration.setPassword(password); configuration.setPort(port); return new LettuceConnectionFactory(configuration, clientConfig);
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package io.project.app.domain; import java.io.Serializable; import lombok.AllArgsConstructor; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import lombok.ToString; import org.springframework.data.redis.core.RedisHash; /** * * @author armena */ @RedisHash @Data @AllArgsConstructor @NoArgsConstructor @ToString @EqualsAndHashCode public class SearchData implements Serializable{ private String id; //unique id private String key; //key for search private String title; private String content; }
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package io.project.app.repositories; import io.project.app.domain.SearchData; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Component; import org.springframework.stereotype.Repository; /** * * @author armena */ @Repository @Component public interface SearchRepository extends CrudRepository<SearchData, String>{ public SearchRepository findByTitle(String title); }
Insert Operation
@Bean(name = "switch_redis_master") <T> RedisTemplate<String, T> switchMasterRedis() { RedisTemplate template = new RedisTemplate<>(); FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class); template.setConnectionFactory(switchRedisMaster()); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(fastJsonRedisSerializer); return template; } @Autowired @Qualifier("switch_redis_master") private RedisTemplate<String, Integer> redisTemplate; private boolean invokeFrequencyValidate(String agentCode, Date checkInDate, Date checkOutDate) { int openingHotelNum = hotelService.getOpeningHotelNum(); Date currentDate = new Date(); // 16-90 if (AbstractDateUtils.isAfter(checkInDate, AbstractDateUtils.getDeltaDateByDays(currentDate, 15))) { Integer count = redisTemplate.opsForValue().get(agentCode + "_" + SIXTEEN_TO_NINETY); if (count == null) { redisTemplate.opsForValue().set(agentCode + "_" + SIXTEEN_TO_NINETY, 1, TIME_PERIOD_THREE, TimeUnit.MINUTES); return true; } if (count >= maxQueryBase + openingHotelNum / FIFTEEN_OUTER_DIVIDED) { return false; } return true; }
@Autowired private SearchRepository searchRepository; public SearchData save(SearchData googleSearch) { return searchRepository.save(googleSearch); }
Select Operation
private List<RoomShareUsedCountEntity> calculateRoomShareUsedCountWithType(StandardRoomWithTypeRequest standardRoomWithTypeRequest) { List<String> validDateList = calculateAllKeyDate(standardRoomWithTypeRequest.getCheckInDate(), standardRoomWithTypeRequest.getCheckOutDate()); List<String> fullKey = Lists.newArrayList(); List<UsedShareRoomCount> allUsedShareRoomCountList = Lists.newArrayList(); for (String key : fullKey) { List<UsedShareRoomCount> value = (List<UsedShareRoomCount>) usedShareRoomCountSlaveRedisTemplate.opsForValue().get(key); } if (CollectionUtils.isEmpty(allUsedShareRoomCountList)) { return Lists.newArrayList(); } return convert2RoomShareUsedCountEntityFromUsedShareCount(allUsedShareRoomCountList); }
@Autowired private SearchRepository searchRepository; public Optional<SearchData> find(String id) { return searchRepository.findById(id); }
Update Operation
private boolean invokeFrequencyValidate(String agentCode, Date checkInDate, Date checkOutDate) { int openingHotelNum = hotelService.getOpeningHotelNum(); Date currentDate = new Date(); // 16-90 if (AbstractDateUtils.isAfter(checkInDate, AbstractDateUtils.getDeltaDateByDays(currentDate, 15))) { Integer count = redisTemplate.opsForValue().get(agentCode + "_" + SIXTEEN_TO_NINETY); if (count == null) { redisTemplate.opsForValue().set(agentCode + "_" + SIXTEEN_TO_NINETY, 1, TIME_PERIOD_THREE, TimeUnit.MINUTES); return true; } if (count >= maxQueryBase + openingHotelNum / FIFTEEN_OUTER_DIVIDED) { return false; } redisTemplate.opsForValue().increment(agentCode + "_" + SIXTEEN_TO_NINETY, 1); return true; }
Delete Operation
public void delete(String key) { template.opsForValue().getOperations().delete(key); }
@Autowired private SearchRepository searchRepository; public Optional<SearchData> delete(String id) { return searchRepository.deleteById(id); }
Limitations
Unknown Redis connection/collection objects are created in case of unresolved names.
- No labels