
spring boot对常用的数据库支持外,对nosql 数据库也进行了封装自动化。
redis介绍
Redis是目前业界使用最广泛的内存数据存储。相比memcached,Redis支持更丰富的数据结构,例如hashes, lists,
sets等,同时支持数据持久化。除此之外,Redis还提供一些类数据库的特性,比如事务,HA,主从库。可以说Redis兼具了缓存系统和数据库的一些特性,因此有着丰富的应用场景。本文介绍Redis在Spring
Boot中两个典型的应用场景。
如何使用
1、引入 spring-boot-starter-redis
<dependency>
<groupId>orgspringframeworkboot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
2、添加配置文件
# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
springredisdatabase=0
# Redis服务器地址
springredishost=192168058
# Redis服务器连接端口
springredisport=6379
# Redis服务器连接密码(默认为空)
springredispassword=
# 连接池最大连接数(使用负值表示没有限制)
springredispoolmax-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
springredispoolmax-wait=-1
# 连接池中的最大空闲连接
springredispoolmax-idle=8
# 连接池中的最小空闲连接
springredispoolmin-idle=0
# 连接超时时间(毫秒)
springredistimeout=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();
sbappend(targetgetClass()getName());
sbappend(methodgetName());
for (Object obj : params) {
sbappend(objtoString());
}
return sbtoString();
}
};
}
@SuppressWarnings("rawtypes")
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
//设置缓存过期时间
//rcmsetDefaultExpiration(60);//秒
return rcm;
}
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
StringRedisTemplate template = new StringRedisTemplate(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Objectclass);
ObjectMapper om = new ObjectMapper();
omsetVisibility(PropertyAccessorALL, JsonAutoDetectVisibilityANY);
omenableDefaultTyping(ObjectMapperDefaultTypingNON_FINAL);
jackson2JsonRedisSerializersetObjectMapper(om);
templatesetValueSerializer(jackson2JsonRedisSerializer);
templateafterPropertiesSet();
return template;
}
}
3、好了,接下来就可以直接使用了
@RunWith(SpringJUnit4ClassRunnerclass)
@SpringApplicationConfiguration(Applicationclass)
public class TestRedis {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate redisTemplate;
@Test
public void test() throws Exception {
stringRedisTemplateopsForValue()set("aaa", "111");
AssertassertEquals("111", stringRedisTemplateopsForValue()get("aaa"));
}
@Test
public void testObj() throws Exception {
User user=new User("aa@126com", "aa", "aa123456", "aa","123");
ValueOperations<String, User> operations=redisTemplateopsForValue();
operationsset("comneox", user);
operationsset("comneof", user,1,TimeUnitSECONDS);
Threadsleep(1000);
//redisTemplatedelete("comneof");
boolean exists=redisTemplatehasKey("comneof");
if(exists){
Systemoutprintln("exists is true");
}else{
Systemoutprintln("exists is false");
}
// AssertassertEquals("aa", operationsget("comneof")getUserName());
}
}
以上都是手动使用的方式,如何在查找数据库的时候自动使用缓存呢,看下面;
4、自动根据方法生成缓存
@RequestMapping("/getUser")
@Cacheable(value="user-key")
public User getUser() {
User user=userRepositoryfindByUserName("aa");
Systemoutprintln("若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功");
return user;
}
其中value的值就是缓存到redis中的key
@Test
public void queryCacheKey() throws Exception{
String key = "00000002";
String hashKey = "513117070";
//校验哈希key是否存在,存在为true,反之false
Boolean hasKey = redisTemplateopsForHash()hasKey(key, hashKey);
Systemoutprintln(hasKey);
if(hasKey){
//取值
Object object = redisTemplateopsForHash()get(key, hashKey);
Systemoutprintln("取出存储key-value:" + objecttoString());
}
}
char c = "test";
CFStringRef str = CFStringCreateWithCString(NULL, c, kCFStringEncodingASCII);
NSString test = [(NSString )str substringToIndex:2];
NSLog(test);
以上就是关于怎么看spring-bootspring-data-redis全部的内容,包括:怎么看spring-bootspring-data-redis、org.springframework.data.redis.core.RedisTemplate 查一个key的哈希怎么查、redistemplate 怎么清除redis里面的valueoperations中map的某个key值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)