@Component("redisMQ") public class RedisMQ implements IRedisMQ { @Resource private StringRedisTemplate stringRedisTemplate;
@Resource private JsonRedisSeriaziler jsonRedisSerializer;
/** * 压栈 * * @param key * @param value * @return */ public Long push(String key, String value) { return stringRedisTemplate.opsForList().leftPush(key, value); }
/** * 出栈 * * @param key * @return */ public String pop(String key) { return stringRedisTemplate.opsForList().leftPop(key); }
/** * 入队 * * @param key * @param value * @return */ public Long in(String key, String value) { return stringRedisTemplate.opsForList().rightPush(key, value); }
/** * 从队列的头,插入对象 */ public <T> Long pushFromHead(String key,T value){ return stringRedisTemplate.opsForList().leftPush(key, this.jsonRedisSerializer.seriazileAsString(value)); }
/** * 从对尾,插入对象 * @param value */ public <T> Long pushFromTail(String key,T value){ return stringRedisTemplate.opsForList().rightPush(key, this.jsonRedisSerializer.seriazileAsString(value)); }
/** * 出队 * * @param key * @return */ public String out(String key) { return stringRedisTemplate.opsForList().leftPop(key); } public String out(String key,int timeout){ return stringRedisTemplate.opsForList().leftPop(key, timeout, TimeUnit.SECONDS); }
/** * 出队,从对头出队 * noblocking * @return null if no item in queue */ public <T> T removeFromHead(String key,Class<T> clazz) { return this.jsonRedisSerializer.deserializeAsObject(stringRedisTemplate.opsForList().leftPop(key).toString(),clazz); } /** * 出队,从对头出队 * noblocking * @return null if no item in queue */ public <T> T removeFromHead(String key, int timeout,Class<T> clazz) { return this.jsonRedisSerializer.deserializeAsObject(stringRedisTemplate.opsForList().leftPop(key, timeout, TimeUnit.SECONDS).toString(),clazz) ; }
/** * 出队,从队尾出队 * noblocking * @return null if no item in queue */ public <T> T removeFromTail(String key,Class<T> clazz){ return this.jsonRedisSerializer.deserializeAsObject(stringRedisTemplate.opsForList().rightPop(key).toString(),clazz); } /** * 栈/队列长 * * @param key * @return */ public Long length(String key) { return stringRedisTemplate.opsForList().size(key); }
/** * 范围检索 * * @param key * @param start * @param end * @return */ public List<String> range(String key, int start, int end) { return stringRedisTemplate.opsForList().range(key, start, end); }
/** * 移除 * * @param key * @param i * @param value */ public void remove(String key, long i, String value) { stringRedisTemplate.opsForList().remove(key, i, value); }
/** * 检索 * * @param key * @param index * @return */ public String index(String key, long index) { return (String) stringRedisTemplate.opsForList().index(key, index); }
/** * 置值 * * @param key * @param index * @param value */ public void set(String key, long index, String value) { stringRedisTemplate.opsForList().set(key, index, value); }
/** * 裁剪 * * @param key * @param start * @param end */ public void trim(String key, long start, int end) { stringRedisTemplate.opsForList().trim(key, start, end); }
}
|