学习笔录-SPRING BOOT

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

原创不易,完成人机校验,阅读全文

相关推荐