diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingCacheRepository.java b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingCacheRepository.java index a807a61862..d9f3a3584e 100644 --- a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingCacheRepository.java +++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingCacheRepository.java @@ -1,13 +1,118 @@ package com.baeldung.spring.cloud.bootstrap.svcrating.rating; +import java.io.IOException; import java.util.List; +import java.util.stream.Collectors; -public interface RatingCacheRepository { +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; +import org.springframework.data.redis.core.SetOperations; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.data.redis.core.ValueOperations; - List findCachedRatingsByBookId(Long bookId); - Rating findCachedRatingById(Long ratingId); - List findAllCachedRatings(); - boolean createRating(Rating persisted); - boolean updateRating(Rating persisted); - boolean deleteRating(Long ratingId); -} +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.stereotype.Repository; + +@Repository +public class RatingCacheRepository implements InitializingBean { + + @Autowired + private JedisConnectionFactory cacheConnectionFactory; + + private StringRedisTemplate redisTemplate; + private ValueOperations valueOps; + private SetOperations setOps; + + private ObjectMapper jsonMapper; + + public List findCachedRatingsByBookId(Long bookId) { + return setOps.members("book-" + bookId) + .stream() + .map(rtId -> { + try { + return jsonMapper.readValue(valueOps.get(rtId), Rating.class); + } catch (IOException ex) { + return null; + } + }) + .collect(Collectors.toList()); + } + + public Rating findCachedRatingById(Long ratingId) { + + try { + return jsonMapper.readValue(valueOps.get("rating-" + ratingId), Rating.class); + } catch (IOException e) { + return null; + } + + } + + public List findAllCachedRatings() { + List ratings = null; + + ratings = redisTemplate.keys("rating*") + .stream() + .map(rtId -> { + try { + + return jsonMapper.readValue(valueOps.get(rtId), Rating.class); + + } catch (IOException e) { + return null; + } + }) + .collect(Collectors.toList()); + + return ratings; + } + + public boolean createRating(Rating persisted) { + try { + valueOps.set("rating-" + persisted.getId(), jsonMapper.writeValueAsString(persisted)); + setOps.add("book-" + persisted.getBookId(), "rating-" + persisted.getId()); + return true; + } catch (JsonProcessingException ex) { + return false; + } + } + + public boolean updateRating(Rating persisted) { + try { + valueOps.set("rating-" + persisted.getId(), jsonMapper.writeValueAsString(persisted)); + return true; + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + return false; + } + + public boolean deleteRating(Long ratingId) { + Rating toDel; + try { + + toDel = jsonMapper.readValue(valueOps.get("rating-" + ratingId), Rating.class); + setOps.remove("book-" + toDel.getBookId(), "rating-" + ratingId); + redisTemplate.delete("rating-" + ratingId); + return true; + } catch (IOException e) { + e.printStackTrace(); + } + return false; + } + + @Override + public void afterPropertiesSet() throws Exception { + + this.redisTemplate = new StringRedisTemplate(cacheConnectionFactory); + + this.valueOps = redisTemplate.opsForValue(); + this.setOps = redisTemplate.opsForSet(); + + jsonMapper = new ObjectMapper(); + jsonMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingCacheRepositoryImpl.java b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingCacheRepositoryImpl.java deleted file mode 100644 index 35eddefade..0000000000 --- a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingCacheRepositoryImpl.java +++ /dev/null @@ -1,120 +0,0 @@ -package com.baeldung.spring.cloud.bootstrap.svcrating.rating; - -import java.io.IOException; -import java.util.List; -import java.util.stream.Collectors; - -import org.springframework.beans.factory.InitializingBean; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; -import org.springframework.data.redis.core.SetOperations; -import org.springframework.data.redis.core.StringRedisTemplate; -import org.springframework.data.redis.core.ValueOperations; -import org.springframework.stereotype.Repository; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.ObjectMapper; - -@Repository -public class RatingCacheRepositoryImpl implements InitializingBean, RatingCacheRepository { - - @Autowired - private JedisConnectionFactory cacheConnectionFactory; - - private StringRedisTemplate redisTemplate; - private ValueOperations valueOps; - private SetOperations setOps; - - private ObjectMapper jsonMapper; - - public List findCachedRatingsByBookId(Long bookId) { - return setOps.members("book-" + bookId) - .stream() - .map(rtId -> { - try { - Rating rt = jsonMapper.readValue(valueOps.get(rtId), Rating.class); - rt.setFromCache(true); - return rt; - } catch (IOException ex) { - return null; - } - }) - .collect(Collectors.toList()); - } - - public Rating findCachedRatingById(Long ratingId) { - - try { - return jsonMapper.readValue(valueOps.get("rating-" + ratingId), Rating.class); - } catch (IOException e) { - return null; - } - - } - - public List findAllCachedRatings() { - List ratings = null; - - ratings = redisTemplate.keys("rating*") - .stream() - .map(rtId -> { - try { - - return jsonMapper.readValue(valueOps.get(rtId), Rating.class); - - } catch (IOException e) { - return null; - } - }) - .collect(Collectors.toList()); - - return ratings; - } - - public boolean createRating(Rating persisted) { - try { - valueOps.set("rating-" + persisted.getId(), jsonMapper.writeValueAsString(persisted)); - setOps.add("book-" + persisted.getBookId(), "rating-" + persisted.getId()); - return true; - } catch (JsonProcessingException ex) { - return false; - } - } - - public boolean updateRating(Rating persisted) { - try { - valueOps.set("rating-" + persisted.getId(), jsonMapper.writeValueAsString(persisted)); - return true; - } catch (JsonProcessingException e) { - e.printStackTrace(); - } - return false; - } - - public boolean deleteRating(Long ratingId) { - Rating toDel; - try { - - toDel = jsonMapper.readValue(valueOps.get("rating-" + ratingId), Rating.class); - setOps.remove("book-" + toDel.getBookId(), "rating-" + ratingId); - redisTemplate.delete("rating-" + ratingId); - return true; - } catch (IOException e) { - e.printStackTrace(); - } - return false; - } - - @Override - public void afterPropertiesSet() throws Exception { - - this.redisTemplate = new StringRedisTemplate(cacheConnectionFactory); - - this.valueOps = redisTemplate.opsForValue(); - this.setOps = redisTemplate.opsForSet(); - - jsonMapper = new ObjectMapper(); - jsonMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - } -}