SpringBoot 整合 Redis 的简单案例
Redis今天看了redis,只知道redis能做3件事:做缓存做非关系型数据库做消息中间件1).安装redis 在opt目录下,使用root用户cd /optmkdir rediswget http://download.redis.io/releases/redis-3.2.8.tar.gztar -zxvf redis-3.2.8.
Redis
今天看了redis, 只知道redis能做3件事:
做缓存
做非关系型数据库
做消息中间件
1).安装redis
在opt目录下,使用root用户
cd /opt mkdir redis wget http://download.redis.io/releases/redis-3.2.8.tar.gztar -zxvf redis-3.2.8.tar.gz cd redis-3.2.8/ make cd /opt chmod 777 -R redis12345678
2).测试安装redis是否成功
重新开一个终端
3).在原有项目中引入redis,先在pom.xml添加redis的依赖
<!-- 添加redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.4.5.RELEASE</version> </dependency>123456
4).在application.properties中添加redis配置信息
#redis spring.redis.hostName=127.0.0.1spring.redis.port=6379 spring.redis.pool.maxActive=8 spring.redis.pool.maxWait=-1 spring.redis.pool.maxIdle=8 spring.redis.pool.minIdle=0 spring.redis.timeout=0 12345678
5).新建一个config包,用来存放一些配置文件,新建RedisConfig.java
package com.hsp.config;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.RedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;@Configuration @EnableCaching//开启注解 public class RedisConfig extends CachingConfigurerSupport {
@Bean
public CacheManager cacheManager(RedisTemplate<?,?> redisTemplate) {
CacheManager cacheManager = new RedisCacheManager(redisTemplate); return cacheManager;
} @Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
redisTemplate.setConnectionFactory(factory); return redisTemplate;
}
}12345678910111213141516原创不易,完成人机校验,阅读全文
相关推荐
工作小总结Spring boot+redis 整合websocket实现消息实时推送案例REDIS简单的Redis工具类学习笔录-SPRING BOOT使用idea实现springboot Mybatis 整合(springboot 2.x)工作小总结很详细的SpringBoot整合UEditor教程学习笔录-SPRING BOOTspring 整合 redis,以及spring的RedisTemplate如何使用学习笔录-SPRING BOOTspring boot框架学习11-spring boot整合redis经验分享XStream 简单使用GIT项目【SpringBootFrame】springboot集成mybatis、redis、shiro、druid、通用mapper、分页插件、swagger框架