- 浏览: 167098 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (212)
- Java基础 (67)
- Js基础 (14)
- NoSQL (7)
- Spring (34)
- sql (8)
- JMS (1)
- Redis (8)
- Flex (5)
- java线程 (2)
- Linux (8)
- Hibernate (3)
- maven (12)
- bug (1)
- node.js (3)
- servlet (7)
- 设计模式 (6)
- struts (1)
- bootstrap3.js (2)
- cxf (1)
- Thymeleaf (2)
- HTML (1)
- parsley.js (1)
- jquery (1)
- restful (4)
- eclipse&Idea (3)
- Spring boot (2)
- Http (1)
- DB (3)
- Jboss (1)
- web (1)
- rpc (3)
- zookeeper (1)
- 杂谈 (4)
- angularjs (2)
- selenium2.0 (1)
- Mybatis (6)
- MySql (7)
- 数据结构 (1)
- Nginx (2)
最新评论
-
wangyudong:
由Spring Boot实现的微服务需要有比较好的工具去测试R ...
spring boot 启动 -
arsemilan:
很实用的sql,面试必备
sql 简单应用
package com.hoss.common.cache;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import cn.sina.api.commons.util.StatLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.ZParams;
import redis.clients.jedis.exceptions.JedisException;
import cn.sina.api.commons.redis.jedis.JedisHAServer;
import cn.sina.api.commons.redis.jedis.JedisPort;
import com.hoss.common.config.SystemConfigFactory;
import com.hoss.common.config.SystemConfigService;
import com.hoss.common.model.CommonSystemConfig;
import com.hoss.common.util.SpringContextUtils;
public class RedisManager {
private static Logger logger = LoggerFactory.getLogger("RedisManager");
private static final JedisHAServer server = new JedisHAServer();
private static JedisPort first;
private static JedisPort second;
private RedisManager() {
}
public static JedisHAServer getHaServer(){
if( !server.isAlive() ){
init();
}
return server;
}
/**
* 初始化redis
*/
private synchronized static void init(){
if( !server.isAlive() ){
StatLog.setPausePrint(true); //关闭redis监控日志打印
try{
SystemConfigService systemConfigService= SpringContextUtils.getBean(SystemConfigService.class);
String servers= systemConfigService.findSystemConfigValueByKeyFormDB(CommonSystemConfig.class, "redis");
JedisPoolConfig jedisPoolConfig= new JedisPoolConfig();
jedisPoolConfig.setTestWhileIdle(false);
server.setPoolConfig(jedisPoolConfig);
server.setServers(initialServerAddressesS(servers));
server.setDoubleWrite(true);
first = server.getFirst();
second = server.getSecond();
logger.info("初始化 redis server addr=" + servers);
}catch (Exception e){
e.printStackTrace();
logger.error("初始化 redis server error");
}
}
}
private static List<String> initialServerAddressesS(String servers) {
if(servers == null || servers.trim().length()<1){
logger.warn("RedisManager init error servers is empty");
}
List<String> addrs = new ArrayList<String>();
String[] hostIps = servers.split(",");
System.out.println(hostIps.length);
for (int i = 0; i < hostIps.length; i++) {
addrs.add(hostIps[i]);
}
return addrs;
}
public static Long zadd(String key, double score, String member){
Long result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zadd(key, score, member);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
if (result != null) {
result = jedis.zadd(key, score, member);
second.getCustomPool().returnResource(jedis);
}
} else {
result2 = jedis.zadd(key, score, member);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static Long zadd(byte[] key, double score, byte member[]){
Long result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zadd(key, score, member);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
if (result != null) {
result = jedis.zadd(key, score, member);
second.getCustomPool().returnResource(jedis);
}
} else {
result2 = jedis.zadd(key, score, member);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static Long zunionstore(String dstkey, String... sets){
Long result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zunionstore(dstkey, sets);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
if (result != null) {
result = jedis.zunionstore(dstkey, sets);
second.getCustomPool().returnResource(jedis);
}
} else {
result2 = jedis.zunionstore(dstkey, sets);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static Long zunionstore(String dstkey, ZParams params, String... sets){
Long result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zunionstore(dstkey,params, sets);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
if (result != null) {
result = jedis.zunionstore(dstkey, params,sets);
second.getCustomPool().returnResource(jedis);
}
} else {
result2 = jedis.zunionstore(dstkey, params, sets);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static Long zunionstore(byte[] dstkey, byte[]... sets){
Long result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zunionstore(dstkey, sets);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
if (result != null) {
result = jedis.zunionstore(dstkey, sets);
second.getCustomPool().returnResource(jedis);
}
} else {
result2 = jedis.zunionstore(dstkey, sets);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static Long zunionstore(byte[] dstkey, ZParams params, byte[]... sets){
Long result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zunionstore(dstkey,params, sets);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
if (result != null) {
result = jedis.zunionstore(dstkey, params,sets);
second.getCustomPool().returnResource(jedis);
}
} else {
result2 = jedis.zunionstore(dstkey, params, sets);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static Long zrem(byte[] dstkey, byte[] sets){
Long result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zrem(dstkey, sets);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
if (result != null) {
result = jedis.zrem(dstkey, sets);
second.getCustomPool().returnResource(jedis);
}
} else {
result2 = jedis.zrem(dstkey, sets);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static Long zrem(String dstkey, String sets){
Long result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zrem(dstkey, sets);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
if (result != null) {
result = jedis.zrem(dstkey,sets);
second.getCustomPool().returnResource(jedis);
}
} else {
result2 = jedis.zrem(dstkey, sets);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static Set<String> zrange(String key, int start, int end){
Set<String> result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zrange(key, start, end);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
if (result != null) {
result = jedis.zrange(key, start, end);
second.getCustomPool().returnResource(jedis);
}
} else {
result2 = jedis.zrange(key, start, end);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static Set<byte[]> zrange(byte[] key, int start, int end){
Set<byte[]> result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zrange(key, start, end);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
if (result != null) {
result = jedis.zrange(key, start, end);
second.getCustomPool().returnResource(jedis);
}
} else {
result2 = jedis.zrange(key, start, end);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static Set<byte[]> zrevrange(byte[] key, int start, int end){
Set<byte[]> result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zrevrange(key, start, end);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
if (result != null) {
result = jedis.zrevrange(key, start, end);
second.getCustomPool().returnResource(jedis);
}
} else {
result2 = jedis.zrevrange(key, start, end);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static Set<String> zrevrange(String key, int start, int end){
Set<String> result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zrevrange(key, start, end);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
if (result != null) {
result = jedis.zrevrange(key, start, end);
second.getCustomPool().returnResource(jedis);
}
} else {
result2 = jedis.zrevrange(key, start, end);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static Set<String> zrangeByScore(String key, int min, int max){
Set<String> result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zrangeByScore(key, min, max);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
if (result != null) {
result = jedis.zrangeByScore(key, min, max);
second.getCustomPool().returnResource(jedis);
}
} else {
result2 = jedis.zrangeByScore(key, min, max);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static Set<byte[]> zrangeByScore(byte[] key, int start, int end){
Set<byte[]> result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zrangeByScore(key, start, end);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
if (result != null) {
result = jedis.zrangeByScore(key, start, end);
second.getCustomPool().returnResource(jedis);
}
} else {
result2 = jedis.zrangeByScore(key, start, end);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static double zincrby(byte[] key, double score, byte[] member){
double result=0.0, result2=0.0;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zincrby(key, score, member);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
jedis.zincrby(key, score, member);
second.getCustomPool().returnResource(jedis);
} else {
result2 = jedis.zincrby(key, score, member);
}
second.getCustomPool().returnResource(jedis);
}
if (Double.valueOf("0.000001").doubleValue() < result) {
return result;
}
if (Double.valueOf("0.000001").doubleValue() < result2) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static double zincrby(String key, double score, String member){
double result=0.0, result2=0.0;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zincrby(key, score, member);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
jedis.zincrby(key, score, member);
second.getCustomPool().returnResource(jedis);
} else {
result2 = jedis.zincrby(key, score, member);
}
second.getCustomPool().returnResource(jedis);
}
if (Double.valueOf("0.000001").doubleValue() < result) {
return result;
}
if (Double.valueOf("0.000001").doubleValue() < result2) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static Long expire(String key, int sec){
Long result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.expire(key, sec);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
result2 = jedis.expire(key, sec);
second.getCustomPool().returnResource(jedis);
} else {
result2 = jedis.expire(key, sec);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
}
package com.hoss.common.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* 获取 spring bean 的工具类
* @author linxiaohui
*
*/
@Component
public class SpringContextUtils implements ApplicationContextAware {
private static final Logger logger= LoggerFactory.getLogger(SpringContextUtils.class);
private static ApplicationContext context;
public void setApplicationContext(ApplicationContext context) throws BeansException {
SpringContextUtils.context = context;
logger.info("---------SpringContextUtils 启动完成--------------------");
}
public static <T> T getBean(String beanId, Class<T> clazz) {
return context.getBean(beanId, clazz);
}
public static <T> T getBean(Class<T> clazz) {
return context.getBean(clazz);
}
public static ApplicationContext getContext() {
return context;
}
}
package com.hoss.common.config;
import com.hoss.common.exception.SystemConfigNotFoundException;
import java.util.List;
/**
* Created by linxiaohui on 14-5-20.
*/
public interface SystemConfigService {
/**
* 根据key获取 配置
* 先从缓存服务器中获取,缓存服务器中不存在去数据库中获取(当 数据库中存在,缓存中不存在,同步到缓存)
*
* @param cls
* @param key
* @return
*/
SystemConfigDto findSystemConfigByKey(Class<? extends SystemConfig> cls,String key) throws SystemConfigNotFoundException;
/**
* 根据key获取 配置
* 从数据库中查询
*
* @param cls
* @param key
* @return
*/
SystemConfigDto findSystemConfigByKeyFormDB(Class<? extends SystemConfig> cls,String key) throws SystemConfigNotFoundException;
/**
* 根据key获取 配置的数
* 先从缓存服务器中获取,缓存服务器中不存在去数据库中获取(当 数据库中存在,缓存中不存在,同步到缓存)
*
* @param cls
* @param key
* @return
*/
String findSystemConfigValueByKey(Class<? extends SystemConfig> cls,String key) throws SystemConfigNotFoundException;
/**
* 根据key获取 配置的数
* 从数据库中查询
*
* @param cls
* @param key
* @return
*/
String findSystemConfigValueByKeyFormDB(Class<? extends SystemConfig> cls,String key) throws SystemConfigNotFoundException;
/**
* 根据key获取 配置的数
* 从数据库中查询(数据库中不存在,把默认值插入到数据库中)
*
* @param cls
* @param key
* @return
*/
String findSystemConfigValueByKeyFormDB(Class<? extends SystemConfig> cls,String key,String defaultValue) ;
/**
* 从数据库中获取某一个类型的配置
* @param cls
* @return
*/
List<SystemConfigDto> findAllFormDB(Class<? extends SystemConfig> cls);
/**
* 增加一个配置到数据库并同步到缓存服务器
* @param systemConfig
* @return
*/
SystemConfigDto addSystemConfig(SystemConfig systemConfig);
/**
* 更新一个配置到数据库并同步到缓存服务器
* @param systemConfig
* @return
*/
SystemConfigDto updateSystemConfig(SystemConfig systemConfig);
/**
* 增加一个配置到数据库
* @param systemConfig
* @return
*/
void saveSystemConfig(SystemConfig systemConfig);
}
package com.hoss.common.model;
import com.hoss.common.config.SystemConfig;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
/**
* Created by linxiaohui on 14-5-20.
*/
@Entity
@DiscriminatorValue(value = "common_config")
public class CommonSystemConfig extends SystemConfig {
public CommonSystemConfig(){
}
public CommonSystemConfig(String key,String value){
super(key,value);
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import cn.sina.api.commons.util.StatLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.ZParams;
import redis.clients.jedis.exceptions.JedisException;
import cn.sina.api.commons.redis.jedis.JedisHAServer;
import cn.sina.api.commons.redis.jedis.JedisPort;
import com.hoss.common.config.SystemConfigFactory;
import com.hoss.common.config.SystemConfigService;
import com.hoss.common.model.CommonSystemConfig;
import com.hoss.common.util.SpringContextUtils;
public class RedisManager {
private static Logger logger = LoggerFactory.getLogger("RedisManager");
private static final JedisHAServer server = new JedisHAServer();
private static JedisPort first;
private static JedisPort second;
private RedisManager() {
}
public static JedisHAServer getHaServer(){
if( !server.isAlive() ){
init();
}
return server;
}
/**
* 初始化redis
*/
private synchronized static void init(){
if( !server.isAlive() ){
StatLog.setPausePrint(true); //关闭redis监控日志打印
try{
SystemConfigService systemConfigService= SpringContextUtils.getBean(SystemConfigService.class);
String servers= systemConfigService.findSystemConfigValueByKeyFormDB(CommonSystemConfig.class, "redis");
JedisPoolConfig jedisPoolConfig= new JedisPoolConfig();
jedisPoolConfig.setTestWhileIdle(false);
server.setPoolConfig(jedisPoolConfig);
server.setServers(initialServerAddressesS(servers));
server.setDoubleWrite(true);
first = server.getFirst();
second = server.getSecond();
logger.info("初始化 redis server addr=" + servers);
}catch (Exception e){
e.printStackTrace();
logger.error("初始化 redis server error");
}
}
}
private static List<String> initialServerAddressesS(String servers) {
if(servers == null || servers.trim().length()<1){
logger.warn("RedisManager init error servers is empty");
}
List<String> addrs = new ArrayList<String>();
String[] hostIps = servers.split(",");
System.out.println(hostIps.length);
for (int i = 0; i < hostIps.length; i++) {
addrs.add(hostIps[i]);
}
return addrs;
}
public static Long zadd(String key, double score, String member){
Long result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zadd(key, score, member);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
if (result != null) {
result = jedis.zadd(key, score, member);
second.getCustomPool().returnResource(jedis);
}
} else {
result2 = jedis.zadd(key, score, member);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static Long zadd(byte[] key, double score, byte member[]){
Long result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zadd(key, score, member);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
if (result != null) {
result = jedis.zadd(key, score, member);
second.getCustomPool().returnResource(jedis);
}
} else {
result2 = jedis.zadd(key, score, member);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static Long zunionstore(String dstkey, String... sets){
Long result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zunionstore(dstkey, sets);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
if (result != null) {
result = jedis.zunionstore(dstkey, sets);
second.getCustomPool().returnResource(jedis);
}
} else {
result2 = jedis.zunionstore(dstkey, sets);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static Long zunionstore(String dstkey, ZParams params, String... sets){
Long result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zunionstore(dstkey,params, sets);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
if (result != null) {
result = jedis.zunionstore(dstkey, params,sets);
second.getCustomPool().returnResource(jedis);
}
} else {
result2 = jedis.zunionstore(dstkey, params, sets);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static Long zunionstore(byte[] dstkey, byte[]... sets){
Long result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zunionstore(dstkey, sets);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
if (result != null) {
result = jedis.zunionstore(dstkey, sets);
second.getCustomPool().returnResource(jedis);
}
} else {
result2 = jedis.zunionstore(dstkey, sets);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static Long zunionstore(byte[] dstkey, ZParams params, byte[]... sets){
Long result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zunionstore(dstkey,params, sets);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
if (result != null) {
result = jedis.zunionstore(dstkey, params,sets);
second.getCustomPool().returnResource(jedis);
}
} else {
result2 = jedis.zunionstore(dstkey, params, sets);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static Long zrem(byte[] dstkey, byte[] sets){
Long result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zrem(dstkey, sets);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
if (result != null) {
result = jedis.zrem(dstkey, sets);
second.getCustomPool().returnResource(jedis);
}
} else {
result2 = jedis.zrem(dstkey, sets);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static Long zrem(String dstkey, String sets){
Long result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zrem(dstkey, sets);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
if (result != null) {
result = jedis.zrem(dstkey,sets);
second.getCustomPool().returnResource(jedis);
}
} else {
result2 = jedis.zrem(dstkey, sets);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static Set<String> zrange(String key, int start, int end){
Set<String> result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zrange(key, start, end);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
if (result != null) {
result = jedis.zrange(key, start, end);
second.getCustomPool().returnResource(jedis);
}
} else {
result2 = jedis.zrange(key, start, end);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static Set<byte[]> zrange(byte[] key, int start, int end){
Set<byte[]> result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zrange(key, start, end);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
if (result != null) {
result = jedis.zrange(key, start, end);
second.getCustomPool().returnResource(jedis);
}
} else {
result2 = jedis.zrange(key, start, end);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static Set<byte[]> zrevrange(byte[] key, int start, int end){
Set<byte[]> result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zrevrange(key, start, end);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
if (result != null) {
result = jedis.zrevrange(key, start, end);
second.getCustomPool().returnResource(jedis);
}
} else {
result2 = jedis.zrevrange(key, start, end);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static Set<String> zrevrange(String key, int start, int end){
Set<String> result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zrevrange(key, start, end);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
if (result != null) {
result = jedis.zrevrange(key, start, end);
second.getCustomPool().returnResource(jedis);
}
} else {
result2 = jedis.zrevrange(key, start, end);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static Set<String> zrangeByScore(String key, int min, int max){
Set<String> result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zrangeByScore(key, min, max);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
if (result != null) {
result = jedis.zrangeByScore(key, min, max);
second.getCustomPool().returnResource(jedis);
}
} else {
result2 = jedis.zrangeByScore(key, min, max);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static Set<byte[]> zrangeByScore(byte[] key, int start, int end){
Set<byte[]> result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zrangeByScore(key, start, end);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
if (result != null) {
result = jedis.zrangeByScore(key, start, end);
second.getCustomPool().returnResource(jedis);
}
} else {
result2 = jedis.zrangeByScore(key, start, end);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static double zincrby(byte[] key, double score, byte[] member){
double result=0.0, result2=0.0;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zincrby(key, score, member);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
jedis.zincrby(key, score, member);
second.getCustomPool().returnResource(jedis);
} else {
result2 = jedis.zincrby(key, score, member);
}
second.getCustomPool().returnResource(jedis);
}
if (Double.valueOf("0.000001").doubleValue() < result) {
return result;
}
if (Double.valueOf("0.000001").doubleValue() < result2) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static double zincrby(String key, double score, String member){
double result=0.0, result2=0.0;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.zincrby(key, score, member);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
jedis.zincrby(key, score, member);
second.getCustomPool().returnResource(jedis);
} else {
result2 = jedis.zincrby(key, score, member);
}
second.getCustomPool().returnResource(jedis);
}
if (Double.valueOf("0.000001").doubleValue() < result) {
return result;
}
if (Double.valueOf("0.000001").doubleValue() < result2) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
public static Long expire(String key, int sec){
Long result = null, result2 = null;
Jedis jedis = null;
if (first != null && first.isAlive()) {
jedis = first.getCustomPool().getResource();
result = jedis.expire(key, sec);
first.getCustomPool().returnResource(jedis);
}
if (getHaServer().isDoubleWrite() && second != null && second.isAlive()) {
jedis = second.getCustomPool().getResource();
if (getHaServer().isSetSecond()) {
result2 = jedis.expire(key, sec);
second.getCustomPool().returnResource(jedis);
} else {
result2 = jedis.expire(key, sec);
}
second.getCustomPool().returnResource(jedis);
}
if (result != null) {
return result;
}
if (result2 != null) {
return result2;
}
throw new JedisException("redis server all dead: " + first + " " + second);
}
}
package com.hoss.common.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* 获取 spring bean 的工具类
* @author linxiaohui
*
*/
@Component
public class SpringContextUtils implements ApplicationContextAware {
private static final Logger logger= LoggerFactory.getLogger(SpringContextUtils.class);
private static ApplicationContext context;
public void setApplicationContext(ApplicationContext context) throws BeansException {
SpringContextUtils.context = context;
logger.info("---------SpringContextUtils 启动完成--------------------");
}
public static <T> T getBean(String beanId, Class<T> clazz) {
return context.getBean(beanId, clazz);
}
public static <T> T getBean(Class<T> clazz) {
return context.getBean(clazz);
}
public static ApplicationContext getContext() {
return context;
}
}
package com.hoss.common.config;
import com.hoss.common.exception.SystemConfigNotFoundException;
import java.util.List;
/**
* Created by linxiaohui on 14-5-20.
*/
public interface SystemConfigService {
/**
* 根据key获取 配置
* 先从缓存服务器中获取,缓存服务器中不存在去数据库中获取(当 数据库中存在,缓存中不存在,同步到缓存)
*
* @param cls
* @param key
* @return
*/
SystemConfigDto findSystemConfigByKey(Class<? extends SystemConfig> cls,String key) throws SystemConfigNotFoundException;
/**
* 根据key获取 配置
* 从数据库中查询
*
* @param cls
* @param key
* @return
*/
SystemConfigDto findSystemConfigByKeyFormDB(Class<? extends SystemConfig> cls,String key) throws SystemConfigNotFoundException;
/**
* 根据key获取 配置的数
* 先从缓存服务器中获取,缓存服务器中不存在去数据库中获取(当 数据库中存在,缓存中不存在,同步到缓存)
*
* @param cls
* @param key
* @return
*/
String findSystemConfigValueByKey(Class<? extends SystemConfig> cls,String key) throws SystemConfigNotFoundException;
/**
* 根据key获取 配置的数
* 从数据库中查询
*
* @param cls
* @param key
* @return
*/
String findSystemConfigValueByKeyFormDB(Class<? extends SystemConfig> cls,String key) throws SystemConfigNotFoundException;
/**
* 根据key获取 配置的数
* 从数据库中查询(数据库中不存在,把默认值插入到数据库中)
*
* @param cls
* @param key
* @return
*/
String findSystemConfigValueByKeyFormDB(Class<? extends SystemConfig> cls,String key,String defaultValue) ;
/**
* 从数据库中获取某一个类型的配置
* @param cls
* @return
*/
List<SystemConfigDto> findAllFormDB(Class<? extends SystemConfig> cls);
/**
* 增加一个配置到数据库并同步到缓存服务器
* @param systemConfig
* @return
*/
SystemConfigDto addSystemConfig(SystemConfig systemConfig);
/**
* 更新一个配置到数据库并同步到缓存服务器
* @param systemConfig
* @return
*/
SystemConfigDto updateSystemConfig(SystemConfig systemConfig);
/**
* 增加一个配置到数据库
* @param systemConfig
* @return
*/
void saveSystemConfig(SystemConfig systemConfig);
}
package com.hoss.common.model;
import com.hoss.common.config.SystemConfig;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
/**
* Created by linxiaohui on 14-5-20.
*/
@Entity
@DiscriminatorValue(value = "common_config")
public class CommonSystemConfig extends SystemConfig {
public CommonSystemConfig(){
}
public CommonSystemConfig(String key,String value){
super(key,value);
}
}
- api-commons-2.1.jar (364.4 KB)
- 下载次数: 1
发表评论
-
Redis高可用部署及监控
2015-03-24 18:54 449一、 Redis Se ... -
Redis与Memcached
2015-03-24 16:10 362实际MySQL是适合进行海量数据存储的,通过Memcach ... -
redis的应用场景
2015-03-24 16:06 3421.取最新N个数据的操作 ... -
redis持久化、主从复制、简单的事务支持及发布订阅功能
2015-03-24 16:05 466持久化 •redis是一个支持持久化的内存数据库,也就是说 ... -
Redis的持久化机制
2015-03-24 11:32 614Redis由于支持非常丰富的内存数据结构类型,如何把这些复杂 ... -
MembaseManager
2014-07-12 12:00 318/** * Created by Administrator ...
相关推荐
为了方便管理和操作Redis服务器,出现了各种Redis管理工具,如`redismanager`和`RedisDesktopManager`。 **1. Redismanager:一体化Redis操作平台** `redismanager` 是一个专为Redis设计的操作工具,它提供了一个...
本文将详细介绍“redismanager.zip”提供的Redis桌面可视化工具——RedisManager,以及如何利用它来管理和操作Redis数据库。 RedisManager是针对Redis数据库的优秀桌面管理工具,它为用户提供了直观且高效的界面,...
Magento-Redismanager, 在 Magento,用于Redis的超级用户控制 Redis 管理Redis服务的实用程序特性系统> 配置>的自动或者手动配置高级> Redis管理在负载均衡环境中缓存服务不同的理想情况直接从magento面板管理员管理...
Redismanager是一款专为Laravel应用设计的Web管理界面,它使得开发者可以方便地通过图形化方式来管理和操作Redis数据库。Redis是一种高效的键值存储系统,常用于缓存、消息队列以及作为数据存储等场景。Laravel是PHP...
RedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManagerRedisManager
`redismanager.rar`提供的是一款适用于Windows操作系统的Redis客户端工具——Redis Desktop Manager 0.9.3,这也是该工具的最后一个免费版本。 Redis Desktop Manager是一款图形化界面的应用程序,它允许用户通过...
redis可视化工具
Redis Manager 是一款针对Redis缓存管理的工具,它旨在简化Redis缓存的清理工作,使得运维人员无需直接通过命令行执行复杂的Redis操作,而是通过图形化的界面进行更加直观和便捷的管理。这款工具的出现,极大地提升...
RedisManager是一款图形化的Redis管理工具,通常被用来方便地进行Redis实例的连接、数据查看、操作以及配置管理。在这个压缩包中,包含的是Redis 64位Windows安装包(Redis-x64-3.2.100.msi)和RedisDesktopManager...
Redis Manager 是一款专为 Redis 数据库设计的可视化操作工具,旨在提供更为直观和便捷的数据管理体验。通过这个工具,用户可以高效地进行 Redis 数据的查看、添加、编辑和删除等操作,大大简化了 Redis 的日常管理...
使用Redis存放Session RedisManager
**Redis Desktop Manager**,简称RDM,是一款专为Redis数据库设计的桌面管理工具,它提供了直观、用户友好的图形化界面,使得Redis数据库的管理和操作变得更加便捷。这款工具跨平台,支持Windows、Mac OS X以及Linux...
RedisManager是一款针对Redis数据库的高级桌面管理工具,其版本号为1.3.5,相较于其他如RedisPlus等客户端,据称在易用性和功能上有所优势。此压缩包包含的主要文件是`RedisManager.1.3.5.exe`,这应该是一个可以...
本项目"基于java的使用Redis存放Session RedisManager.zip"就是针对这一需求的一个实现。 Redis作为缓存数据库,支持高速读写操作,特别适合处理Session这种频繁访问的数据。通过将Session数据存储在Redis中,可以...
本项目“基于Java的使用Redis存放Session RedisManager”就是针对这一需求提供的一种解决方案,利用Redis作为分布式缓存系统来存储和管理用户的Session数据。下面我们将详细探讨这个项目的实现原理、优势以及相关...
**Redis Manager 0.98 知识点详解** Redis Manager 0.98 是一个专为Redis数据库设计的GUI(图形用户界面)管理工具,主要用于简化 Redis 数据库的管理和操作。这款工具提供了直观的界面,让开发者和系统管理员能够...
标题“使用Redis存放Session RedisManager.7z”和描述中提到的主题是关于使用Redis作为Session存储的解决方案。Redis是一款高性能的键值数据存储系统,常用于缓存、消息队列等场景,而在此处,它被用作Web应用中用户...
Redis Manager 0.9.9 是一款针对Redis数据库的管理工具,主要面向开发人员和系统管理员,便于他们高效地管理和操作Redis实例。由于最新版本不再提供预编译的可执行文件,用户需要自行通过源码进行编译。在这个压缩包...
"java源码:使用Redis存放Session RedisManager.zip"就是一个实例,展示了如何通过Java代码实现这一功能。 首先,Redis是一个开源、高性能的键值存储系统,适合存储字符串、哈希、列表、集合等多种数据结构,特别...
Redis Desktop Manager 版本