This file contains hidden or 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
public class Post implements Serializable { | |
private String id; | |
private String title; | |
private String description; | |
private String image; | |
private int shares; | |
private Author author; | |
//getters setters and constructors | |
} |
This file contains hidden or 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
@CacheEvict(value = "post-single", key = "#id") | |
@DeleteMapping("/delete/{id}") | |
public void deletePostByID(@PathVariable String id) throws PostNotFoundException { | |
log.info("delete post with id {}", id); | |
postService.deletePost(id); | |
} | |
@CacheEvict(value = "post-top") | |
@GetMapping("/top/evict") | |
public void evictTopPosts() { |
This file contains hidden or 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
@CachePut(value = "post-single", key = "#post.id") | |
@PutMapping("/update") | |
public Post updatePostByID(@RequestBody Post post) throws PostNotFoundException { | |
log.info("update post with id {}", post.getId()); | |
postService.updatePost(post); | |
return post; | |
} |
This file contains hidden or 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
@Cacheable(value = "post-single", key = "#id", unless = "#result.shares < 500") | |
@GetMapping("/{id}") | |
public Post getPostByID(@PathVariable String id) throws PostNotFoundException { | |
log.info("get post with id {}", id); | |
return postService.getPostByID(id); | |
} | |
@Cacheable(value = "post-top") | |
@GetMapping("/top") | |
public List<Post> getTopPosts() { | |
return postService.getTopPosts(); |
This file contains hidden or 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
package com.example.model; | |
import java.io.Serializable; | |
public class Post implements Serializable { | |
private String id; | |
private String title; | |
private String description; | |
private String image; |
This file contains hidden or 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
#dump file name (up to you) | |
dbfilename cache-dump.rdb | |
#dump every 300 seconds if at least 1000 keys changed (up to you) | |
save 300 1000 | |
#memory limit up to 128mb (up to you) | |
maxmemory 128mb | |
#remove the less recently used (LRU) keys first | |
maxmemory-policy allkeys-lru | |
#LRU precision (up to you) | |
maxmemory-samples 10 |
This file contains hidden or 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
package com.example.configurations; | |
import org.springframework.cache.CacheManager; | |
import org.springframework.cache.annotation.CachingConfigurerSupport; | |
import org.springframework.cache.annotation.EnableCaching; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.data.redis.cache.RedisCacheManager; | |
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; | |
import org.springframework.data.redis.core.RedisTemplate; |