优化redis集群工具

This commit is contained in:
zhuangqian 2017-01-05 22:27:07 +08:00
parent 4ebd92bf79
commit 1d408f4f4a
7 changed files with 178 additions and 128 deletions

View File

@ -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<String, Object> findOne(String cacheName, String key, String sql);
Map<String, Object> findOne(String cacheName, String key, String sql, Object modelOrMap);
Map<String, Object> findOneBySqlId(String cacheName, String key, String sqlId);
Map<String, Object> findOneBySqlId(String cacheName, String key, String sqlId, Object modelOrMap);
List<Map<String, Object>> find(String cacheName, String key, String sql);
List<Map<String, Object>> find(String cacheName, String key, String sql, Object modelOrMap);
List<Map<String, Object>> findBySqlId(String cacheName, String key, String sqlId);
List<Map<String, Object>> 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<String, Object> findOne(String cacheName, String key, String sql);
Map<String, Object> findOne(String cacheName, String key, String sql, Object modelOrMap);
Map<String, Object> findOneBySqlId(String cacheName, String key, String sqlId);
Map<String, Object> findOneBySqlId(String cacheName, String key, String sqlId, Object modelOrMap);
List<Map<String, Object>> find(String cacheName, String key, String sql);
List<Map<String, Object>> find(String cacheName, String key, String sql, Object modelOrMap);
List<Map<String, Object>> findBySqlId(String cacheName, String key, String sqlId);
List<Map<String, Object>> findBySqlId(String cacheName, String key, String sqlId, Object modelOrMap);
}

View File

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

View File

@ -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<ContextStoppedEvent> {
@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<ContextClosedEvent> {
@Override
public void onApplicationEvent(ContextClosedEvent event) {
if (event.getApplicationContext().getParent() == null) {
destroyPlugin();
}
}
/**
* 插件的停用
*/
private void destroyPlugin() {
PluginManager.init().stop();
}
}

View File

@ -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关闭成功");
}
}

View File

@ -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> T call(ICallBack call);
/**
* 关闭连接
*/
public void close();
}

View File

@ -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<String> keys(String pattern) {
return null;
Set<String> keys = new HashSet<>();
JedisCluster jedis = getJedis();
Map<String, JedisPool> clusterNodes = jedis.getClusterNodes();
for (JedisPool jedisPool : clusterNodes.values()) {
Jedis resource = jedisPool.getResource();
Set<String> 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> 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() {

View File

@ -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> T call(ICallBack call) {
Jedis jedis = getJedis();
return call.call(jedis);
}
// ---------