Skip to content

Instantly share code, notes, and snippets.

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;
@matthewfrank
matthewfrank / redis.conf
Created March 31, 2017 17:34
Simple redis configuration for cacheing
#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
@matthewfrank
matthewfrank / Post.java
Created March 31, 2017 17:37
Post model class
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;
@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();
@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;
}
@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() {
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
}