redis链接池工具类

package com.zp.util;


import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;


public class RedisUtil {
// Redis服务器IP
private static String ADDR = "127.0.0.1";


// Redis的端口号
private static int PORT = 6379;


// 访问密码
// private static String AUTH = "requirepass";


// 可用连接实例的最大数目,默认值为8;
// 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
private static int MAX_ACTIVE = 1024;


// 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
private static int MAX_IDLE = 200;


// 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;
private static int MAX_WAIT = 10000;


private static int TIMEOUT = 10000;


// 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
private static boolean TEST_ON_BORROW = true;


protected static  JedisPool jedisPool = null;


/**
* 初始化Redis连接池
*/
static {
try {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(MAX_ACTIVE);
config.setMaxIdle(MAX_IDLE);
config.setMaxWait(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT);
} catch (Exception e) {
e.printStackTrace();
}
}


/**
* 获取Jedis实例

* @return
*/
public synchronized static Jedis getJedis() {
try {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
return resource;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}


/**
* 释放jedis资源

* @param jedis
*/
public static void returnResource(final Jedis jedis) {
if (jedis != null) {
jedisPool.returnResource(jedis);
}
}
public static void main(String[] args) {
JedisPool jedisPool = RedisUtil.jedisPool;
Jedis jedis = jedisPool.getResource();  
jedis.set("xixi", "1111");
}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Java 工具类,可以使用 Socket 建立 Redis,并使用指定密码进行身份验证,然后执行 Redis 命令。请注意,此示例代码仅用于演示目的,不应在生产环境中使用。 ```java import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; public class RedisTool { private static final String REDIS_HOST = "localhost"; private static final int REDIS_PORT = 6379; private static final String REDIS_PASSWORD = "your_password_here"; public static void main(String[] args) { try (Socket socket = new Socket(REDIS_HOST, REDIS_PORT)) { InputStream inputStream = socket.getInputStream(); OutputStream outputStream = socket.getOutputStream(); // 发送 AUTH 命令进行身份验证 sendCommand(outputStream, "AUTH " + REDIS_PASSWORD + "\r\n"); // 执行其他 Redis 命令 String result1 = sendCommand(outputStream, "PING\r\n"); String result2 = sendCommand(outputStream, "SET key value\r\n"); String result3 = sendCommand(outputStream, "GET key\r\n"); System.out.println(result1); System.out.println(result2); System.out.println(result3); } catch (IOException e) { e.printStackTrace(); } } private static String sendCommand(OutputStream outputStream, String command) throws IOException { byte[] buffer = new byte[1024]; outputStream.write(command.getBytes()); int count = inputStream.read(buffer); return new String(buffer, 0, count); } } ``` 在此示例中,首先使用 Socket 建立与本地 Redis 服务器的连。然后,使用 `sendCommand` 方法发送 `AUTH` 命令进行身份验证。如果密码正确,Redis 服务器将返回 `OK` 响应。然后,使用 `sendCommand` 方法执行其他 Redis 命令,并在控制台上打印响应。请注意,此示例中的 Redis 命令都是字符串格式,必须以 `\r\n` 结尾。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值