Java examples for Big Data:Jedis
Save and read object in Jedis
package allen.commons.redis.test; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.dao.DataAccessException; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisTemplate; import com.alibaba.fastjson.JSON; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class RedisTest { private static final Logger logger = LoggerFactory .getLogger(RedisTest.class); private static JedisPoolConfig jedisPoolConfig; private static Jedis jedis; private static Integer maxActive = 1000; private static Integer maxIdle = 20; private static Integer maxWait = 3000; private static String hostIp = "10.33.3.225"; private static Integer port = 6379; private RedisTemplate redisTemplate; public static Jedis getJedis() { if (jedisPoolConfig == null) { jedisPoolConfig = new JedisPoolConfig(); //jedisPoolConfig.setMaxActive(maxActive); jedisPoolConfig.setMaxTotal(maxActive); jedisPoolConfig.setMaxIdle(maxIdle); //jedisPoolConfig.setMaxWait(maxWait); jedisPoolConfig.setMaxWaitMillis(maxWait); }// ww w.j a v a2 s. co m JedisPool jedisPool = new JedisPool(jedisPoolConfig, hostIp, port); jedis = jedisPool.getResource(); return jedis; } public static void set(String key, Object value) { try { Jedis jedis = getJedis(); jedis.set(key.getBytes(), serialize(value)); logger.info("!!!"); } catch (Exception e) { logger.error(e.getMessage(), e); } finally { //jedisPool.returnResource(jedis); } } @SuppressWarnings("unchecked") public String saveTestBean(TestBean1 test) { return (String) redisTemplate.execute(new RedisCallback<String>() { @Override public String doInRedis(RedisConnection connection) throws DataAccessException { byte[] key = getKey(String.valueOf(test.getId())); byte[] value = com.alibaba.fastjson.JSON.toJSONString(test) .getBytes(); connection.set(key, value); return null; } }); } public static void set(String key, String value) { try { Jedis jedis = getJedis(); jedis.set(key, value); } catch (Exception e) { logger.error(e.getMessage(), e); } } public static Object get(String key) { Jedis jedis = getJedis(); return null; } 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) { } return null; } 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) { } return null; } public TestBean1 getTestBean(Integer id, String name) { return TestBean1.getInstance(id, name); } public static byte[] getKey(String id) { return ("HQYG:Allen:test:" + id).getBytes(); } public static void main(String[] args) { for (int i = 0; i < 100; i++) { Jedis jedis = getJedis(); //jedis.set("allen", "aaa"); TestBean1 test = TestBean1.getInstance(i, "allen"); byte[] key = getKey(String.valueOf(test.getId())); byte[] value = JSON.toJSONString(test).getBytes(); jedis.set(key, value); } } } class TestBean1 implements Serializable { private static final long serialVersionUID = -2730211713088606559L; private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public static TestBean1 getInstance(Integer id, String name) { TestBean1 bean = new TestBean1(); bean.setId(id); bean.setName(name); return bean; } public static void main(String[] args) { // TODO Auto-generated method stub } }