`

征服 Redis + Jedis + Spring (三)—— 列表操作

阅读更多

一开始以为Spring下操作哈希表,列表,真就是那么土。恍惚间发现“stringRedisTemplate.opsForList()”的强大,抓紧时间恶补下。酷

  

相关链接:

征服 Redis

征服 Redis + Jedis

征服 Redis + Jedis + Spring (一)—— 配置&常规操作(GET SET DEL)

征服 Redis + Jedis + Spring (二)—— 哈希表操作(HMGET HMSET)

征服 Redis + Jedis + Spring (三)—— 列表操作

 

通过spring-data-redis完成LINDEX, LLEN, LPOP, LPUSH, LRANGE, LREM, LSET, LTRIM, RPOP, RPUSH命令。其实还有一些命令,当前版本不支持。皱眉不过,这些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

征服 Redis + Jedis

征服 Redis + Jedis + Spring (一)—— 配置&常规操作(GET SET DEL)

征服 Redis + Jedis + Spring (二)—— 哈希表操作(HMGET HMSET)

征服 Redis + Jedis + Spring (三)—— 列表操作

 

12
0
分享到:
评论
2 楼 liulovesun 2013-09-26  
运行报错:
Exception in thread "main" java.lang.NoSuchMethodError: redis.clients.jedis.Jedis.rpush([B[B)Ljava/lang/Long;
at org.springframework.data.redis.connection.jedis.JedisConnection.rPush(JedisConnection.java:1109)
at org.springframework.data.redis.core.DefaultListOperations$12.doInRedis(DefaultListOperations.java:171)
at org.springframework.data.redis.core.DefaultListOperations$12.doInRedis(DefaultListOperations.java:168)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:162)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:133)
at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:75)
at org.springframework.data.redis.core.DefaultListOperations.rightPush(DefaultListOperations.java:168)
at com.x.redis.dao.impl.ListOps.in(ListOps.java:58)
at com.x.redis.app.ListOpsTest.queue(ListOpsTest.java:40)
at com.x.redis.app.ListOpsTest.main(ListOpsTest.java:23)
1 楼 jdbcy 2013-07-23  
你的释放只是归还连接回池子,程序结束的时候 要destroy掉连接池

相关推荐

    征服 Redis + Jedis + Spring (二)—— 哈希表操作(HMGET HMSET)

    在本篇博客“征服 Redis + Jedis + Spring (二)—— 哈希表操作(HMGET HMSET)”中,我们将深入探讨如何利用Redis、Jedis库以及Spring框架进行哈希表的操作,特别是`HMGET`和`HMSET`这两个重要的命令。...

    征服 Redis + Jedis

    这篇“征服Redis + Jedis”的博文很可能是介绍如何在Java应用中高效地使用Redis和Jedis进行数据交互。 首先,了解Redis的基本数据类型是至关重要的。Redis支持字符串(Strings)、哈希表(Hashes)、列表(Lists)...

    各种版本的redis+Jedis驱动包

    综上所述,这个压缩包提供了全面的Redis部署选项和Java开发工具,无论是在Windows还是Linux环境下,都可以快速搭建和管理Redis服务器,并使用Jedis进行高效的数据操作。对于初学者和开发者来说,这是一个非常实用的...

    redis+spring jedis方式

    标题提到的"redis+spring jedis方式",意味着我们将讨论如何通过Spring Data Redis结合Jedis来操作Redis。首先,你需要在项目中引入Spring Data Redis库,版本如描述中所示为`spring-data-redis-1.3.4.RELEASE.jar`...

    Redis+Jedis+案例实战(高清视频教程).rar

    在本教程“Redis+Jedis+案例实战”中,你将深入学习如何利用Redis和Jedis进行实际项目开发。以下是一些关键知识点的概述: 1. Redis基础知识: - 数据类型:Redis支持五种基本数据类型,包括字符串(String)、...

    SpringBoot2+MybatisPlus+SpringSecurity+jwt+redis+Vue的前后端分离的商城系统

    基于当前流行技术组合的前后端分离商城系统:SpringBoot2+MybatisPlus+SpringSecurity+jwt+redis+Vue的前后端分离的商城系统, 包含商城、sku、运费模板、素材库、小程序直播、拼团、砍价、商户管理、 秒杀、优惠券...

    Springboot+SpringSecurity+SpringSession+Redis+Mybatis-Plus+Swwager.zip

    本项目“Springboot+SpringSecurity+SpringSession+Redis+Mybatis-Plus+Swwager”整合了Spring Boot、Spring Security、Spring Session、Redis、Mybatis-Plus以及Swagger等技术,旨在构建一个强大的、安全的、具有...

    基于SpringBoot2+MybatisPlus+SpringSecurity+jwt+redis+Vue的前后端商城系统源码

    yshop基于当前流行技术组合的前后端分离商城系统: SpringBoot2+MybatisPlus+SpringSecurity+jwt+redis+Vue的前后端分离的商城系统, 包含分类、sku、运费模板、素材库、小程序直播、拼团、砍价、商户管理、 秒杀、...

    maven+springmvc+redis+mybatis整合

    本项目以“maven+springmvc+redis+mybatis整合”为主题,旨在提供一个基于这些技术的集成框架,特别强调了利用Redis作为缓存来提升应用性能。下面将详细阐述这个框架中的各个组成部分以及它们之间的协作。 首先,...

    redis+jedis+api

    Jedis是Java语言编写的Redis客户端,提供了丰富的API来操作Redis服务器,使得在Java应用中使用Redis变得简单。 在Redis 3.2.1版本中,引入了一些重要的改进和优化,包括但不限于以下几点: 1. **lua脚本支持增强**...

    SpringBoot+Shiro+JWT+Jedis+MybatisPlus+前后端分离+基于url通用权限管理系统

    本系统“SpringBoot+Shiro+JWT+Jedis+MybatisPlus+前后端分离+基于url通用权限管理系统”就是针对这一需求而设计的,它结合了多种技术,提供了高效、安全且灵活的解决方案。 首先,SpringBoot是这个系统的核心,它...

    Redis+Jedis安装与使用(一)-附件资源

    Redis+Jedis安装与使用(一)-附件资源

    基于springboot微服务框架的个人博客系统,技术栈SpringCloud+MyBatis+Redis+shiro+vue

    基于springboot微服务框架的个人博客系统,技术栈SpringCloud+MyBatis+Redis+shiro+vue 基于springboot微服务框架的个人博客系统,技术栈SpringCloud+MyBatis+Redis+shiro+vue 基于springboot微服务框架的个人博客...

    通用权限管理系统+springboot+mybatis plus+spring security+jwt+redis+mysql

    后端使用SpringBoot框架进行业务逻辑开发,利用Spring Security实现权限控制。数据库采用MySQL进行数据存储,使用MyBatis进行数据访问。 权限控制模块设计包括用户、角色和权限三个主要模块。用户模块用于管理用户...

    spring-data + jedis + redis代码

    在压缩包中的"redis+spring-data"文件可能包含以下内容: - spring配置文件:如application.yml或application-context.xml,其中配置了Redis连接和RedisTemplate。 - 测试代码:可能包含JUnit测试类,展示了如何使用...

    大型电商项目实战1:Redis+Rest+Linux+Nginx+Spring+SpringMVC实现JAVA高并发秒杀系统

    大型电商项目实战1:Redis+Rest+Linux+Nginx+Spring+SpringMVC实现JAVA高并发秒杀系统,baidu链接,谢谢

    Shiro + JWT + SpringBoot + MySQL + Redis(Jedis)实现无状态鉴权机制

    本项目结合了Apache Shiro、JSON Web Token (JWT)、SpringBoot、MySQL数据库以及Redis缓存技术(通过Jedis客户端)来实现这一机制。下面我们将详细探讨这些组件在实现无状态鉴权中的作用。 **Apache Shiro** Apache...

    redis3.2.1+jedis2.8.0+jedis.api.rar一站下载

    Jedis是Java语言开发的Redis客户端,提供了丰富的API来操作Redis服务器,便于Java开发者在项目中集成Redis。 Redis 3.2.1是Redis的一个稳定版本,相比于早期版本,它引入了一些新的特性和改进。其中,重要的更新...

    SpringBoot+VUE+Redis+nginx 网上拍卖平台源码.zip

    SpringBoot+VUE+Redis+nginx 网上拍卖平台源码 SpringBoot+VUE+Redis+nginx 网上拍卖平台源码 SpringBoot+VUE+Redis+nginx 网上拍卖平台源码 SpringBoot+VUE+Redis+nginx 网上拍卖平台源码 SpringBoot+...

Global site tag (gtag.js) - Google Analytics