前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java连接redis集群方式_redis java

java连接redis集群方式_redis java

作者头像
全栈程序员站长
发布2022-11-04 15:11:46
1K0
发布2022-11-04 15:11:46
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

代码语言:javascript
复制
package org.rx.util;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisShardInfo;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Set;
/**
* Created by wangxiaoming on 2016/3/29.
*
* @author http://blog.csdn.net/java2000_wl
*/
@Component
public class RedisClient {
private static RedissonClient redisson;
//https://github.com/mrniko/redisson/wiki/8.-Distributed-locks-and-synchronizers
public synchronized static RedissonClient getRedisson() {
if (redisson == null) {
Map<String, String> map = App.readSettings("app");
Config config = new Config();
config.useSingleServer().setAddress(String.format("%s:%s", map.get("redis.host"), map.get("redis.port")))
.setTimeout(App.convert(map.get("redis.timeout"), Integer.class));
redisson = Redisson.create(config);
}
return redisson;
}
private static RedisTemplate<String, Object> Template;
@Autowired
private RedisTemplate<String, Object>        template;
private String                               keyPrefix;
public String getKeyPrefix() {
return keyPrefix;
}
public void setKeyPrefix(String keyPrefix) {
if (template != null) {
throw new IllegalArgumentException("Autowired Instance");
}
this.keyPrefix = keyPrefix;
}
private RedisTemplate<String, Object> getTemplate() {
if (template == null && Template == null) {
Map<String, String> map = App.readSettings("app");
JedisShardInfo config = new JedisShardInfo(map.get("redis.host"), Integer.parseInt(map.get("redis.port")));
JedisConnectionFactory fac = new JedisConnectionFactory(config);
fac.setTimeout(App.convert(map.get("redis.timeout"), Integer.class));
fac.setUsePool(true);
Template = new RedisTemplate<>();
Template.setConnectionFactory(fac);
Template.setKeySerializer(
new org.springframework.data.redis.serializer.StringRedisSerializer(Charset.forName("UTF8")));
Template.setValueSerializer(
new org.springframework.data.redis.serializer.JdkSerializationRedisSerializer());
Template.afterPropertiesSet();
}
return App.isNull(template, Template);
}
private byte[] getKeyBytes(String key) {
try {
key = App.isNull(keyPrefix, "") + key;
return key.getBytes(App.UTF8);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
public void set(String key, Object value) {
this.set(key, value, 0L);
}
public void set(final String key, final Object value, final long liveTime) {
getTemplate().execute(new RedisCallback() {
public Long doInRedis(RedisConnection client) throws DataAccessException {
byte[] theKey = getKeyBytes(key);
client.set(theKey, App.serialize(value));
if (liveTime > 0) {
client.expire(theKey, liveTime);
}
return 1L;
}
});
}
public Object get(final String key) {
return getTemplate().execute(new RedisCallback() {
public Object doInRedis(RedisConnection client) throws DataAccessException {
byte[] theKey = getKeyBytes(key);
byte[] theVal = client.get(theKey);
if (theVal == null || theVal.length == 0) {
return null;
}
return App.deserialize(theVal);
}
});
}
public long del(final String... keys) {
return (long) getTemplate().execute(new RedisCallback() {
public Long doInRedis(RedisConnection client) throws DataAccessException {
long result = 0;
for (String key : keys) {
result += client.del(getKeyBytes(key));
}
return result;
}
});
}
public Set<String> keys(String pattern) {
return getTemplate().keys(pattern);
}
public long dbSize() {
return (long) getTemplate().execute(new RedisCallback<Object>() {
public Long doInRedis(RedisConnection client) throws DataAccessException {
return client.dbSize();
}
});
}
public boolean exists(final String key) {
return (boolean) getTemplate().execute(new RedisCallback() {
public Boolean doInRedis(RedisConnection client) throws DataAccessException {
return client.exists(getKeyBytes(key));
}
});
}
public void flushDb() {
getTemplate().execute(new RedisCallback() {
public Object doInRedis(RedisConnection client) throws DataAccessException {
client.flushDb();
return null;
}
});
}
}
代码语言:javascript
复制
 <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.8.6.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>3.5.0</version> </dependency>

转载于:https://www.cnblogs.com/Googler/p/7422489.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/182705.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年10月12日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档