整合 Spring Boot 2.x + Redis
所需服务
本地启动Redis
端口号使用默认端口号6379
Spring Data Redis 单机无需配置,即可使用!
所需依赖
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
|
直接开始使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| import lombok.AllArgsConstructor; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource; import java.io.Serializable;
@Slf4j @RunWith(SpringRunner.class) @SpringBootTest public class RedisTest { @Resource private RedisTemplate<String, Object> redisTemplate;
@Test public void test() { String key = "user:1"; redisTemplate.opsForValue().set(key, new User(1, "Josway", 18)); Object o = redisTemplate.opsForValue().get(key); log.info("user:" + o.toString());
} }
@Data @AllArgsConstructor class User implements Serializable { private static final long serialVersionUID = -6680135988818656280L; private Integer id; private String name; private Integer age; }
|
Spring Cache 单机无需配置,即可使用!
所需依赖
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
|
开始使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication @EnableCaching public class DroolsApplication { public static void main(String[] args) { SpringApplication.run(DroolsApplication.class, args); } }
|
@Cacheable
@CachePut
@CacheEvict 可以自动缓存。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| import com.example.visualization.entity.Demo; import com.example.visualization.mapper.DemoMapper; import com.example.visualization.service.DemoService; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@CacheConfig(cacheNames = "demo") @Service public class DemoServiceImpl implements DemoService {
@Resource private DemoMapper demoMapper;
@Override public int deleteByPrimaryKey(Integer id) { return demoMapper.deleteByPrimaryKey(id); }
@Override public int insert(Demo record) { return demoMapper.insert(record); }
@Override public int insertSelective(Demo record) { return demoMapper.insertSelective(record); }
@Cacheable @Override public Demo selectByPrimaryKey(Integer id) { return demoMapper.selectByPrimaryKey(id); }
@CachePut(key = "#record.id") @Override public int updateByPrimaryKeySelective(Demo record) { return demoMapper.updateByPrimaryKeySelective(record); }
@CacheEvict @Override public int updateByPrimaryKey(Demo record) { return demoMapper.updateByPrimaryKey(record); }
}
|