-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
musab.bozkurt
committed
Feb 4, 2024
1 parent
d83e743
commit 4322ea0
Showing
17 changed files
with
462 additions
and
4 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
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
25 changes: 25 additions & 0 deletions
25
src/main/java/com/mb/livedataservice/api/controller/RedisController.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,25 @@ | ||
package com.mb.livedataservice.api.controller; | ||
|
||
import com.mb.livedataservice.data.entity.RedisHashData; | ||
import com.mb.livedataservice.service.RedisHashService; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Slf4j | ||
@RestController | ||
@RequiredArgsConstructor | ||
public class RedisController { | ||
|
||
private final RedisHashService redisHashService; | ||
|
||
/** | ||
* Create RedisHashData | ||
*/ | ||
@PostMapping("/redis-hash") | ||
public RedisHashData createRedisHashData() { | ||
log.info("Received a request to create RedisHashData. createRedisHashData."); | ||
return redisHashService.save(RedisHashData.builder().destination("hello_world").build()); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/com/mb/livedataservice/config/CacheConfig.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,61 @@ | ||
package com.mb.livedataservice.config; | ||
|
||
import com.mb.livedataservice.utils.RedisConstants; | ||
import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; | ||
import org.springframework.cache.annotation.EnableCaching; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.cache.RedisCacheConfiguration; | ||
import org.springframework.data.redis.cache.RedisCacheManager; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.core.RedisKeyValueAdapter; | ||
import org.springframework.data.redis.core.RedisOperations; | ||
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; | ||
import org.springframework.session.data.redis.config.ConfigureRedisAction; | ||
|
||
import java.time.Duration; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Configuration | ||
@EnableCaching | ||
@AutoConfigureAfter(RedisAutoConfiguration.class) | ||
@ConditionalOnClass({RedisOperations.class, RedisConnectionFactory.class, RedisCacheConfiguration.class}) | ||
@EnableRedisRepositories(enableKeyspaceEvents = RedisKeyValueAdapter.EnableKeyspaceEvents.ON_STARTUP) | ||
public class CacheConfig { | ||
|
||
@Bean(name = "cacheManager") | ||
@ConditionalOnMissingBean(name = "cacheManager") | ||
public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) { | ||
RedisCacheConfiguration expireIn1Day = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofDays(1)); | ||
|
||
Map<String, RedisCacheConfiguration> cacheConfigurations = new HashMap<>(); | ||
|
||
cacheConfigurations.put(RedisConstants.CACHE_KEY, expireIn1Day); | ||
|
||
return RedisCacheManager.RedisCacheManagerBuilder | ||
.fromConnectionFactory(connectionFactory) | ||
.withInitialCacheConfigurations(cacheConfigurations) | ||
.build(); | ||
} | ||
|
||
/* | ||
* If the Redis client is protected, add this config bean. Otherwise, this bean can be removed. | ||
* | ||
* However, if you can run any commands in redis, please run the following command to enable org.springframework.data.redis.core.RedisKeyExpiredEvent | ||
* command -> redis-cli config set notify-keyspace-events xE | ||
* | ||
* notify-keyspace-events should be xE. | ||
* To get the value of notify-keyspace-events run this -> config get "notify-keyspace-events" | ||
* | ||
* This means that Spring Session cannot configure Redis Keyspace events for you. | ||
* To disable the automatic configuration add ConfigureRedisAction.NO_OP as a bean. | ||
* */ | ||
@Bean | ||
public static ConfigureRedisAction configureRedisAction() { | ||
return ConfigureRedisAction.NO_OP; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/mb/livedataservice/config/RedissonConfig.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,22 @@ | ||
package com.mb.livedataservice.config; | ||
|
||
import org.redisson.Redisson; | ||
import org.redisson.api.RedissonClient; | ||
import org.redisson.config.Config; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class RedissonConfig { | ||
|
||
@Bean | ||
@ConditionalOnProperty(value = "redisson.enabled", havingValue = "true") | ||
public RedissonClient redissonClient(@Value("${redisson.url}") String address) { | ||
Config config = new Config(); | ||
config.useSingleServer().setAddress(address); | ||
|
||
return Redisson.create(config); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/mb/livedataservice/data/entity/RedisHashData.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,43 @@ | ||
package com.mb.livedataservice.data.entity; | ||
|
||
import jakarta.persistence.Id; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.data.redis.core.RedisHash; | ||
import org.springframework.data.redis.core.TimeToLive; | ||
import org.springframework.data.redis.core.index.Indexed; | ||
|
||
import java.util.UUID; | ||
|
||
@Data | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@RedisHash(value = "RedisHashData") | ||
public class RedisHashData { | ||
|
||
@Id | ||
@Builder.Default | ||
private String id = UUID.randomUUID().toString(); | ||
|
||
@Indexed | ||
private String redisHashCode; | ||
|
||
@Indexed | ||
private String reference; | ||
|
||
private String destination; | ||
|
||
private int count; | ||
|
||
@TimeToLive | ||
@Builder.Default | ||
private long expiration = 60L; | ||
|
||
public RedisHashData(String redisHashCode, String reference) { | ||
this.redisHashCode = redisHashCode; | ||
this.reference = reference; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/mb/livedataservice/data/repository/RedisHashDataRepository.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,8 @@ | ||
package com.mb.livedataservice.data.repository; | ||
|
||
import com.mb.livedataservice.data.entity.RedisHashData; | ||
import org.springframework.data.repository.CrudRepository; | ||
|
||
public interface RedisHashDataRepository extends CrudRepository<RedisHashData, String> { | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/mb/livedataservice/queue/RedisKeyExpiredEventListenerImpl.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,23 @@ | ||
package com.mb.livedataservice.queue; | ||
|
||
import com.mb.livedataservice.data.entity.RedisHashData; | ||
import com.mb.livedataservice.service.RedisHashService; | ||
import lombok.AllArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.data.redis.core.RedisKeyExpiredEvent; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Component | ||
@AllArgsConstructor | ||
public class RedisKeyExpiredEventListenerImpl { | ||
|
||
private final RedisHashService redisHashService; | ||
|
||
@EventListener(condition = "#event.keyspace == 'RedisHashData'") | ||
public void redisExpiredKeyEventForRedisHashData(RedisKeyExpiredEvent<?> event) { | ||
log.info("Redis key expired event log. RedisHashData - event:{}", event.toString()); | ||
redisHashService.delete(RedisHashData.builder().id(new String(event.getId())).build()); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/mb/livedataservice/service/RedisHashService.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,16 @@ | ||
package com.mb.livedataservice.service; | ||
|
||
import com.mb.livedataservice.data.entity.RedisHashData; | ||
|
||
import java.util.Optional; | ||
|
||
public interface RedisHashService { | ||
|
||
RedisHashData save(RedisHashData redisHashData); | ||
|
||
Optional<RedisHashData> findById(String id); | ||
|
||
void delete(RedisHashData redisHashData); | ||
|
||
void deleteRedisHashDataById(String redisHashDataId); | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/mb/livedataservice/service/RedisTokenStoreService.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,10 @@ | ||
package com.mb.livedataservice.service; | ||
|
||
public interface RedisTokenStoreService { | ||
|
||
String getToken(String tokenId, String key); | ||
|
||
void storeToken(String tokenId, String key); | ||
|
||
void deleteToken(String tokenId, String key); | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/mb/livedataservice/service/impl/RedisHashServiceImpl.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,39 @@ | ||
package com.mb.livedataservice.service.impl; | ||
|
||
import com.mb.livedataservice.data.entity.RedisHashData; | ||
import com.mb.livedataservice.data.repository.RedisHashDataRepository; | ||
import com.mb.livedataservice.service.RedisHashService; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Optional; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class RedisHashServiceImpl implements RedisHashService { | ||
|
||
private final RedisHashDataRepository redisHashDataRepository; | ||
|
||
@Override | ||
public RedisHashData save(RedisHashData redisHashData) { | ||
return redisHashDataRepository.save(redisHashData); | ||
} | ||
|
||
@Override | ||
public Optional<RedisHashData> findById(String id) { | ||
return redisHashDataRepository.findById(id); | ||
} | ||
|
||
@Override | ||
public void delete(RedisHashData redisHashData) { | ||
redisHashDataRepository.delete(redisHashData); | ||
} | ||
|
||
@Override | ||
public void deleteRedisHashDataById(String redisHashDataId) { | ||
log.info("Deleting RedisHashData by ID: '{}'.", redisHashDataId); | ||
redisHashDataRepository.deleteById(redisHashDataId); | ||
} | ||
} |
Oops, something went wrong.