JAVA中 redisTemplate 和 jedis的配合使用操作

网友投稿 323 2023-02-01

JAVA中 redisTemplate 和 jedis的配合使用操作

首先项目A,也就是SpringBOOT项目中使用redisTemplate 来做REDIS的缓存时,你会发现存到REDIS里边的KEY和VALUE,redisTemplat使用jdkSerializeable存储二进制字节编码

项目B中使用jedis时,存储起来的是字符串,导致项目A要调用项目缓存的键值对时,获取不到

解决方案:

修改项目A的redisTemplate的序列方式

@Configuration

@EnableCaching

public class RedisConfig extends CachingConfigurerSupport {

/**

* redis模板,存储关键字是字符串,值是Jdk序列化

* @param factory

* @return

* @Description:

*/

@Bean

public RedisTemplate, ?> redisTemplate(RedisConnectionFactory factory) {

RedisTemplate, ?> redisTemplate = new RedisTemplate<>();

redisTemplate.setConnectionFactory(factory);

//key序列化方式;但是如果方法上有Long等非String类型的话,会报类型转换错误;

RedisSerializer redisSerializer = new StringRedisSerializer();//Long类型不http://可以会出现异常信息;

redisTemplate.setKeySerializer(redisSerializer);

redisTemplate.setHashKeySerializer(redisSerializer);

//默认使用JdkSerializationRedisSerializer序列化方式;会出现乱码,改成StringRedisSerializer

StringRedisSerializer stringSerializer = new StringRedisSerializer();

redisTemplate.setKeySerializer(stringSerializer);

redisTemplate.setValueSerializer(stringSerializer);

redisTemplate.setHashKeySerializer(stringSerializer);

redisTemplate.setHashValueSerializer(stringSerializer);

return redisTemplate;

}

}

补充:RedisTemplate初始化和创建(非Spring注入方式)

概述

在工作中, 可能会在非Spring项目(如Spark,Flink作业)中去操作Redis, 重复造轮子去写工具类没有太大的意义, 使用RedisTemplate已经足够丰富和完善了,使用New的方式进行创建即可, 不同的spring-data-redis的版本会略有不同, 下面以2.3.0和1.8.9做为示例.

2.3.0

maven

org.springframework.data

spring-data-redis

2.3.0.RELEASE

redis.clients

jedis

3.3.0

代码

import org.springframework.data.redis.connection.RedisClusterConfiguration;

import org.springframework.data.redis.connection.RedisNode;

import org.springframework.data.redis.connection.RedisStandaloneConfiguration;

import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.core.ValueOperations;

import java.util.Collections;

import java.util.List;

import java.util.Objects;

public class RedisTest {

public static void main(String[] args) {

//单机模式

RedisStandaloneConfiguration rsc = new RedisStandaloneConfigurahttp://tion();

rsc.setPort(6379);

rsc.setPassword("123456");

rsc.setHostName("192.168.1.1");

//集群模式

RedisClusterConfiguration rcc = new RedisClusterConfiguration();

rcc.setPassword("123456");

List nodes = Collections.singletonList(new RedisNode("192.168.1.1", 6379));

rcc.setClusterNodes(nodes);

RedisTemplate template = new RedisTemplate<>();

//单机模式

JedisConnectionFactory fac = new JedisConnectionFactory(rsc);

//集群模式

//JedisConnectionFactory fac = new JedisConnectionFactory(rcc);

fac.afterPropertiesSet();

template.setConnectionFactory(fac);

template.setDefaultSerializer(new StringRedisSerializer());

template.afterPropertiesSet();

ValueOperations op = template.opsForValue();

final String key = "123_tmp";

final String value = "abc";

template.delete(key);

op.set(key, value);

assert Objects.equals(op.get(key), vhttp://alue);

}

}

集群方式运行报错

Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: ERR This instance has cluster support disabled

解决

在redis.conf下将cluster-enabled改为yes

如果只有一个节点, 改为单机模式

1.8.9

maven

org.springframework.data

spring-data-redis

1.8.9.RELEASE

redis.clients

jedis

2.9.0

代码

import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.core.ValueOperations;

import redis.clients.jedis.JedisPoolConfig;

import redis.clients.jedis.JedisShardInfo;

import java.util.Objects;

public class RedisTest {

public static void main(String[] args) {

RedisTemplate template = new RedisTemplate<>();

JedisConnectionFactory fac = new JedisConnectionFactory(new JedisPoolConfig());

JedisShardInfo shardInfo = new JedisShardInfo("192.168.1.1", 6379);

shardInfo.setPassword("123456");

fac.setShardInfo(shardInfo);

template.setConnectionFactory(fac);

template.setDefaultSerializer(new StringRedisSerializer());

template.afterPropertiesSet();

ValueOperations op = template.opsForValue();

final String key = "123_tmp";

final String value = "abc";

template.delete(key);

op.set(key, value);

assert Objects.equals(op.get(key), value);

}

}

这里有个小细节, 如果不调用setShardInfo()方法, 那么需要执行下面的代码, afterPropertiesSet()用来初始化

JedisConnectionFacVQmBZQktory fac = new JedisConnectionFactory(new JedisPoolConfig());

fac.setPort(6379);

fac.setPassword("123456");

fac.setHostName("192.168.1.1");

fac.afterPropertiesSet();

说明

RedisTemplate的构造方法有多种, 上面所举例子为其中的一种; 不通过SpringBoot自动装配的方式, 必须手动去执行afterPropertiesSet()进行初始化; 可参考SpringBoot整合redis的方式, 查看对应实现

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:调用api接口(调用api接口教程)
下一篇:开放API接口的网盘(百度网盘api开发文档)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~