Redis 实现简单的队列

本文介绍了如何利用Redis作为基础,实现一个简单的Java消息队列。通过提供的核心代码和序列化方法,阐述了具体的操作步骤和使用方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值