-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: RedisService and Cache IssueService
- Loading branch information
Showing
5 changed files
with
179 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
..._backend/src/main/java/com/thutasann/project_management_backend/config/JacksonConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.thutasann.project_management_backend.config; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer; | ||
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
@Configuration | ||
public class JacksonConfig { | ||
|
||
// Define the date format to use for LocalDate serialization/deserialization | ||
private static final String DATE_FORMAT = "yyyy-MM-dd"; | ||
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern(DATE_FORMAT); | ||
|
||
@Bean | ||
public ObjectMapper objectMapper() { | ||
// Create an ObjectMapper instance | ||
ObjectMapper mapper = new ObjectMapper(); | ||
|
||
// Register the JavaTimeModule for handling Java 8 date/time types | ||
JavaTimeModule module = new JavaTimeModule(); | ||
|
||
// Customize LocalDate serialization/deserialization | ||
module.addSerializer(LocalDate.class, new LocalDateSerializer(DATE_FORMATTER)); | ||
module.addDeserializer(LocalDate.class, new LocalDateDeserializer(DATE_FORMATTER)); | ||
|
||
mapper.registerModule(module); | ||
|
||
return mapper; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...ackend/src/main/java/com/thutasann/project_management_backend/utilities/RedisService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package com.thutasann.project_management_backend.utilities; | ||
|
||
import java.time.Duration; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.stereotype.Service; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
|
||
/** | ||
* Redis Service | ||
*/ | ||
@Service | ||
public class RedisService { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(RedisService.class); | ||
|
||
@Autowired | ||
private RedisTemplate<String, Object> redisTemplate; | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
public RedisService(RedisTemplate<String, String> redisTemplate) { | ||
this.objectMapper = new ObjectMapper().registerModule(new JavaTimeModule()); | ||
} | ||
|
||
/** | ||
* Cache data with a specific key and TTL. | ||
* | ||
* @param key The Redis key | ||
* @param data The data to cache | ||
* @param ttl The time-to-live in seconds | ||
*/ | ||
public void cacheData(String key, Object data, long ttl) { | ||
try { | ||
String jsonData = objectMapper.writeValueAsString(data); | ||
redisTemplate.opsForValue().set(key, jsonData, Duration.ofSeconds(ttl)); | ||
logger.info("Data cached with key: {}", key); | ||
} catch (JsonProcessingException e) { | ||
logger.error("Error serializing data for caching with key: {}", key, e); | ||
} | ||
} | ||
|
||
/** | ||
* Retrieve cached data by key. | ||
* | ||
* @param key The Redis key | ||
* @return The cached object or null if not found or deserialization fails | ||
*/ | ||
public <T> T getCachedData(String key, Class<T> type) { | ||
try { | ||
String jsonData = (String) redisTemplate.opsForValue().get(key); | ||
if (jsonData != null) { | ||
logger.info("Cache hit for key: {}", key); | ||
return objectMapper.readValue(jsonData, type); // Type-safe deserialization | ||
} | ||
} catch (JsonProcessingException e) { | ||
logger.error("Error deserializing cached data for key: {}", key, e); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Evict a specific cache by key. | ||
* | ||
* @param key The Redis key | ||
*/ | ||
public void evictCache(String key) { | ||
redisTemplate.delete(key); | ||
logger.info("Cache evicted for key: {}", key); | ||
} | ||
|
||
/** | ||
* Delete Cached Data | ||
* | ||
* @param key - Key | ||
*/ | ||
public void deleteCachedData(String key) { | ||
redisTemplate.delete(key); | ||
} | ||
|
||
/** | ||
* Increment Key | ||
* | ||
* @param key - Key | ||
* @param delta - Delta | ||
* @return Increment key | ||
*/ | ||
public Long incrementKey(String key, long delta) { | ||
return redisTemplate.opsForValue().increment(key, delta); | ||
} | ||
|
||
/** | ||
* Set with Conditional Check | ||
* | ||
* @param key - Key | ||
* @param value - Value | ||
* @param timeoutInSeconds - timeoutSeconds | ||
* @return Boolean Value | ||
*/ | ||
@SuppressWarnings("null") | ||
public boolean setIfAbsent(String key, Object value, long timeoutInSeconds) { | ||
return redisTemplate.opsForValue().setIfAbsent(key, value, Duration.ofSeconds(timeoutInSeconds)); | ||
} | ||
} |