Redis + Jedis + Spring (list操作)
为了简便操作,我使用了StringRedisTemplate。用字符串操作做展示。当然,你可以继续使用RedisTemplate。
闲言少叙,上代码,一目了然:
- /**
- * Mar 5, 2013
- */
- package org.zlex.redis.support;
- import java.util.List;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.redis.core.StringRedisTemplate;
- import org.springframework.stereotype.Component;
- /**
- *
- * @author snowolf
- * @version 1.0
- * @since 1.0
- */
- @Component("listOps")
- public class ListOps {
- @Autowired
- private StringRedisTemplate stringRedisTemplate;
- /**
- * 压栈
- *
- * @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);
- }
- /**
- * 出队
- *
- * @param key
- * @return
- */
- public String out(String key) {
- return stringRedisTemplate.opsForList().leftPop(key);
- }
- /**
- * 栈/队列长
- *
- * @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 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);
- }
- }
这里说明下,例如LPUSH,RPUSH,其实就是从左边压栈,还是从右边压栈的不同命令。可以把堆栈看作是一个从左至右的数组。如果左边压栈,右边出栈,那就是队列的入队/出队操作;如果左边压栈,左边出栈,那就是堆栈操作。
举个具体的例子:
队列操作:LPUSH入队,RPOP出队,同理,可把L|R替换。
堆栈操作:LPUSH压栈,LPOP出栈,同理,可把L|R替换。
下面进行测试用例,初始、结束时,分别做入队、出队操作,期间进行堆栈,队列操作。不用我细说了,看测试用例,很简单!
- /**
- * Mar 5, 2013
- */
- package org.zlex.redis;
- import static org.junit.Assert.*;
- import java.util.List;
- import org.junit.Before;
- import org.junit.After;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.zlex.redis.support.ListOps;
- /**
- *
- * @author snowolf
- * @version 1.0
- * @since 1.0
- */
- public class ListOpsTest {
- private ApplicationContext app;
- private ListOps listOps;
- private String key = "queue";
- @Before
- public void before() throws Exception {
- app = new ClassPathXmlApplicationContext("applicationContext.xml");
- listOps = (ListOps) app.getBean("listOps");
- System.out.println("------------IN---------------");
- for (int i = 0; i < 5; i++) {
- String uid = "u" + i;
- System.out.println(uid);
- listOps.in(key, uid);
- }
- }
- @After
- public void after() {
- // ------------OUT---------------
- System.out.println("------------OUT---------------");
- long length = listOps.length(key);
- for (long i = 0; i < length; i++) {
- String uid = listOps.out(key);
- System.out.println(uid);
- }
- }
- @Test
- public void stack() {
- // ------------PUSH---------------
- String key = "stack";
- int len = 5;
- System.out.println("------------PUSH---------------");
- for (int i = 0; i < len; i++) {
- String uid = "u" + System.currentTimeMillis();
- System.out.println(uid);
- listOps.push(key, uid);
- }
- long length = listOps.length(key);
- assertEquals(len, length);
- // ------------POP---------------
- System.out.println("------------POP---------------");
- for (long i = 0; i < length; i++) {
- String uid = listOps.pop(key);
- System.out.println(uid);
- }
- }
- @Test
- public void index() {
- // -------------INDEX-------------
- String value = listOps.index(key, 3);
- assertEquals("u3", value);
- }
- @Test
- public void range() {
- // -------------RANGE-------------
- List<String> list = listOps.range(key, 3, 5);
- boolean result1 = list.contains("u3");
- assertEquals(true, result1);
- boolean result2 = list.contains("u1");
- assertEquals(false, result2);
- }
- @Test
- public void trim() {
- // ------------TRIM---------------
- List<String> list = listOps.range(key, 3, 5);
- listOps.trim(key, 3, 5);
- boolean result3 = list.contains("u1");
- assertEquals(false, result3);
- }
- @Test
- public void set() {
- // ------------SET-----------------
- List<String> list = listOps.range(key, 3, 5);
- listOps.set(key, 4, "ux4");
- boolean result4 = list.contains("u4");
- assertEquals(true, result4);
- }
- @Test
- public void remove() {
- // ------------REMOVE-----------------
- listOps.remove(key, 4, "u4");
- String value = listOps.index(key, 4);
- assertEquals(null, value);
- }
- }
Redis + Jedis + Spring (list操作)的更多相关文章
- 征服 Redis + Jedis + Spring (一)—— 配置&常规操作(GET SET DEL)
有日子没写博客了,真的是忙得要疯掉. 完成项目基础架构搭建工作,解决了核心技术问题,接着需要快速的调研下基于Spring框架下的Redis操作. 相关链接: 征服 Redis 征服 Redis + J ...
- 征服 Redis + Jedis + Spring (三)—— 列表操作【转】
一开始以为Spring下操作哈希表,列表,真就是那么土.恍惚间发现“stringRedisTemplate.opsForList()”的强大,抓紧时间恶补下. 相关链接: 征服 Redis 征服 Re ...
- Redis实战之征服 Redis + Jedis + Spring (一)
Redis + Jedis + Spring (一)—— 配置&常规操作(GET SET DEL)接着需要快速的调研下基于Spring框架下的Redis操作. 相关链接: Redis实战 Re ...
- Redis实战之征服 Redis + Jedis + Spring (二)
不得不说,用哈希操作来存对象,有点自讨苦吃! 不过,既然吃了苦,也做个记录,也许以后API升级后,能好用些呢?! 或许,是我的理解不对,没有真正的理解哈希表. 相关链接: Redis实战 Redis实 ...
- Redis实战之征服 Redis + Jedis + Spring (三)
一开始以为Spring下操作哈希表,列表,真就是那么土.恍惚间发现“stringRedisTemplate.opsForList()”的强大,抓紧时间恶补下. 通过spring-data-redis完 ...
- 征服 Redis + Jedis + Spring —— 配置&常规操作
Spring提供了对于Redis的专门支持:spring-data-redis.此外,类似的还有: 我想大部分人对spring-data-hadoop.spring-data-mongodb.spri ...
- Redis + Jedis + Spring 实例(对象的操作)
目录(?)[+] 不得不说,用哈希操作来存对象,有点自讨苦吃! 不过,既然吃了苦,也做个记录,也许以后API升级后,能好用些呢?! 或许,是我的理解不对,没有真正的理解哈希表. 一.预期 接上 ...
- Redis + Jedis + Spring整合遇到的异常(转)
项目中需要用到缓存,经过比较后,选择了redis,客户端使用jedis连接,也使用到了spring提供的spring-data-redis.配置正确后启动tomcat,发现如下异常: Caused b ...
- Redis实战之Redis + Jedis
用Memcached,对于缓存对象大小有要求,单个对象不得大于1MB,且不支持复杂的数据类型,譬如SET 等.基于这些限制,有必要考虑Redis! 相关链接: Redis实战 Redis实战之Redi ...
随机推荐
- Django之上传文件
使用Form表单上传文件 upload.html <!DOCTYPE html> <html lang="en"> <head> <met ...
- 住javaWeb分页实现(模拟百度首页)
本文来源于 http://blog.csdn.net/tjpu_lin/article/details/41050475 近期在开发一个项目,项目中有非常多数据展示的模块.所以要用到分页,网上搜了非常 ...
- 黑马程序员——String类
------<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS ...
- SVG 路径(path)
本文转自:https://developer.mozilla.org/zh-CN/docs/Web/SVG/Tutorial/Paths <path>元素是SVG基本形状中最强大的一个,它 ...
- Proguard 保留native methods的问题
发现一个奇怪的问题,如果使用下面的配置来keep的话,native的方法还是被删掉了,百思不得其解. -keepclasseswithmembers class * { native *; } ...
- (转载)iOS 多媒体
音频:(音效.音乐) 在iOS中音频播放从形式上可以分为音效播放和音乐播放.前者主要指的是一些短音频播放,通常作为点缀音频,对于这类音频不需要进行进度.循环等控制.后者指的是一些较长的音频,通常是主音 ...
- js中完数的输出
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- hdu 验证角谷猜想 1279
Problem Description 数论中有许多猜想尚未解决,其中有一个被称为"角谷猜想"的问题,该问题在五.六十年代的美国多个著名高校中曾风行一时,这个问题是这样描述的:任何 ...
- libtcmalloc 简单使用
下载地址: https://github.com/gperftools/gperftools这个编译很简单,直接进入vsprojects\libtcmalloc_minimal,用vc编译即可,整个过 ...
- java 时区处理机制(0时区转换到服务器时区)
package com.globalroam.util; import java.util.Calendar; import java.util.Date; import java.util.Time ...