Skip to content

Commit

Permalink
updating halted transaction cache without affecting ttl
Browse files Browse the repository at this point in the history
Signed-off-by: Sachin Rana <[email protected]>
  • Loading branch information
sacrana0 committed Jan 29, 2025
1 parent 66548fa commit c3fcd22
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
12 changes: 12 additions & 0 deletions signup-service/src/main/java/io/mosip/signup/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
Expand Down Expand Up @@ -40,4 +43,13 @@ public Executor taskExecutor() {
public LockProvider lockProvider(RedisConnectionFactory redisConnectionFactory) {
return new RedisLockProvider(redisConnectionFactory);
}

@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new JdkSerializationRedisSerializer());
return template;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,20 @@
import net.javacrumbs.shedlock.core.LockAssert;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.ReturnType;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

import static io.mosip.signup.util.SignUpConstants.*;

Expand All @@ -47,6 +48,9 @@ public class CacheUtilService {
@Autowired
private RedisConnectionFactory redisConnectionFactory;

@Autowired
private RedisTemplate<String, Object> redisTemplate;


private static final String CLEANUP_SCRIPT = "local function binary_to_long(binary_str)\n" +
" local result = 0\n" +
Expand Down Expand Up @@ -227,7 +231,8 @@ public IdentityVerificationTransaction getSlotAllottedTransaction(String transac
return cacheManager.getCache(SignUpConstants.SLOT_ALLOTTED).get(transactionId, IdentityVerificationTransaction.class); //NOSONAR getCache() will not be returning null here.
}

public IdentityVerificationTransaction getVerifiedSlotTransaction(String slotId) {
public IdentityVerificationTransaction
getVerifiedSlotTransaction(String slotId) {
return cacheManager.getCache(SignUpConstants.VERIFIED_SLOT).get(slotId, IdentityVerificationTransaction.class); //NOSONAR getCache() will not be returning null here.
}

Expand All @@ -242,13 +247,13 @@ public void updateVerifiedSlotTransaction(String slotId, IdentityVerificationTra
}

public void updateVerificationStatus(String haltedTransactionId, String status, String errorCode) {
if(cacheManager.getCache(Constants.HALTED_CACHE) != null) {
OIDCTransaction oidcTransaction = cacheManager.getCache(Constants.HALTED_CACHE).get(haltedTransactionId, OIDCTransaction.class);
if(oidcTransaction != null) {
oidcTransaction.setVerificationStatus(status);
oidcTransaction.setVerificationErrorCode(errorCode);
cacheManager.getCache(Constants.HALTED_CACHE).put(haltedTransactionId, oidcTransaction);
}
String cacheKey = Constants.HALTED_CACHE + "::" + haltedTransactionId;
OIDCTransaction oidcTransaction = (OIDCTransaction) redisTemplate.opsForValue().get(cacheKey);
if(oidcTransaction != null) {
oidcTransaction.setVerificationStatus(status);
oidcTransaction.setVerificationErrorCode(errorCode);
long ttl = redisTemplate.getExpire(cacheKey);
redisTemplate.opsForValue().set(cacheKey, oidcTransaction, ttl, TimeUnit.SECONDS);
}
}

Expand Down

0 comments on commit c3fcd22

Please sign in to comment.