Reputation: 305
I have a data (as per below) which is stored in a POJO (EquityFeeds):
externalTransactionId,clientId,transactionType,transactionDate,marketValue,sourceSystem,priorityFlag,processingFee
SAPEXTXN1,GS,BUY,23/11/13,101.9,BLO,Y,0
SAPEXTXN2,AS,SELL,20/11/13,121.9,BLO,N,0
SAPEXTXN3,AP,DEPOSIT,19/11/13,120,BLO,Y,0
SAPEXTXN4,HJ,WITHDRAW,30/11/13,230,BLO,N,0
SAPEXTXN5,GS,WITHDRAW,26/11/13,330,BLO,Y,0
SAPEXTXN6,AP,SELL,25/11/13,100,BLO,Y,0
SAPEXTXN7,AS,BUY,06/11/13,121.1,BLO,N,0
I am storing data in my main application in REDIS cache. Code:
@Override
public void saveClientId(EquityFeeds equityFeeds) {
System.out.println("Saving ClientId in RedisCache : "+equityFeeds.getClientId());
hashOperations.put("CLIENTID", equityFeeds.getClientId(), equityFeeds);
}
@Override
public List<EquityFeeds> findByClientId(String clientId) {
System.out.println("Client Id search in REDIS repository: "+clientId);
return (List<EquityFeeds>) hashOperations.multiGet("CLIENTID", Arrays.asList(clientId));
}
Now I have made a REST Controller to get the data via REST API Code:
@GetMapping("/getByClientId/{clientId}")
public Optional<List<EquityFeeds>> getByClientId(@PathVariable("clientId") final String clientId) {
Optional<List<EquityFeeds>> cId = Optional.ofNullable(equityFeedsRedisRepositoryImpl.findByClientId(clientId));
if(cId.isPresent()) {
return cId;
} else {
cId.orElseThrow(() -> new RuntimeException("Client Id not found"));
}
return cId;
}
Issue: Now the issue is that I can have multiple rows with the same clientId for e.g. in the sample data from csv shown above I am having 2 records with the same clientId 'AP'. My requirement is that it should show multiple records with the given clientId when the REST API mapping is called from the browser. It should show all the records which are there.
Right now it is showing ONLY (single) last record for the given clientId which is understandable since by the concept of hash the last record would overwrite the previous record (since the hashKey - for e.g. 'AP' is same) and it would show one and ONLY one record which would the latest one.
Now how to solve this issue? Can it be solved using the hashOperations? Kindly suggest a way to solve the above requirement.
Upvotes: 1
Views: 1000
Reputation: 305
I solved the above issue using RedisList. Below is the code:
private ListOperations listOperations;
public EquityFeedsRedisRepositoryImpl(RedisTemplate<String, EquityFeeds> redisTemplate) {
this.redisTemplate = redisTemplate;
this.hashOperations = redisTemplate.opsForHash();
this.listOperations = redisTemplate.opsForList();
}
@Override
public void saveClientId(EquityFeeds equityFeeds) {
System.out.println("Saving ClientId in RedisCache : "+equityFeeds.getClientId());
listOperations.rightPush(equityFeeds.getClientId(), equityFeeds);
}
@Override
public List<EquityFeeds> findByClientId(String clientId) {
System.out.println("Client Id search in REDIS repository: "+clientId);
return (List<EquityFeeds>) listOperations.range(clientId, 0, -1);
}
REST Controller to get data via REST API Code.
@GetMapping("/getByClientId/{clientId}")
public Optional<List<EquityFeeds>> getByClientId(@PathVariable("clientId") final String clientId) {
Optional<List<EquityFeeds>> cId = Optional.ofNullable(equityFeedsRedisRepositoryImpl.findByClientId(clientId));
if(cId.isPresent()) {
return cId;
} else {
cId.orElseThrow(() -> new RuntimeException("Client Id not found"));
}
return cId;
}
Upvotes: 1