From 1d408f4f4a40cee3b65a7f85cf5b734c24e8731d Mon Sep 17 00:00:00 2001 From: zhuangqian Date: Thu, 5 Jan 2017 22:27:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96redis=E9=9B=86=E7=BE=A4?= =?UTF-8?q?=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../smallchill/core/interfaces/ICache.java | 82 +++++++++--------- .../smallchill/core/interfaces/ICheck.java | 72 ++++++++-------- .../core/listener/StopListener.java | 86 +++++++++---------- .../plugins/connection/ConnectionPlugin.java | 9 +- .../smallchill/core/toolbox/redis/IJedis.java | 12 +++ .../core/toolbox/redis/RedisCluster.java | 35 ++++++-- .../core/toolbox/redis/RedisSingle.java | 10 +++ 7 files changed, 178 insertions(+), 128 deletions(-) diff --git a/src/main/java/com/smallchill/core/interfaces/ICache.java b/src/main/java/com/smallchill/core/interfaces/ICache.java index 84ea024a..17c7f310 100644 --- a/src/main/java/com/smallchill/core/interfaces/ICache.java +++ b/src/main/java/com/smallchill/core/interfaces/ICache.java @@ -1,41 +1,41 @@ -/** - * Copyright (c) 2015-2017, Chill Zhuang 庄骞 (smallchill@163.com). - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.smallchill.core.interfaces; - -import java.util.List; -import java.util.Map; - -/** - * 缓存用接口 - */ -public interface ICache { - - Map findOne(String cacheName, String key, String sql); - - Map findOne(String cacheName, String key, String sql, Object modelOrMap); - - Map findOneBySqlId(String cacheName, String key, String sqlId); - - Map findOneBySqlId(String cacheName, String key, String sqlId, Object modelOrMap); - - List> find(String cacheName, String key, String sql); - - List> find(String cacheName, String key, String sql, Object modelOrMap); - - List> findBySqlId(String cacheName, String key, String sqlId); - - List> findBySqlId(String cacheName, String key, String sqlId, Object modelOrMap); -} +/** + * Copyright (c) 2015-2017, Chill Zhuang 庄骞 (smallchill@163.com). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.smallchill.core.interfaces; + +import java.util.List; +import java.util.Map; + +/** + * 缓存用接口 + */ +public interface ICache { + + Map findOne(String cacheName, String key, String sql); + + Map findOne(String cacheName, String key, String sql, Object modelOrMap); + + Map findOneBySqlId(String cacheName, String key, String sqlId); + + Map findOneBySqlId(String cacheName, String key, String sqlId, Object modelOrMap); + + List> find(String cacheName, String key, String sql); + + List> find(String cacheName, String key, String sql, Object modelOrMap); + + List> findBySqlId(String cacheName, String key, String sqlId); + + List> findBySqlId(String cacheName, String key, String sqlId, Object modelOrMap); +} diff --git a/src/main/java/com/smallchill/core/interfaces/ICheck.java b/src/main/java/com/smallchill/core/interfaces/ICheck.java index a5a6713d..60972d4b 100644 --- a/src/main/java/com/smallchill/core/interfaces/ICheck.java +++ b/src/main/java/com/smallchill/core/interfaces/ICheck.java @@ -1,36 +1,36 @@ -/** - * Copyright (c) 2015-2017, Chill Zhuang 庄骞 (smallchill@163.com). - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.smallchill.core.interfaces; - - -/** - * 检查用接口 - */ -public interface ICheck { - - /** - * 检查指定角色 - * @param permissions - * @return boolean - */ - boolean check(Object[] permissions); - - /** - * 检查全体角色 - * @return boolean - */ - boolean checkAll(); -} +/** + * Copyright (c) 2015-2017, Chill Zhuang 庄骞 (smallchill@163.com). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.smallchill.core.interfaces; + + +/** + * 检查用接口 + */ +public interface ICheck { + + /** + * 检查指定角色 + * @param permissions + * @return boolean + */ + boolean check(Object[] permissions); + + /** + * 检查全体角色 + * @return boolean + */ + boolean checkAll(); +} diff --git a/src/main/java/com/smallchill/core/listener/StopListener.java b/src/main/java/com/smallchill/core/listener/StopListener.java index 87c1242c..62ddf38c 100644 --- a/src/main/java/com/smallchill/core/listener/StopListener.java +++ b/src/main/java/com/smallchill/core/listener/StopListener.java @@ -1,44 +1,44 @@ -/** - * Copyright (c) 2015-2017, Chill Zhuang 庄骞 (smallchill@163.com). - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.smallchill.core.listener; - -import org.springframework.context.ApplicationListener; -import org.springframework.context.event.ContextStoppedEvent; -import org.springframework.stereotype.Component; - -import com.smallchill.core.plugins.PluginManager; - -/** - * 关闭监听器 - */ -@Component -public class StopListener implements ApplicationListener { - - @Override - public void onApplicationEvent(ContextStoppedEvent event) { - if (event.getApplicationContext().getParent() == null) { - destroyPlugin(); - } - } - - /** - * 插件的停用 - */ - private void destroyPlugin() { - PluginManager.init().stop(); - } - +/** + * Copyright (c) 2015-2017, Chill Zhuang 庄骞 (smallchill@163.com). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.smallchill.core.listener; + +import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ContextClosedEvent; +import org.springframework.stereotype.Component; + +import com.smallchill.core.plugins.PluginManager; + +/** + * 关闭监听器 + */ +@Component +public class StopListener implements ApplicationListener { + + @Override + public void onApplicationEvent(ContextClosedEvent event) { + if (event.getApplicationContext().getParent() == null) { + destroyPlugin(); + } + } + + /** + * 插件的停用 + */ + private void destroyPlugin() { + PluginManager.init().stop(); + } + } \ No newline at end of file diff --git a/src/main/java/com/smallchill/core/plugins/connection/ConnectionPlugin.java b/src/main/java/com/smallchill/core/plugins/connection/ConnectionPlugin.java index 4b77c488..66f65af9 100644 --- a/src/main/java/com/smallchill/core/plugins/connection/ConnectionPlugin.java +++ b/src/main/java/com/smallchill/core/plugins/connection/ConnectionPlugin.java @@ -17,13 +17,16 @@ package com.smallchill.core.plugins.connection; import com.smallchill.core.config.BladeConfig; import com.smallchill.core.interfaces.IPlugin; +import com.smallchill.core.toolbox.kit.LogKit; import com.smallchill.core.toolbox.redis.IJedis; import com.smallchill.core.toolbox.redis.IKeyNamingPolicy; import com.smallchill.core.toolbox.redis.RedisCluster; import com.smallchill.core.toolbox.redis.RedisSingle; import com.smallchill.core.toolbox.redis.serializer.JdkSerializer; + import org.beetl.sql.core.IDAutoGen; import org.beetl.sql.core.SQLManager; + import redis.clients.jedis.JedisCluster; import redis.clients.jedis.JedisPool; @@ -95,8 +98,12 @@ public class ConnectionPlugin implements IPlugin{ } public void stop() { - sqlManagerPool.clear(); + for (IJedis jedis : redisCachePool.values()) { + jedis.close(); + } redisCachePool.clear(); + sqlManagerPool.clear(); + LogKit.println("ConnectionPlugin关闭成功"); } } diff --git a/src/main/java/com/smallchill/core/toolbox/redis/IJedis.java b/src/main/java/com/smallchill/core/toolbox/redis/IJedis.java index 276ced81..535c6b57 100644 --- a/src/main/java/com/smallchill/core/toolbox/redis/IJedis.java +++ b/src/main/java/com/smallchill/core/toolbox/redis/IJedis.java @@ -19,6 +19,8 @@ import java.util.List; import java.util.Map; import java.util.Set; +import com.smallchill.core.interfaces.ICallBack; + /** * Redis 单机、集群统一接口 */ @@ -582,4 +584,14 @@ public interface IJedis { */ public Double zscore(Object key, Object member); + /** + * 回调接口 + */ + public T call(ICallBack call); + + /** + * 关闭连接 + */ + public void close(); + } diff --git a/src/main/java/com/smallchill/core/toolbox/redis/RedisCluster.java b/src/main/java/com/smallchill/core/toolbox/redis/RedisCluster.java index 2fbb513c..d1f5a585 100644 --- a/src/main/java/com/smallchill/core/toolbox/redis/RedisCluster.java +++ b/src/main/java/com/smallchill/core/toolbox/redis/RedisCluster.java @@ -26,8 +26,12 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.JedisPool; +import com.smallchill.core.interfaces.ICallBack; +import com.smallchill.core.toolbox.kit.LogKit; import com.smallchill.core.toolbox.redis.serializer.ISerializer; /** @@ -96,7 +100,16 @@ public class RedisCluster implements IJedis{ } public Set keys(String pattern) { - return null; + Set keys = new HashSet<>(); + JedisCluster jedis = getJedis(); + Map clusterNodes = jedis.getClusterNodes(); + for (JedisPool jedisPool : clusterNodes.values()) { + Jedis resource = jedisPool.getResource(); + Set key = resource.keys(pattern); + keys.addAll(key); + resource.close(); + } + return keys; } public String mset(Object... keysValues) { @@ -743,6 +756,20 @@ public class RedisCluster implements IJedis{ } finally {close(jedis);} } + + + public void close() { + try { + jedisCluster.close(); + } catch (IOException e) { + LogKit.error(e.getMessage(), e); + } + } + + public T call(ICallBack call) { + JedisCluster jedis = getJedis(); + return call.call(jedis); + } // --------- @@ -833,13 +860,7 @@ public class RedisCluster implements IJedis{ public void close(JedisCluster jedis) { if (threadLocalJedis.get() == null && jedis != null) - try { - jedis.close(); - } catch (IOException e) { - e.printStackTrace(); - } finally { removeThreadLocalJedis(); - } } public JedisCluster getThreadLocalJedis() { diff --git a/src/main/java/com/smallchill/core/toolbox/redis/RedisSingle.java b/src/main/java/com/smallchill/core/toolbox/redis/RedisSingle.java index 8cd051d2..103152a6 100644 --- a/src/main/java/com/smallchill/core/toolbox/redis/RedisSingle.java +++ b/src/main/java/com/smallchill/core/toolbox/redis/RedisSingle.java @@ -25,6 +25,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import com.smallchill.core.interfaces.ICallBack; import com.smallchill.core.toolbox.redis.serializer.ISerializer; import redis.clients.jedis.Jedis; @@ -789,6 +790,15 @@ public class RedisSingle implements IJedis{ } finally {close(jedis);} } + + public void close() { + jedisPool.close(); + } + + public T call(ICallBack call) { + Jedis jedis = getJedis(); + return call.call(jedis); + } // ---------