commit 7f206d3969246336f4ffcc65765337f387ef749c Author: Lu-aiLiang <13208981+Lu-aiLiang@user.noreply.gitee.com> Date: Sun Oct 29 19:43:37 2023 +0800 csh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/$PROJECT_FILE$ b/.idea/$PROJECT_FILE$ new file mode 100644 index 0000000..58b7e3e --- /dev/null +++ b/.idea/$PROJECT_FILE$ @@ -0,0 +1,11 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..ecf5e69 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..5a2f139 --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..5825257 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/qaplug_profiles.xml b/.idea/qaplug_profiles.xml new file mode 100644 index 0000000..3dfd21f --- /dev/null +++ b/.idea/qaplug_profiles.xml @@ -0,0 +1,465 @@ + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..e02136a --- /dev/null +++ b/pom.xml @@ -0,0 +1,57 @@ + + + 4.0.0 + + groupId + redisPool + 1.0-SNAPSHOT + + + 8 + 8 + UTF-8 + + + + + + org.springframework.boot + spring-boot-starter + 2.4.3 + + + + org.springframework.boot + spring-boot-starter-test + 2.4.3 + + + + redis.clients + jedis + 3.7.1 + + + + org.springframework.boot + spring-boot-starter-web + 2.4.3 + + + + org.springframework.boot + spring-boot-starter-tomcat + 2.4.3 + + + junit + junit + 4.13.2 + test + + + + + diff --git a/redisPool.iml b/redisPool.iml new file mode 100644 index 0000000..a80f8af --- /dev/null +++ b/redisPool.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/main/java/com/lhj/ai/IRedisPool.java b/src/main/java/com/lhj/ai/IRedisPool.java new file mode 100644 index 0000000..9cf1e6f --- /dev/null +++ b/src/main/java/com/lhj/ai/IRedisPool.java @@ -0,0 +1,23 @@ +package com.lhj.ai; + +import redis.clients.jedis.Jedis; + +/** + * @version: java version 1.8 + * @Author: Mr Orange + * @description: + * @date: 2023-10-29 14:35 + */ +public interface IRedisPool { + public void init(int maxTotal,long maxWaitMillis); + /** + * 获取 连接 + * */ + public Jedis getResource()throws Exception; + /** + * 取得连接 + * */ + public void release(Jedis jedis); + + +} diff --git a/src/main/java/com/lhj/ai/PoolApplication.java b/src/main/java/com/lhj/ai/PoolApplication.java new file mode 100644 index 0000000..660b49b --- /dev/null +++ b/src/main/java/com/lhj/ai/PoolApplication.java @@ -0,0 +1,17 @@ +package com.lhj.ai; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @version: java version 1.8 + * @Author: Mr Orange + * @description: + * @date: 2023-10-29 19:35 + */ +@SpringBootApplication +public class PoolApplication { + public static void main(String[] args) { + SpringApplication.run(PoolApplication.class); + } +} diff --git a/src/main/java/com/lhj/ai/myRedisPool.java b/src/main/java/com/lhj/ai/myRedisPool.java new file mode 100644 index 0000000..5cc8309 --- /dev/null +++ b/src/main/java/com/lhj/ai/myRedisPool.java @@ -0,0 +1,81 @@ +package com.lhj.ai; + +import redis.clients.jedis.Jedis; + +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * @version: java version 1.8 + * @Author: Mr Orange + * @description: + * @date: 2023-10-29 16:20 + */ +public class myRedisPool implements IRedisPool{ + private int maxTotal; + private long maxWautMillis; + //定义一个空闲的连接集合; + private LinkedBlockingQueue idleObjects = null; + private LinkedBlockingQueue activObjects = null; + + private AtomicInteger count = new AtomicInteger(); + @Override + public void init(int maxTotal, long maxWaitMillis) { + this.maxTotal = maxTotal; + this.maxWautMillis = maxWaitMillis; + idleObjects = new LinkedBlockingQueue(maxTotal); + activObjects = new LinkedBlockingQueue(maxTotal); + } + + @Override + public Jedis getResource() throws Exception { + //伪代码 理思路 + //第一步,记录开始时间戳,用于判断超时时间. + //第二步, + long startTime = System.currentTimeMillis(); + Jedis redis = null; + while (true){ + redis = idleObjects.poll(); + if (redis != null){ + activObjects.offer(redis); + return redis; + } + if (count.get() < maxTotal){ + if (count.incrementAndGet() <= maxTotal){ + new Jedis("192.168.31.128",6379); + System.out.println("redis 创建了一个连接"); + activObjects.offer(redis); + return redis; + }else { + count.decrementAndGet(); + } + } + //4.如果连接池慢了,等待其他线程释放连接到空闲连接集合中,如果说是一定时间之内,可以的等到一个空闲的连接,将连接放入到活动连接集合中,返回该连接 + try { + idleObjects.poll(maxWautMillis - (System.currentTimeMillis() - startTime), TimeUnit.MILLISECONDS); + if (redis !=null){ + activObjects.offer(redis); + return redis; + } + }catch (Exception e){ + e.printStackTrace(); + } + //5.如果等待时间超过了我们最长等待时间,抛出等待超时异常. + if (maxWautMillis < (System.currentTimeMillis() - startTime)){ + throw new Exception("time,超过最长等待连接时间"); + }else { + continue; + } + + } + + } + + @Override + public void release(Jedis jedis) { + if (activObjects.remove(jedis)){ + idleObjects.offer(jedis); + } + } +} diff --git a/src/test/java/IRedisPoolTest.java b/src/test/java/IRedisPoolTest.java new file mode 100644 index 0000000..d35f856 --- /dev/null +++ b/src/test/java/IRedisPoolTest.java @@ -0,0 +1,58 @@ +import com.lhj.ai.IRedisPool; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.test.context.SpringBootTest; +import redis.clients.jedis.Jedis; + +import java.util.concurrent.CountDownLatch; + +/** + * @version: java version 1.8 + * @Author: Mr Orange + * @description: + * @date: 2023-10-29 15:05 + */ +@SpringBootTest +public class IRedisPoolTest { + @Qualifier("myRedisPool") + @Autowired + private IRedisPool pool; + + private final static int THRAD_NUM = 50; + + private CountDownLatch cd1 = new CountDownLatch(THRAD_NUM); + + + @Test + public void test() throws Exception { + pool.init(20, 2000); + for (int i = 0; i < THRAD_NUM; i++) { + + new Thread(new Runnable() { + @Override + public void run() { + try { + cd1.await(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + Jedis jedis = null; + try { + jedis = pool.getResource(); + jedis.incr("pooltest.incr"); + + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + pool.release(jedis); + } + + } + }).start(); + cd1.countDown(); + + } + Thread.sleep(2000); + } +} diff --git a/target/classes/classpath.index b/target/classes/classpath.index new file mode 100644 index 0000000..4058d7b Binary files /dev/null and b/target/classes/classpath.index differ diff --git a/target/classes/com/lhj/ai/IRedisPool.class b/target/classes/com/lhj/ai/IRedisPool.class new file mode 100644 index 0000000..596c123 Binary files /dev/null and b/target/classes/com/lhj/ai/IRedisPool.class differ diff --git a/target/classes/com/lhj/ai/PoolApplication.class b/target/classes/com/lhj/ai/PoolApplication.class new file mode 100644 index 0000000..095bfee Binary files /dev/null and b/target/classes/com/lhj/ai/PoolApplication.class differ diff --git a/target/classes/com/lhj/ai/myRedisPool.class b/target/classes/com/lhj/ai/myRedisPool.class new file mode 100644 index 0000000..14f0058 Binary files /dev/null and b/target/classes/com/lhj/ai/myRedisPool.class differ diff --git a/target/test-classes/IRedisPoolTest$1.class b/target/test-classes/IRedisPoolTest$1.class new file mode 100644 index 0000000..2433767 Binary files /dev/null and b/target/test-classes/IRedisPoolTest$1.class differ diff --git a/target/test-classes/IRedisPoolTest.class b/target/test-classes/IRedisPoolTest.class new file mode 100644 index 0000000..3de78d1 Binary files /dev/null and b/target/test-classes/IRedisPoolTest.class differ diff --git a/target/test-classes/classpath.index b/target/test-classes/classpath.index new file mode 100644 index 0000000..52d717c Binary files /dev/null and b/target/test-classes/classpath.index differ