当前位置:网站首页>redis连接idea
redis连接idea
2022-08-11 05:12:00 【匿名热心市民】
redis支持的语言
java连接redis的第三方依赖,常用前三个
新建maven工程
导入依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.8.0</version>
</dependency>
单机连接
启动redis,确保redis的远程访问处于开启
测试
对string的操作测试
对hash的操作测试
使用连接池连接
public void Myjiqn(){
/*创建连接池配置类*/
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
/*最大空闲数*/
jedisPoolConfig.setMaxIdle(20);
/*最小空闲数*/
jedisPoolConfig.setMinIdle(5);
/*最大等待时间*/
jedisPoolConfig.setMaxWait(Duration.ofMillis(3000));
/*创建对象,连接集群*/
JedisPool jedisPool = new JedisPool(jedisPoolConfig,"192.168.92.241",6379);
/*耗时测试,获取当前时间*/
long start = System.currentTimeMillis();
for(int i=0;i<50;i++){
/*从连接池获取资源*/
Jedis jedis = jedisPool.getResource();
/*进行ping测试*/
String ping = jedis.ping();
/*关闭资源*/
jedis.close();
}
/*耗时测试,获取当前时间*/
long end = System.currentTimeMillis();
System.out.println("耗时"+(end-start));
}
集群连接
public void Myjiqun(){
/*设置集群*/
Set<HostAndPort> nodes = new HashSet<>();
nodes.add(new HostAndPort("192.168.92.241",6001));
nodes.add(new HostAndPort("192.168.92.241",6002));
nodes.add(new HostAndPort("192.168.92.241",6003));
nodes.add(new HostAndPort("192.168.92.241",6004));
nodes.add(new HostAndPort("192.168.92.241",6005));
nodes.add(new HostAndPort("192.168.92.241",6006));
/*连接集群*/
JedisCluster jedisCluster = new JedisCluster(nodes);
/*调用方法*/
String set = jedisCluster.set("k1", "张三");
System.out.println(set);
/*关闭资源*/
jedisCluster.close();
}
边栏推荐
猜你喜欢
论文笔记:BBN: Bilateral-Branch Network with Cumulative Learningfor Long-Tailed Visual Recognition
分库分表ShardingSphere-JDBC笔记整理
交换机和路由器技术-25-OSPF多区域配置
IP-Guard如何禁止运行U盘程序
How IP-Guard prohibits running U disk programs
元宇宙社交应用,靠什么吸引用户「为爱发电」?
async(异步)和await的使用
【嵌入式开源库】MultiButton的使用,简单易用的事件驱动型按键驱动模块
prometheus:(二)监控概述(你永远逃不出我的手掌哈哈)
普林斯顿微积分读本05第四章--求解多项式的极限问题
随机推荐
交换机和路由器技术-36-端口镜像
ERROR: Could not install packages due to an OSError: [Errno 2] No such file or directory: '/data/xxxx
C Language: Practical Debugging Tips
Weekly recommended short video: your commonly used Polaroid, its predecessor turned out to be like this!
C language antithesis: who is the murderer!
@Resource和@Autowired的区别
交换机和路由器技术-33-静态NAT
Switch and Router Technology - 25 - OSPF Multi-Area Configuration
[Embedded open source library] The use of MultiButton, an easy-to-use event-driven button driver module
How to read a paper
C语言题解:谁是凶手!
报表控件Stimulsoft报告中的数据矩阵条形码介绍
paddlepaddle implements CS_CE Loss and incorporates PaddleClas
You must understand - the nine built-in objects and four domain objects of JSP
leetcode 9. Palindromic Numbers
MySQL must know and must know (primary articles)
一个月闭关直接面进大厂,这份Android面试笔记是真的牛逼
开发工具篇第七讲:阿里云日志查询与分析
MFC 进程间通信(共享内存)
K8s Review Notes 7--K8S Implementation of Redis Standalone and Redis-cluster