JedisUtils

redis操作。

package com.ane.service.common.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import redis.clients.jedis.JedisCluster;

import com.alibaba.fastjson.JSON;
import com.ane.entity.ScanDataEntity;
import com.ane.service.common.IRedisCommandService;

/**
 * Author: Jeff 
 * Date: 2015年6月23日下午3:59:52 
 * Copyright (c) 2015, HNCF All Rights Reserved.
 * 
 * History: .......
 */

public class RedisCommandServiceImpl implements IRedisCommandService {
	
	@Autowired
	public JedisCluster jedisCluster;
	private static final String mapKey_separate = "&$&";

	@Override
	public String set(String key, Object value, int expire) throws Exception {
		try {
			String result = jedisCluster.set(key, JSON.toJSONString(value));
			if (expire > 0)
				jedisCluster.expire(key, expire);
			return result;
		} catch (Exception e) {
			throw e;
		}

	}

	@Override
	public <T> T get(String key, Class<T> cls) throws Exception {
		try {
			String jsonValue = jedisCluster.get(key);
			return JSON.parseObject(jsonValue, cls);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public Long scard(String key) throws Exception {
		Long count = 0l;
		try {
			count = jedisCluster.scard(key);
		} catch (Exception e) {
			throw e;
		}

		return count;
	}

	@Override
	public Long sadd(String key, Object... members) throws Exception {
		Long rows = 0l;
		try {
			String[] item = new String[members.length];
			int i = 0;
			for (Object obj : members) {
				item[i] = JSON.toJSONString(obj);
				i++;
			}
			rows = jedisCluster.sadd(key, item);
		} catch (Exception e) {
			throw e;
		}

		return rows;
	}

	@Override
	public Long srem(String key, Object... members) throws Exception {
		Long rows = 0l;
		try {
			String[] item = new String[members.length];
			int i = 0;
			for (Object obj : members) {
				item[i] = JSON.toJSONString(obj);
				i++;
			}
			rows = jedisCluster.srem(key, item);
		} catch (Exception e) {
			throw e;
		}
		return rows;
	}

	@Override
	public <T> Set<T> smembers(String key, Class<T> t) throws Exception {
		try {
			Set<String> sItem = jedisCluster.smembers(key);

			Set<T> result = new HashSet<T>();
			Iterator<String> rt = sItem.iterator();
			while (rt.hasNext()) {
				result.add(JSON.parseObject(rt.next(), t));
			}
			return result;
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public Long sremove(String key) throws Exception {
		Long result = 0L;
		try {
			result = jedisCluster.del(key);
		} catch (Exception e) {
			throw e;
		}
		return result;
	}

	// -------------------map--------------------------
	@Override
	public void setMap(String key, String mapKey, Object value)
			throws Exception {
		try {
			String jsonValue = JSON.toJSONString(value);
			jedisCluster.hset(key, mapKey, jsonValue);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public void setMapList(String key, String mapKey, List<?> list)
			throws Exception {
		try {
			jedisCluster.hset(key, mapKey, JSON.toJSONString(list));
		} catch (Exception e) {
			throw e;
		}		
	}

	@Override
	public <T> T getMapValue(String key, String mapKey, Class<T> t)
			throws Exception {
		List<String> list = null;
		try {

			list = jedisCluster.hmget(key, mapKey);
		} catch (Exception e) {
			throw e;
		}
		if (list == null || list.size() == 0) {
			return null;
		}

		String value = list.get(0);
		if (value != null) {
			return JSON.parseObject(value, t);
		}
		return null;
	}

	
	
	@Override
	public String getMapValue(String key, String mapKey) throws Exception {
		// TODO Auto-generated method stub
		String value = null;
		try {
			value = jedisCluster.hget(key, mapKey);
		} catch (Exception e) {
			throw e;
		}
		return value;
	}

	@Override
	public <T> List<T> getMapValues(String key, Class<T> t) throws Exception {
		List<String> list = new ArrayList<String>();
		List<T> rList = null;

		try {

			list = jedisCluster.hvals(key);
		} catch (Exception e) {
			throw e;
		}

		if (list != null && list.size()>0) {
			rList = new ArrayList<T>();
			for (int i = 0; i < list.size(); i++) {
				rList.add(JSON.parseObject(list.get(i), t));
			}
		}

		return rList;
	}

	@Override
	public Long removeMap(String key, String valueKey)  {
		Long result = 0L;

		try {
			result = jedisCluster.hdel(key, valueKey);
		} catch (Exception e) {
		    e.printStackTrace();
		}
		return result;
	}

	@Override
	public Map<String, String> getMaps(String key) throws Exception {

		Map<String, String> rList = new HashMap<String, String>();
		try {

			rList = jedisCluster.hgetAll(key);
		} catch (Exception e) {
			throw new Exception(e);
		}
		return rList;
	}

	// -------------------common--------------------------
	@Override
	public Long remove(String name) throws Exception {
		Long result = 0L;

		try {

			result = jedisCluster.del(name);
		} catch (Exception e) {
			throw e;
		}
		return result;
	}

	@Override
	public boolean exists(String key) throws Exception {

		try {

			return jedisCluster.exists(key);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public Long expire(String key, int seconds) throws Exception {
		try {
			return jedisCluster.expire(key, seconds);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public String type(String key) throws Exception {
		try {
			
			return jedisCluster.type(key);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public Long rpush(String key, String str) {
		try {
			return jedisCluster.rpush(key, str);
		} catch (Exception e) {
			throw e;
		}
		
		
	}

	@Override
	public String rpop(String key) {
		try {
			return jedisCluster.rpop(key);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public String lpop(String key) {
		try {
			return jedisCluster.lpop(key);
		} catch (Exception e) {
			throw e;
		}
	}
	
	@Override
	public List<String> lrange(String key) throws Exception {
		try {
			return jedisCluster.lrange(key, 0, -1);
		} catch (Exception e) {
			throw e;
		}
	}
	
	@Override
	public void setBatchMap(String key, List<ScanDataEntity> list) throws Exception {
		try {
			for(ScanDataEntity s : list) {
				if(s != null) {
					jedisCluster.hset(key, s.getSubBillCode()+mapKey_separate+s.getScanType()+mapKey_separate+s.getTaskCode(), JSON.toJSONString(s));
				}
			}
		} catch (Exception e) {
			throw e;
		}
	}

	
	
}

package com.ane.service.common;  

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.ane.entity.ScanDataEntity;

 /**  
 * Author: Jeff
 * Date: 2015年6月23日上午11:12:30  
 * Copyright (c) 2015, HNCF All Rights Reserved.  
 *  
 * History: 
 * .......
 */
public interface IRedisCommandService {
	//----------------String-------------------
	/**
	 * 给一个 KEY 设置一个 值 ,并且设置有效时间
	 * @param key 存储的KEY
	 * @param value 存储KEY的值
	 * @param expire 有效时间
	 * @return 操作成功完成时返回 OK
	 * @throws Exception 异常
	 */
	public String set(String key, Object value,int expire ) throws Exception;
	/**
	 * 获取 KEY对应的值
	 * @param key 查询的KEY
	 * @param cls 返回的对象类型
	 * @return
	 * @throws Exception 异常
	 */
	public <T> T get(String key, Class<T> cls) throws Exception;

	
	//--------------Set-----------------------
	/**
	 * 返回Set中成员的数量,如果该Key并不存在,返回0。
	 * 
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public Long scard(String key) throws Exception;

	/**
	 * 如果在插入的过程用,参数中有的成员在Set中已经存在,该成员将被忽略,而其它成员仍将会被正常插入。
	 * 如果执行该命令之前,该Key并不存在,该命令将会创建一个新的Set,此后再将参数中的成员陆续插入。
	 * 如果该Key的Value不是Set类型,该命令将返回相关的错误信息。
	 * 
	 * @param key
	 * @param members
	 * @return 本次操作实际插入的成员数量。
	 * @throws Exception 
	 */
	public Long sadd(String key, Object... members) throws Exception;

	/**
	 * 从与Key关联的Set中删除参数中指定的成员,不存在的参数成员将被忽略,如果该Key并不存在,将视为空Set处理。
	 * 
	 * @param key
	 * @param members
	 * @return 从Set中实际移除的成员数量,如果没有则返回0。
	 * @throws Exception 
	 */
	public Long srem(String key, Object... members) throws Exception;

	/**
	 * 查询set 里面的成员
	 * 
	 * @param key
	 * @throws Exception 
	 */
	public <T> Set<T> smembers(String key,Class<T> t ) throws Exception;

	/**
	 * 删除 set key的数据
	 * @throws Exception 
	 */
	public Long sremove(String key) throws Exception;
	
	//--------------Map-----------------------
	/**
	 *  Map集合存储值(为MAP新增key-value)
	 * @param key map的key
	 * @param mapKey map里面value对应的key
	 * @param value 要存储的值
	 * @throws Exception 异常
	 */
	public void setMap(String key, String mapKey, Object value) throws Exception;

	/**
	 *  Map存储List(map(key,List))
	 * @param key map的key
	 * @param mapKey map里面value对应的key
	 * @param value 要存储的值
	 * @throws Exception 异常
	 */
	public void setMapList(String key, String mapKey, List<?> list) throws Exception;
	
	/**
	 * 获取缓存中,map集合中mapkey存放的对象
	 * @param name  以对象形式存储的名字
	 * @param mapKey map中key值
	 * @param t 返回实体对象类型
	 * @throws Exception
	 */
	public <T> T getMapValue(String key, String mapKey, Class<T> t) throws Exception;

	public String getMapValue(String key, String mapKey) throws Exception;
	/**
	 * 获取缓存中,map集合中的值
	 * @param name 以对象形式存储的名字
	 * @param t 返回实体对象类型
	 * @throws Exception
	 */
	public <T> List<T> getMapValues(String key, Class<T> t) throws Exception;
	
	/**
	 * 删除 map里面的某一个值
	 * @param key map的外层key
	 * @param valueKey 值对应的key
	 * @return
	 * @throws Exception
	 */
	public Long removeMap(String key, String valueKey) ;
	
	/**
	 * 获取 map里面 所有 key对应的 value
	 * @param name 以对象形式存储的key
	 * @throws Exception 
	 */
	public Map<String, String> getMaps(String key) throws Exception;
	
	//--------------List----------------------
	
	//--------------common--------------------
	/**
	 * 删除 KEY -----此处可以删除 任意数据类型的KEY数据
	 * @param key 要删除的KEY
	 * @return 影响的数据行
	 * @throws Exception 异常
	 */
	public Long remove(String key) throws Exception;
	/**
	 * 检测 KEY在缓存中是否存在
	 * @param key 检测的KEY
	 * @return
	 * @throws Exception
	 */
	public boolean exists(String key) throws Exception;
	/**
	 * 设置有效期
	 * @param key 有效期的key
	 * @param seconds 有效时间 秒
	 * @return 影响行
	 */
	public Long expire(String key,int seconds) throws Exception;
    
	/**
	 * 
	* @Title: type 
	* @Description: 返回 key 所储存的值的类型。
	* @param @param key
	* @param @return
	* @param @throws Exception 
	* @return String 	none (key不存在)
						string (字符串)
						list (列表)
						set (集合)
						zset (有序集)
						hash (哈希表)
	* @throws
	 */
	public String type(String key)throws Exception;
    
	/**
	 * 往缓存里面从右按顺序加数据     结合rpop 实现先进先出
	 * @param key
	 * @param str
	 * @return
	 */
	public Long rpush(String key,String str);
	
	/**
	 * 从缓存里面按顺序从右边取数据     取出rpush添加的数据    实现先进先出
	 * @param key
	 * @return
	 */
	public String rpop(String key);
	
	/**
	 * 从缓存里面按顺序从左边边取数据     取出rpush添加的数据    实现先进先出
	 * @param key
	 * @return
	 */
	public String lpop(String key);
	
	public void setBatchMap(String key,List<ScanDataEntity> list) throws Exception;
	
	public  List<String> lrange(String key) throws Exception;
	
}
  

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值