SpringBoot对常用的数据库支持外,对NoSQL 数据库也举办了封装自动化。
redis先容
Redis是今朝业界利用最遍及的内存数据存储。对比memcached,Redis支持更富厚的数据布局,譬喻hashes, lists, sets等,同时支持数据耐久化。除此之外,Redis还提供一些类数据库的特性,好比事务,HA,主从库。可以说Redis兼具了缓存系统和数据库的一些特性,图纸加密,因此有着富厚的应用场景。本文先容Redis在Spring Boot中两个典范的应用场景。
如何利用
1、引入 spring-boot-starter-redis
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
2、添加设置文件
# REDIS (RedisProperties) # Redis数据库索引(默认为0) spring.redis.database=0 # Redis处事器地点 spring.redis.host=192.168.0.58 # Redis处事器毗连端口 spring.redis.port=6379 # Redis处事器毗连暗码(默认为空) spring.redis.password= # 毗连池最大毗连数(利用负值暗示没有限制) spring.redis.pool.max-active=8 # 毗连池最大阻塞期待时间(利用负值暗示没有限制) spring.redis.pool.max-wait=-1 # 毗连池中的最大空闲毗连 spring.redis.pool.max-idle=8 # 毗连池中的最小空闲毗连 spring.redis.pool.min-idle=0 # 毗连超时时间(毫秒) spring.redis.timeout=0
3、添加cache的设置类
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport{
@Bean
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : params) {
sb.append(obj.toString());
}
return sb.toString();
}
};
}
@SuppressWarnings("rawtypes")
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
//配置缓存逾期时间
//rcm.setDefaultExpiration(60);//秒
return rcm;
}
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
StringRedisTemplate template = new StringRedisTemplate(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
4、好了,接下来就可以直接利用了
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
public class TestRedis {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate redisTemplate;
@Test
public void test() throws Exception {
stringRedisTemplate.opsForValue().set("aaa", "111");
Assert.assertEquals("111", stringRedisTemplate.opsForValue().get("aaa"));
}
@Test
public void testObj() throws Exception {
User user=new User("aa@126.com", "aa", "aa123456", "aa","123");
ValueOperations<String, User> operations=redisTemplate.opsForValue();
operations.set("com.neox", user);
operations.set("com.neo.f", user,1,TimeUnit.SECONDS);
Thread.sleep(1000);
//redisTemplate.delete("com.neo.f");
boolean exists=redisTemplate.hasKey("com.neo.f");
if(exists){
System.out.println("exists is true");
}else{
System.out.println("exists is false");
}
// Assert.assertEquals("aa", operations.get("com.neo.f").getUserName());
}
}
以上都是手动利用的方法,如安在查找数据库的时候自动利用缓存呢,看下面;
5、自动按照要领生成缓存
@RequestMapping("/getUser")
@Cacheable(value="user-key")
public User getUser() {
User user=userRepository.findByUserName("aa");
System.out.println("若下面没呈现“无缓存的时候挪用”字样且能打印出数据暗示测试乐成");
return user;
}
个中value的值就是缓存到redis中的key
共享Session-spring-session-data-redis
漫衍式系统中,sessiong共享有许多的办理方案,劳务派遣管理系统,个中托管到缓存中应该是最常用的方案之一,
Spring Session官方说明
Spring Session provides an API and implementations for managing a user’s session information.
如何利用