依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

 

application.yml

spring:
    redis:
# 指的是默认操作redis数据库中的db1 database:
0 host: 127.0.0.1 port: 6379 password: 123456 timeout: 0 pool: max-active: 8 max-idle: 8 max-wait: -1 min-idle: 0

 

测试代码

package com.vast;

import com.vast.dao.AccountRepository;
import com.vast.dao.IAccountMybatisDao;
import com.vast.entity.Account;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.concurrent.TimeUnit;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ApplicationVastStart.class)
public class TestAccountService {

    @Autowired
    private IAccountMybatisDao accountMybatisDao;
    @Autowired
    private AccountRepository accountRepository;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public void TestSaveAccount(){
        Account account = new Account();
        account.setName("222");
        account.setMoney(23.9);
////        accountMybatisDao.saveAccount(account);
//        accountRepository.findByName("");
//        System.out.println(accountRepository.findAll());
//        System.out.println(accountRepository.insert(account));

        // 测试Redis
        ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue();
        stringStringValueOperations.set("name","张三", 1, TimeUnit.MINUTES);//1分钟过期
        //JedisConnectionFactory jedisConnectionFactory = (JedisConnectionFactory) stringRedisTemplate.getConnectionFactory();
// 切换数据库为db1 //jedisConnectionFactory.setDatabase(1); //stringRedisTemplate.setConnectionFactory(jedisConnectionFactory);

System.out.println(stringStringValueOperations.get("name")); } }

 

扩展

Windows中,把redis-server.exe注册成服务命令

redis-server.exe --service-install redis.windows.conf

 

另一种redis连接方式

public class RedisUtil {

    //服务器IP地址
    private static String ADDR = "192.168.41.65";
    //端口
    private static int PORT = 6379;
    //密码
    private static String AUTH = "123456";
    //连接实例的最大连接数
    private static int MAX_ACTIVE = 1024;
    //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
    private static int MAX_IDLE = 200;
    //等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException
    private static int MAX_WAIT = 10000;
    //连接超时的时间  
    private static int TIMEOUT = 10000;
    // 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
    private static boolean TEST_ON_BORROW = true;

    private static JedisPool jedisPool = null;
    //数据库模式是16个数据库 0~15 
    public static final int DEFAULT_DATABASE = 0;
    /**
     * 初始化Redis连接池
     */

    static {

        try {

            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(MAX_ACTIVE);
            config.setMaxIdle(MAX_IDLE);
            config.setMaxWaitMillis(MAX_WAIT);
            config.setTestOnBorrow(TEST_ON_BORROW);
            jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT,AUTH,DEFAULT_DATABASE);

        } catch (Exception e) {

            e.printStackTrace();
        }

    }

    /**
     * 获取Jedis实例
     */

    public synchronized static Jedis getJedis() {

        try {

            if (jedisPool != null) {
                Jedis resource = jedisPool.getResource();
                System.out.println("redis--服务正在运行: "+resource.ping());
                return resource;
            } else {
                return null;
            }

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /***
     * 
     * 释放资源
     */
    
    public static void returnResource(final Jedis jedis) {
            if(jedis != null) {
                jedisPool.returnResource(jedis);
            }
        
    }
}

 

SpringBoot30 整合Mybatis-Plus、整合Redis、利用Ehcache实现二级缓存、利用SpringCache和Redis作为缓存

 

posted on 2019-12-09 16:58  风又奈何  阅读(391)  评论(0编辑  收藏  举报