您的位置:首页 > 数据库 > Redis

redis存储list工具类

2015-12-14 14:03 447 查看
package com.syzton.sunread.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
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.syzton.sunread.model.user.User;

import net.spy.memcached.compat.CloseUtil;
import net.spy.memcached.compat.log.Logger;
import net.spy.memcached.compat.log.LoggerFactory;
import redis.clients.jedis.Jedis;

/**
* jedisUtil
*/
public class JedisUtil {
private static Logger logger = LoggerFactory.getLogger(JedisUtil.class);

/*	public static void saveStringRedis(final String key,final String value) {
redisTemplate.execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection)
throws DataAccessException {
connection.set(
redisTemplate.getStringSerializer().serialize(key),
redisTemplate.getStringSerializer().serialize(value));
return null;
}
});
}
*/

/**
* 向redis 存list
* @param key
* @param list
*/
public static void saveRedisList(String key,List list){
Jedis jedis = JedisPoolUtil.buildJedisPool().getResource();
jedis.set(key.getBytes(), ListTranscoder.serialize(list));
}

/**
* 从redis 取list
* @param key
* @return
*/
public static List getRedisList(String key){
Jedis jedis = JedisPoolUtil.buildJedisPool().getResource();
byte[] in = jedis.get(key.getBytes());
List<Object> newlist = ListTranscoder.deserialize(in);
return newlist;
}

/**
* 向redis 存 object
* @param key
* @param list
*/
public static void saveRedisObject(String key,Object obj){
Jedis jedis = JedisPoolUtil.buildJedisPool().getResource();
jedis.set(key.getBytes(), ObjectTranscoder.serialize(obj));
}

/**
* 从redis 取object
* @param key
* @return
*/
public static Object getRedisObject(String key){
Jedis jedis = JedisPoolUtil.buildJedisPool().getResource();
byte[] in = jedis.get(key.getBytes());
Object obj  = ObjectTranscoder.deserialize(in);
return obj;
}

public static void main(String[] args) {
User a = new User();
a.setUsername("a");
User b = new User();
b.setUsername("b");
List<User> list = new ArrayList<User>();
list.add(a);
list.add(b);
saveRedisList("user",list);
saveRedisObject("111",a);
List<User> userList = getRedisList("user");
for (int i = 0; i < userList.size(); i++) {
String username = userList.get(i).getUsername();
System.out.println(username);
}

User user = (User) getRedisObject("111");
System.out.println(user.getUsername());
}

public static void close(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (Exception e) {
logger.info("Unable to close %s", closeable, e);
}
}
}

static class ListTranscoder{

public static byte[] serialize(List<Object> value) {
if (value == null) {
throw new NullPointerException("Can't serialize null");
}
byte[] rv=null;
ByteArrayOutputStream bos = null;
ObjectOutputStream os = null;
try {
bos = new ByteArrayOutputStream();
os = new ObjectOutputStream(bos);
for(Object obj : value){
os.writeObject(obj);
}
os.writeObject(null);
os.close();
bos.close();
rv = bos.toByteArray();
} catch (IOException e) {
throw new IllegalArgumentException("Non-serializable object", e);
} finally {
close(os);
close(bos);
}
return rv;
}

public static List deserialize(byte[] in) {
List<Object> list = new ArrayList<Object>();
ByteArrayInputStream bis = null;
ObjectInputStream is = null;
try {
if(in != null) {
bis=new ByteArrayInputStream(in);
is=new ObjectInputStream(bis);
while (true) {
Object user = (Object) is.readObject();
if(user == null){
break;
}else{
list.add(user);
}
}
is.close();
bis.close();
}
} catch (IOException e) {
logger.warn("Caught IOException decoding %d bytes of data",
in == null ? 0 : in.length, e);
} catch (ClassNotFoundException e) {
logger.warn("Caught CNFE decoding %d bytes of data",
in == null ? 0 : in.length, e);
} finally {
CloseUtil.close(is);
CloseUtil.close(bis);
}
return list;
}
}

static class ObjectTranscoder{
public static byte[] serialize(Object value) {
if (value == null) {
throw new NullPointerException("Can't serialize null");
}
byte[] rv=null;
ByteArrayOutputStream bos = null;
ObjectOutputStream os = null;
try {
bos = new ByteArrayOutputStream();
os = new ObjectOutputStream(bos);
os.writeObject(value);
os.close();
bos.close();
rv = bos.toByteArray();
} catch (IOException e) {
throw new IllegalArgumentException("Non-serializable object", e);
} finally {
close(os);
close(bos);
}
return rv;
}

public static Object deserialize(byte[] in) {
Object rv=null;
ByteArrayInputStream bis = null;
ObjectInputStream is = null;
try {
if(in != null) {
bis=new ByteArrayInputStream(in);
is=new ObjectInputStream(bis);
rv=is.readObject();
is.close();
bis.close();
}
} catch (IOException e) {
logger.warn("Caught IOException decoding %d bytes of data",
in == null ? 0 : in.length, e);
} catch (ClassNotFoundException e) {
logger.warn("Caught CNFE decoding %d bytes of data",
in == null ? 0 : in.length, e);
} finally {
CloseUtil.close(is);
CloseUtil.close(bis);
}
return rv;
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: