redis(3)

来源:互联网 发布:网络版权保护 编辑:程序博客网 时间:2024/06/06 17:48

在springboot项目中,使用redis

首先在maven中添加依赖

    <dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-redis</artifactId>    <version>1.3.1.RELEASE</version>    </dependency>

添加配置文件

    # REDIS (RedisProperties)    # Redis数据库索引(默认为0)    spring.redis.database=0      # Redis服务器地址    spring.redis.host=127.0.0.1    # 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  

然后在我们需要用缓存的地方,使用注解的方式引入

    //string类型    @Autowired    private StringRedisTemplate stringRedisTemplate;    //对象类型    @Autowired    private RedisTemplate redisTemplate;

在代码中

    redisTemplate.opsForValue().set(key,value),    redisTemplate.opsForValue().get(key,value),

根据实际业务逻辑,添加缓存,获取缓存,




原创粉丝点击