自学内容网 自学内容网

SpringBoot:泛型对象存取与转换<JedisPool>

操作redis时候泛型、对象操作的比较频繁不可能都转成字符串然后再查询转换比较复杂了,这个时候我们需要对存取转换进行封装便于快速开发

1、导入依赖

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.8.1</version>
</dependency>

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.7.2.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.4.2</version>
</dependency>

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.47</version>
</dependency>

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.2</version>
    <scope>test</scope>
</dependency>

2、Redis服务通用类

@Service
public class RedisService {

@Autowired
JedisPool jedisPool;

/**
 * 设置对象
 * @param key 键
 * @param value 值
 * @param exTime 过期时间
 * @param <T> 返回类型
 * @return
 */
public <T> boolean set(String key,T value,int exTime) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String str = beanToString(value);
if (str == null || str.length() <= 0) {
return false;
}

            //设置过期时间
if(exTime <= 0) {
jedis.set(key, str);
} else {
jedis.setex(key, exTime, str);
}

return true;
} finally {
returnToPool(jedis);
}
}

/**
 * 根据key获取对象返回
 * @param key
 * @param clazz
 * @param <T>
 * @return
 */
public <T> T get(String key,Class<T> clazz) {
Jedis jedis = null;

try {
jedis = jedisPool.getResource();

String str = jedis.get(key);
T t = stringToBean(str,clazz);
return t;
}finally {
returnToPool(jedis);
}

}

/**
 * bean 转 String
 * @param value
 * @param <T>
 * @return
 */
public static <T> String beanToString(T value) {
if(value == null) {
return null;
}
Class<?> clazz = value.getClass();
if(clazz == int.class || clazz == Integer.class) {
return ""+value;
}else if(clazz == String.class) {
return (String)value;
}else if(clazz == long.class || clazz == Long.class) {
return ""+value;
}else {
return JSON.toJSONString(value);
}
}


/**
 * string转bean
 * @param str
 * @param clazz
 * @param <T>
 * @return
 */
public static <T> T stringToBean(String str, Class<T> clazz) {
if(str == null || str.length() <= 0 || clazz == null) {
return null;
}
if(clazz == int.class || clazz == Integer.class) {
return (T)Integer.valueOf(str);
}else if(clazz == String.class) {
return (T)str;
}else if(clazz == long.class || clazz == Long.class) {
return (T)Long.valueOf(str);
}else {

return (T) JSON.parseArray(str,clazz);
}
}

/**
 * 关闭连接
 * @param jedis
 */
private void returnToPool(Jedis jedis) {
if(jedis != null) {
jedis.close();
}
}

/**
 * 判断key是否存在
 * */
public <T> boolean exists(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();

return jedis.exists(key);
}finally {
returnToPool(jedis);
}
}
}

3、实体类实现序列化接口 

public class Test implements Serializable {}

4、测试

/**
 * <b>Function: </b> todo
 *
 * @program: BaseTest
 * @Package: com.kingbal.king.dmp
 * @author: dingcho
 * @date: 2024/01/09
 * @version: 1.0
 * @Copyright: 2024 www.kingbal.com Inc. All rights reserved.
 */
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class BaseTest {

    @Autowired
private RedisService redisService;

@Test
public void testGetList() {
    List<Test> testList = null;
        String key = "test:list";
long start = System.currentTimeMillis();
    if(redisService.exists(key)) {
            System.out.println("redis 读取 "+ key+" 数据");
testList = (List<Test>) redisService.get(key, Test.class);
        }else{
System.out.println("redis 读写入"+ key+" 数据");
testList = Lists.newArrayList();
            ....
redisService.set(key, testList, -1);
        }

        System.out.println(testList.size());
log.error("耗时 >> " + (System.currentTimeMillis() - start));
}

}

原文地址:https://blog.csdn.net/yao_guai/article/details/135469745

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!