java连接redis集群方式_redis java

java连接redis集群方式_redis javapackageorg.rx.util;importorg.redisson.Redisson;importorg.redisson.api.RedissonClient;importorg.redisson.config.Config;importorg.springframework.beans.factory.annotation.Autowired;im…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

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;
            }
        });
    }
}

  

        <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.net/182705.html原文链接:https://javaforall.net

(0)
上一篇 2022年10月12日 下午6:46
下一篇 2022年10月12日 下午6:46


相关推荐

  • PHY芯片快速深度理解(持续更新中……)

    PHY芯片快速深度理解(持续更新中……)什么是 phy 物理层芯片称为 PHY 数据链路层芯片称为 MAC 可以看到 PHY 的数据是 RJ45 网络接口 网线口 穿过了的差分信号 而 PHY 作用就是将差分信号转为数字信号 这块内容不用深究 制造商都设计好了 RJ45 座子上一般有两个灯 一个黄色 橙色 一个绿色 绿色亮的话表示网络连接正常 黄色闪烁的话说明当前正在进行网络通信 黄灯闪动频率快表示网速好 这两个灯由 PHY 芯片控制 如果不懂物理层和数据链路层可以看一下网络七层协议 什么是 mido 协议 mido 协议即 SMI 协议

    2026年3月18日
    1
  • macOS 10.12 不允许未知来源开发者的应用

    macOS 10.12 不允许未知来源开发者的应用在终端输入 sudospctlmas disable 注意 master 前面是两个 因为有的时候会出现两个 合成一个 所以说明一下

    2026年3月7日
    3
  • C语言位运算符_C语言左移和右移的区别

    C语言位运算符_C语言左移和右移的区别如果你想了解以下位运算符的话我想你来对了地方&^|~<<>>首先明确位运算符都是在二进制位上运算的先讲比较简单的<<>>(有些人可能认为这个最难以理解包括我)后来我陡然一时想到了十进制左移“<<”右移“>>”十进制10左移三位就是乘以10的3次方=1000010右…

    2022年10月5日
    4
  • 最近在使用腾讯元宝的时候,Deepseek突然变得卡壳了!

    最近在使用腾讯元宝的时候,Deepseek突然变得卡壳了!

    2026年3月13日
    1
  • 编写sudoers文件

    编写sudoers文件前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程一、功能简介sudo能够限制指定用户在指定主机上运行某些命令。sudo可以提供日志,忠实地记录每个用户使用sudo做了些什么,并且能将日志传到中心主机或者日志服务器。sudo为系统管理员提供配置文件,允许系统管理员集中地管理用户的使用权限和使用的主机。它默认的存放位置是/etc/sudoers。sudo使用时间戳文件来完成类似“检票”的系统。当用户执行sudo并且输入密码后,用户获得了一张默认存活期为5

    2022年6月20日
    29
  • java-计算器模板及源码

    java-计算器模板及源码java-计算器模板及源码计算器实现了大部分基础功能:基本运算,菜单栏选项,并且拓展了普通型和科学兴选项等等,读者可以在此基础上进行修改和拓展。其他具体实现方法可以看源码,里面有详细的概述,代码框架清晰。读者在阅读和引用过程中,如有问题欢迎评论区留言和私信交流。运行环境:win10EclipseIDEforJavaDevelopers-2020-06下面是计算器的视图:importjava.awt.*;importjava.awt.event.ActionEvent;im

    2022年7月19日
    15

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号