英文:
Saving / Retrieving list or records in/ from REDIS Cache
问题
以下是翻译好的内容:
我有一些数据(如下所示),存储在一个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
我在主应用程序中将数据存储在REDIS缓存中。
代码:
@Override
public void saveClientId(EquityFeeds equityFeeds) {
System.out.println("在Redis缓存中保存ClientId:" + equityFeeds.getClientId());
hashOperations.put("CLIENTID", equityFeeds.getClientId(), equityFeeds);
}
@Override
public List<EquityFeeds> findByClientId(String clientId) {
System.out.println("在REDIS存储库中搜索Client Id:" + clientId);
return (List<EquityFeeds>) hashOperations.multiGet("CLIENTID", Arrays.asList(clientId));
}
现在我已经创建了一个REST Controller来通过REST API获取数据。
代码:
@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"));
}
return cId;
}
问题:
现在的问题是,我可能有多个具有相同clientId的行,例如,在上面显示的csv示例数据中,我有2个具有相同clientId 'AP' 的记录。我的要求是,当从浏览器调用REST API映射时,它应该显示具有给定clientId的多条记录。它应该显示所有的记录。
现在它只显示(单个)给定clientId的最后一条记录,这是可以理解的,因为按照哈希的概念,最后一条记录会覆盖之前的记录(因为哈希键 - 例如 'AP' 是相同的),并且只会显示一条记录,即最新的记录。
现在如何解决这个问题?可以使用hashOperations来解决吗?请提供解决上述要求的方法。
英文:
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.
答案1
得分: 1
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;
}
英文:
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论