redis使用

1.配置 
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>redis.clients</groupId>
                    <artifactId>jedis</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
 
2.配置application-dev.properties
 
 #####  redis配置
# Matser的ip地址
redis.hostName=127.0.0.1
# 端口号
redis.port=6379
# 如果有密码
redis.password=
# 客户端超时时间单位是毫秒 默认是2000
redis.timeout=3000
# 最大空闲数
redis.maxIdle=500
# 控制一个pool可分配多少个jedis实例,该配置适用本项目jedis版本
redis.maxTotal=1200
# 最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
redis.maxWaitMillis=3000
# 连接的最小空闲时间 默认1800000毫秒(30分钟)
redis.minEvictableIdleTimeMillis=10
# 每次释放连接的最大数目,默认3
redis.numTestsPerEvictionRun=100
# 逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
redis.timeBetweenEvictionRunsMillis=300
# 是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
redis.testOnBorrow=true
# 对jedis的连接实例进行,TestOnBorrow,在borrow一个jedis实例时,是否提前进行validate操作,如果为true,则得到的jedis实例均是可用的;TestOnReturn,在return一个jedis实例时,是否提前进行validate操作.
redis.testOnReturn=true
# 在空闲时检查有效性, 默认false
redis.testWhileIdle=true
# 数据失效时间
redis.dataTimeOut=900
# redis集群配置
#redis.cluster.nodes=192.168.182.128:6379,192.168.182.128:6380,192.168.182.128:6381,192.168.182.128:6382,192.168.182.128:6383,192.168.182.128:6384
#redis.cluster.max-redirects=5000
redis目录文件
3.测试
package com.test;

import com.cambodia.it.Application;
import com.cambodia.it.common.redis.RedisUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class, properties = {"spring.profiles.active=dev"}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class UtilTest {

private static Logger logger = LoggerFactory.getLogger(UtilTest.class);

@Autowired
private RedisUtil redisUtil;

@Test
public void stringbuffer(){
this.redisUtil.setHashData("AA123456789", "CC", "AAAA", 0);
System.out.println(this.redisUtil.getHashData("AA123456789", "CC", 0));
}
}
3

posted on 2019-06-04 17:52  狗狗汪汪叫  阅读(198)  评论(0编辑  收藏  举报

导航