├── README.md
├── pom.xml
└── src
├── main
├── java
│ └── com
│ │ └── javatechie
│ │ └── redis
│ │ ├── SpringDataRedisExampleApplication.java
│ │ ├── config
│ │ └── RedisConfig.java
│ │ ├── entity
│ │ └── Product.java
│ │ └── respository
│ │ └── ProductDao.java
└── resources
│ └── application.properties
└── test
└── java
└── com
└── javatechie
└── redis
└── SpringDataRedisExampleApplicationTests.java
/README.md:
--------------------------------------------------------------------------------
1 | # spring-data-redis-cache
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.springframework.boot
7 | spring-boot-starter-parent
8 | 2.3.4.RELEASE
9 |
10 |
11 | com.javatechie
12 | spring-data-redis-example
13 | 0.0.1-SNAPSHOT
14 | spring-data-redis-example
15 | Demo project for Spring Boot
16 |
17 |
18 | 1.8
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter-data-redis
25 |
26 |
27 | org.springframework.boot
28 | spring-boot-starter-web
29 |
30 |
31 | redis.clients
32 | jedis
33 | 3.3.0
34 |
35 |
36 | org.projectlombok
37 | lombok
38 | true
39 |
40 |
41 | org.springframework.boot
42 | spring-boot-starter-test
43 | test
44 |
45 |
46 | org.junit.vintage
47 | junit-vintage-engine
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 | org.springframework.boot
57 | spring-boot-maven-plugin
58 |
59 |
60 |
61 |
62 |
63 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/redis/SpringDataRedisExampleApplication.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.redis;
2 |
3 | import com.javatechie.redis.entity.Product;
4 | import com.javatechie.redis.respository.ProductDao;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.boot.SpringApplication;
7 | import org.springframework.boot.autoconfigure.SpringBootApplication;
8 | import org.springframework.cache.annotation.CacheEvict;
9 | import org.springframework.cache.annotation.Cacheable;
10 | import org.springframework.cache.annotation.EnableCaching;
11 | import org.springframework.web.bind.annotation.*;
12 |
13 | import java.util.List;
14 |
15 | @SpringBootApplication
16 | @RestController
17 | @RequestMapping("/product")
18 | @EnableCaching
19 | public class SpringDataRedisExampleApplication {
20 |
21 | @Autowired
22 | private ProductDao dao;
23 |
24 | @PostMapping
25 | public Product save(@RequestBody Product product) {
26 | return dao.save(product);
27 | }
28 |
29 | @GetMapping
30 | public List getAllProducts() {
31 | return dao.findAll();
32 | }
33 |
34 | @GetMapping("/{id}")
35 | @Cacheable(key = "#id",value = "Product",unless = "#result.price > 1000")
36 | public Product findProduct(@PathVariable int id) {
37 | return dao.findProductById(id);
38 | }
39 |
40 | @DeleteMapping("/{id}")
41 | @CacheEvict(key = "#id",value = "Product")
42 | public String remove(@PathVariable int id) {
43 | return dao.deleteProduct(id);
44 | }
45 |
46 |
47 | public static void main(String[] args) {
48 | SpringApplication.run(SpringDataRedisExampleApplication.class, args);
49 | }
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/redis/config/RedisConfig.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.redis.config;
2 |
3 | import org.springframework.context.annotation.Bean;
4 | import org.springframework.context.annotation.Configuration;
5 | import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
6 | import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
7 | import org.springframework.data.redis.core.RedisTemplate;
8 | import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
9 | import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
10 | import org.springframework.data.redis.serializer.StringRedisSerializer;
11 |
12 | @Configuration
13 | @EnableRedisRepositories
14 | public class RedisConfig {
15 |
16 | @Bean
17 | public JedisConnectionFactory connectionFactory() {
18 | RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
19 | configuration.setHostName("localhost");
20 | configuration.setPort(6379);
21 | return new JedisConnectionFactory(configuration);
22 | }
23 |
24 | @Bean
25 | public RedisTemplate template() {
26 | RedisTemplate template = new RedisTemplate<>();
27 | template.setConnectionFactory(connectionFactory());
28 | template.setKeySerializer(new StringRedisSerializer());
29 | template.setHashKeySerializer(new StringRedisSerializer());
30 | template.setHashKeySerializer(new JdkSerializationRedisSerializer());
31 | template.setValueSerializer(new JdkSerializationRedisSerializer());
32 | template.setEnableTransactionSupport(true);
33 | template.afterPropertiesSet();
34 | return template;
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/redis/entity/Product.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.redis.entity;
2 |
3 | import lombok.AllArgsConstructor;
4 | import lombok.Data;
5 | import lombok.NoArgsConstructor;
6 | import org.springframework.data.annotation.Id;
7 | import org.springframework.data.redis.core.RedisHash;
8 |
9 | import java.io.Serializable;
10 |
11 | @Data
12 | @AllArgsConstructor
13 | @NoArgsConstructor
14 | @RedisHash("Product")
15 | public class Product implements Serializable {
16 | @Id
17 | private int id;
18 | private String name;
19 | private int qty;
20 | private long price;
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/javatechie/redis/respository/ProductDao.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.redis.respository;
2 |
3 | import com.javatechie.redis.entity.Product;
4 | import org.springframework.beans.factory.annotation.Autowired;
5 | import org.springframework.data.redis.core.RedisTemplate;
6 | import org.springframework.stereotype.Repository;
7 |
8 | import java.util.List;
9 |
10 | @Repository
11 | public class ProductDao {
12 |
13 | public static final String HASH_KEY = "Product";
14 |
15 | @Autowired
16 | private RedisTemplate template;
17 |
18 | public Product save(Product product){
19 | template.opsForHash().put(HASH_KEY,product.getId(),product);
20 | return product;
21 | }
22 |
23 | public List findAll(){
24 | return template.opsForHash().values(HASH_KEY);
25 | }
26 |
27 | public Product findProductById(int id){
28 | System.out.println("called findProductById() from DB");
29 | return (Product) template.opsForHash().get(HASH_KEY,id);
30 | }
31 |
32 |
33 | public String deleteProduct(int id){
34 | template.opsForHash().delete(HASH_KEY,id);
35 | return "product removed !!";
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | server.port=9292
2 |
--------------------------------------------------------------------------------
/src/test/java/com/javatechie/redis/SpringDataRedisExampleApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.javatechie.redis;
2 |
3 | import org.junit.jupiter.api.Test;
4 | import org.springframework.boot.test.context.SpringBootTest;
5 |
6 | @SpringBootTest
7 | class SpringDataRedisExampleApplicationTests {
8 |
9 | @Test
10 | void contextLoads() {
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------