(1)Jedis配置文件
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd" default-lazy-init="true"> <context:component-scan base-package="com.crm.redis" /> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!--最大连接数 --> <!--不同的Common Pool 这个属性不一样 --> <property name="maxTotal" value="50" /> <!--最大空闲数 --> <property name="maxIdle" value="10" /> <property name="minIdle" value="1" /> <!--最大等待时间ms --> <property name="maxWaitMillis" value="30000" /> <property name="testOnBorrow" value="true" /> <property name="testOnReturn" value="true" /> <property name="testWhileIdle" value="true" /> </bean> <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool" destroy-method="destroy"> <constructor-arg ref="jedisPoolConfig" /> <constructor-arg> <list> <bean class="redis.clients.jedis.JedisShardInfo"> <constructor-arg value="202.102.83.169" /> <constructor-arg type="int" value="6379" /> <constructor-arg value="instance:01" /> </bean> <!-- <bean class="redis.clients.jedis.JedisShardInfo"> <constructor-arg value="localhost" /> <constructor-arg type="int" value="6379" /> <constructor-arg value="instance:02" /> </bean> --> </list> </constructor-arg> </bean> </beans>
(2)抽象Jedis公共DAO
package com.crm.redis; import java.io.Serializable; import java.util.List; import java.util.Map; import java.util.Set; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import com.crm.redis.util.JedisSerializeUtil; import redis.clients.jedis.ShardedJedis; import redis.clients.jedis.ShardedJedisPool; @Repository("RedisDao") public class RedisDao { @Autowired private ShardedJedisPool shardedJedisPool; // ------------------------------String------------------------------ public void setString(String key, String value) { ShardedJedis jedis = shardedJedisPool.getResource(); jedis.set(key, value); shardedJedisPool.returnResource(jedis); } public String getString(String key) { ShardedJedis jedis = shardedJedisPool.getResource(); String str = jedis.get(key); shardedJedisPool.returnResource(jedis); return str; } public void delString(String key) { ShardedJedis jedis = shardedJedisPool.getResource(); jedis.del(key); shardedJedisPool.returnResource(jedis); } /** * 根据KEY追加值 (1)如果KEY存在的话,在原有值的基础上追加 (2)如果KEY不存在的话,则新增值KEY-VALUE */ public void valueAppend(String key, String appendValue) { ShardedJedis jedis = shardedJedisPool.getResource(); jedis.append(key, appendValue); shardedJedisPool.returnResource(jedis); } // ------------------------------HashMap------------------------------ /** * 根据key设置HashMap */ public void hmset(String key, Map hashMap) { ShardedJedis jedis = shardedJedisPool.getResource(); jedis.hmset(key, hashMap); shardedJedisPool.returnResource(jedis); } /** * 根据key获取Map的所有Key */ public Set<String> hkeys(String key) { ShardedJedis jedis = shardedJedisPool.getResource(); Set<String> r = jedis.hkeys(key); shardedJedisPool.returnResource(jedis); return r; } /** * 根据key获取Map的所有Value */ public List<String> hvals(String key) { ShardedJedis jedis = shardedJedisPool.getResource(); List<String> r = jedis.hvals(key); shardedJedisPool.returnResource(jedis); return r; } /** * 根据key和HashMap的key获取值 */ public List<String> hmget(String key, String hashMapKey) { ShardedJedis jedis = shardedJedisPool.getResource(); List<String> r = jedis.hmget(key, hashMapKey); shardedJedisPool.returnResource(jedis); return r; } // ------------------------------List------------------------------ /** * 根据key设置List */ public void lpush(String key, List<String> list) { ShardedJedis jedis = shardedJedisPool.getResource(); for (String listVal : list) { jedis.lpush(key, listVal); } shardedJedisPool.returnResource(jedis); } /** * 根据key获取list中的值【起始值~终止值】 end:-1表示取得所有 */ public List<String> lrange(String key, long start, long end) { ShardedJedis jedis = shardedJedisPool.getResource(); List<String> r = jedis.lrange(key, start, end); shardedJedisPool.returnResource(jedis); return r; } // // 添加数据 // jedis.lpush("lists", "vector"); // jedis.lpush("lists", "ArrayList"); // jedis.lpush("lists", "LinkedList"); // // 数组长度 // System.out.println(jedis.llen("lists")); // // 排序 // System.out.println(jedis.sort("lists")); // // 字串 // System.out.println(jedis.lrange("lists", 0, 3)); // // 修改列表中单个值 // jedis.lset("lists", 0, "hello list!"); // // 获取列表指定下标的值 // System.out.println(jedis.lindex("lists", 1)); // // 删除列表指定下标的值 // System.out.println(jedis.lrem("lists", 1, "vector")); // // 删除区间以外的数据 // System.out.println(jedis.ltrim("lists", 0, 1)); // // 列表出栈 // System.out.println(jedis.lpop("lists")); // // 整个列表值 // System.out.println(jedis.lrange("lists", 0, -1)); // ------------------------------Object------------------------------ /** * 设置对象 */ public void setObject(String key, Serializable object) { ShardedJedis jedis = shardedJedisPool.getResource(); jedis.set(key.getBytes(), JedisSerializeUtil.serialize(object)); shardedJedisPool.returnResource(jedis); } /** * 获取对象 */ public Object getObject(String key) { ShardedJedis jedis = shardedJedisPool.getResource(); return JedisSerializeUtil.unserialize(jedis.get(key.getBytes())); } }
(3)Jedis Util
package com.crm.redis.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * 序列化就是将一个对象转换为二进制的数据流。 * 这样就可以进行传输,或者保存到文件中。如果一个类的对象要想实现序列化,就必须实现serializable接口。 * 在此接口中没有任何的方法,此接口只是作为一个标识,表示本类的对象具备了序列化的能力而已。 * * 反序列化:将二进制数据流转换成相应的对象。 * 如果想要完成对象的序列化,则还要依靠ObjectOutputStream和ObjectInputStream,前者属于序列化操作,而后者属于反序列化操作。 * */ public class JedisSerializeUtil { private static Log log = LogFactory.getLog(JedisSerializeUtil.class); /** * 序列化 * * @param object * @return */ public static byte[] serialize(Object object) { ObjectOutputStream oos = null; ByteArrayOutputStream baos = null; try { // 序列化 baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); oos.writeObject(object); byte[] bytes = baos.toByteArray(); return bytes; } catch (Exception e) { log.error("Jedis执行序列化异常:"+e.getMessage()); } return null; } /** * 反序列化 * * @param bytes * @return */ public static Object unserialize(byte[] bytes) { ByteArrayInputStream bais = null; try { // 反序列化 bais = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bais); return ois.readObject(); } catch (Exception e) { log.error("Jedis执行反序列化异常:"+e.getMessage()); } return null; } }
(4)测试代码
package com.crm.test; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.crm.model.operator.BOCrmOperator; import com.crm.redis.RedisDao; public class TestRedis { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext-redis.xml"); RedisDao RedisDao = (RedisDao) ac.getBean("RedisDao"); //--------------------String-------------------- RedisDao.setString("hello", "world"); System.out.println(RedisDao.getString("hello")); RedisDao.valueAppend("hello", "-----append"); System.out.println(RedisDao.getString("hello")); RedisDao.valueAppend("NOT_EXIST", "NOT_EXIST_VALUE"); System.out.println(RedisDao.getString("NOT_EXIST")); //--------------------Map-------------------- Map h = new HashMap(); h.put("name", "zhuyinbai"); h.put("age", "28"); RedisDao.hmset("user", h); System.out.println(RedisDao.hkeys("user")); System.out.println(RedisDao.hvals("user")); System.out.println(RedisDao.hmget("user","name")); System.out.println(RedisDao.hmget("user","age")); //--------------------List-------------------- List _list = new ArrayList(); _list.add("A1"); _list.add("A2"); _list.add("A3"); RedisDao.lpush("list", _list); System.out.println(RedisDao.lrange("list", 0, -1)); //--------------------Object-------------------- BOCrmOperator _o = new BOCrmOperator(); _o.setId(1l); _o.setCode("code"); _o.setName("name"); RedisDao.setObject("o:1", _o); System.out.println(((BOCrmOperator)RedisDao.getObject("o:1")).toString()); } }