com.janeluo.jfinalplus.plugin.redis.ShardRedisPlugin Maven / Gradle / Ivy
/**
* Copyright (c) 2015-2016, BruceZCQ ([email protected]).
*
* 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.janeluo.jfinalplus.plugin.redis;
import com.jfinal.plugin.IPlugin;
import com.jfinal.plugin.redis.IKeyNamingPolicy;
import com.jfinal.plugin.redis.serializer.FstSerializer;
import com.jfinal.plugin.redis.serializer.ISerializer;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedisPool;
import java.util.ArrayList;
import java.util.List;
/**
* ShardRedisPlugin.
*/
public class ShardRedisPlugin implements IPlugin {
private List shards = new ArrayList();
private ISerializer serializer = null;
private IKeyNamingPolicy keyNamingPolicy = null;
private JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
public ShardRedisPlugin(List shards) {
this.shards = shards;
}
public boolean addShard(JedisShardInfo shard) {
if (shard == null) {
throw new IllegalArgumentException("want add a null shard");
}
for (JedisShardInfo jedisShardInfo : this.shards) {
if (jedisShardInfo.getHost().equals(shard.getHost())
&& jedisShardInfo.getPort() == shard.getPort()) {
return true;
}
}
return this.shards.add(shard);
}
public boolean start() {
ShardedJedisPool jedisPool = new ShardedJedisPool(jedisPoolConfig, this.shards);;
if (serializer == null)
serializer = FstSerializer.me;
if (keyNamingPolicy == null)
keyNamingPolicy = IKeyNamingPolicy.defaultKeyNamingPolicy;
ShardCache cache = new ShardCache(jedisPool, serializer, keyNamingPolicy);
ShardRedis.setShardCache(cache);
return true;
}
public boolean stop() {
ShardRedis.shardCache.jedisPool.destroy();
return true;
}
/**
* 当RedisPlugin 提供的设置属性仍然无法满足需求时,通过此方法获取到
* JedisPoolConfig 对象,可对 redis 进行更加细致的配置
*
* 例如:
* redisPlugin.getJedisPoolConfig().setMaxTotal(100);
*
*/
public JedisPoolConfig getJedisPoolConfig() {
return jedisPoolConfig;
}
// ---------
public void setSerializer(ISerializer serializer) {
this.serializer = serializer;
}
public void setKeyNamingPolicy(IKeyNamingPolicy keyNamingPolicy) {
this.keyNamingPolicy = keyNamingPolicy;
}
// ---------
public void setTestWhileIdle(boolean testWhileIdle) {
jedisPoolConfig.setTestWhileIdle(testWhileIdle);
}
public void setMinEvictableIdleTimeMillis(int minEvictableIdleTimeMillis) {
jedisPoolConfig.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
}
public void setTimeBetweenEvictionRunsMillis(int timeBetweenEvictionRunsMillis) {
jedisPoolConfig.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
}
public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) {
jedisPoolConfig.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy