版权声明:转载请加链接 https://blog.csdn.net/y526089989/article/details/88420562
redis可以缓存,自然也可以做简单的消息队列,
下面贴代码
1.核心代码:
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
@Component
public class SongsirRedisTemplate<E> {
@Autowired
private RedisTemplate redisTemplate;
// 序列化工具类
@Autowired
private SerializeUtil<E> serialize;
public static ListOperations<String, String> list;
/**
* 根据键值获取value
*/
public E get(String key) {
ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
return key == null ? null : serialize.unserialize(opsForValue.get(key));
}
/**
* 设置键值对(无超时时间)
*
* @param key
* @param value
* @return
*/
public boolean set(String key, E value) {
ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
try {
opsForValue.set(key, serialize.serialize(value));
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 设置键值对 (设置有效时间,以秒为单位)
*
* @param key
* @param value
* @param expire
* @return boolean
*/
public boolean set(String key, E value, long expire) {
ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
try {
opsForValue.set(key, serialize.serialize(value), expire, TimeUnit.SECONDS);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根据key删除键值对
*
* @param key
* @return
*/
public boolean delete(String key) {
try {
redisTemplate.delete(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* lpush 进队列
*
* @param key
* @param value
* @return
*/
public Long lpush(String key, String value) {
list = redisTemplate.opsForList();
try {
Long leftPush = list.leftPush(key, value);
return leftPush;
} catch (Exception e) {
e.printStackTrace();
return Long.valueOf(-1);
}
}
/**
* rpop 出队列
*
* @param key
* @return
*/
public String rpop(String key) {
list = redisTemplate.opsForList();
try {
return list.rightPop(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 返回指定队列(list)名队列长度
*
* @param key
* @return
*/
public Long llen(String key) {
list = redisTemplate.opsForList();
return list.size(key);
}
}
2.序列化代码:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URLDecoder;
import java.net.URLEncoder;
import org.springframework.stereotype.Component;
@Component
public class SerializeUtil<E> {
public String serialize(E object) {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
String str = baos.toString("ISO-8859-1");
return URLEncoder.encode(str, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
baos.close();
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
@SuppressWarnings("unchecked")
public E unserialize(String serializeStr) {
String readStr = "";
if(serializeStr == null || "".equals(serializeStr)) {
return null;
}
try {
readStr = URLDecoder.decode(serializeStr, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
ObjectInputStream ois = null;
InputStream bais = null;
try {
bais = new ByteArrayInputStream(readStr.getBytes("ISO-8859-1"));
ois = new ObjectInputStream(bais);
return (E) ois.readObject();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
ois.close();
bais.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}
如何使用呢?
//放进去
songsirRedisTemplate.lpush("income", gson.toJson(ic));
//拿出来
gson.fromJson(songsirRedisTemplate.rpop("income"), Income.class);