SpringDataRedis入门到实战

2024-04-10 12:58

本文主要是介绍SpringDataRedis入门到实战,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

5.Spring Data Redis入门实例
准备工作
(1)构建Maven工程 SpringDataRedisDemo
(2)引入Spring相关依赖、引入JUnit依赖 (内容参加其它工程)
(3)引入Jedis和SpringDataRedis依赖
pom.xml


4.0.0
com.ldc.org
SpringDataRedisDemo
0.0.1-SNAPSHOT

<!-- 集中定义依赖版本号 -->
<properties><junit.version>4.12</junit.version><spring.version>4.2.4.RELEASE</spring.version>
</properties><dependencies><!-- Spring --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.9</version></dependency><!-- 缓存 --><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.8.1</version></dependency><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>1.7.2.RELEASE</version></dependency></dependencies>
(4)在src/main/resources下创建properties文件夹,建立redis-config.properties

redis.host=127.0.0.1
redis.port=6379
redis.pass=
redis.database=0
redis.maxIdle=300
redis.maxWait=3000
redis.testOnBorrow=true
(5)在src/main/resources下创建spring文件夹 ,创建applicationContext-redis.xml

<?xml version="1.0" encoding="UTF-8"?>

<context:property-placeholder location=“classpath*:properties/*.properties” />

maxIdle :最大空闲数 maxWaitMillis:连接时的最大等待毫秒数 testOnBorrow:在提取一个jedis实例时,是否提前进行验证操作;如果为true,则得到的jedis实例均是可用的; 6.值类型操作 package test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4Cla***unner;

@RunWith(SpringJUnit4Cla***unner.class)
@ContextConfiguration(locations=“classpath:spring/applicationContext-redis.xml”)
public class TestValue {

@Autowired
private RedisTemplate redisTemplate;@Test
public void setValue() {redisTemplate.boundValueOps("name").set("ldc");
}@Test
public void getValue() {String string=(String) redisTemplate.boundValueOps("name").get();System.out.println(string);
}@Test
public void deleteValue() {redisTemplate.delete("name");
}

}
7. Set类型操作
package test;

import java.util.Set;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4Cla***unner;

@RunWith(SpringJUnit4Cla***unner.class)
@ContextConfiguration(locations=“classpath:spring/applicationContext-redis.xml”)
public class TestSet {

@Autowired
private RedisTemplate redisTemplate;@Test
public void setValue() {//存进去和顺序无关redisTemplate.boundSetOps("nameSet").add("曹操");redisTemplate.boundSetOps("nameSet").add("刘备");redisTemplate.boundSetOps("nameSet").add("孙权");
}@Test
public void getValue() {Set set=redisTemplate.boundSetOps("nameSet").members();System.out.println(set);
}@Test
public void removeValue() {//单独的移除其中一个元素redisTemplate.boundSetOps("nameSet").remove("孙权");
}@Test
public void delete() {//移除全部redisTemplate.delete("nameSet");
}

}
8.List类型操作
package test;

import java.util.List;
import java.util.Set;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4Cla***unner;

@RunWith(SpringJUnit4Cla***unner.class)
@ContextConfiguration(locations=“classpath:spring/applicationContext-redis.xml”)
public class TestList {

@Autowired
private RedisTemplate redisTemplate;/*** 右压栈*/
@Test
public void setValue1() {redisTemplate.boundListOps("nameList1").rightPush("刘备");redisTemplate.boundListOps("nameList1").rightPush("关羽");redisTemplate.boundListOps("nameList1").rightPush("张飞");
}/*** 显示右压栈的值*/
@Test
public void getValue1() {List list=redisTemplate.boundListOps("nameList1").range(0, 10);System.out.println(list);
}/*** 左压栈*/
@Test
public void setValue2() {redisTemplate.boundListOps("nameList2").leftPush("刘备");redisTemplate.boundListOps("nameList2").leftPush("关羽");redisTemplate.boundListOps("nameList2").leftPush("张飞");
}/*** 显示左压栈的值*/
@Test
public void getValue2() {List list=redisTemplate.boundListOps("nameList2").range(0, 10);System.out.println(list);
}/*** 按照索引查询*/
@Test
public void searchByIndex() {String string=(String) redisTemplate.boundListOps("nameList2").index(1);System.out.println(string);
}/*** 删除其中一个元素*/
@Test
public void removeValue() {//单独的移除其中一个元素,第一个参数是移除的个数,不是位置的下表redisTemplate.boundSetOps("nameList1").remove(1,"关羽");
}/*** 删除整个List集合*/
@Test
public void delete() {//单独的移除其中一个元素,第一个参数是移除的个数,不是位置的下表redisTemplate.delete("nameList1");
}

}
9.Hash类型操作
package test;

import java.util.List;
import java.util.Set;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4Cla***unner;

@RunWith(SpringJUnit4Cla***unner.class)
@ContextConfiguration(locations=“classpath:spring/applicationContext-redis.xml”)
public class TestHash {

@Autowired
private RedisTemplate redisTemplate;/*** 存值*/
@Test
public void setValue() {redisTemplate.boundHashOps("nameHash").put("a", "唐僧");redisTemplate.boundHashOps("nameHash").put("b", "悟空");redisTemplate.boundHashOps("nameHash").put("c", "八戒");redisTemplate.boundHashOps("nameHash").put("d", "沙僧");
}/*** 取所有key*/
@Test
public void getKey() {Set keys=redisTemplate.boundHashOps("nameHash").keys();System.out.println(keys);
}/*** 取所有value*/
@Test
public void getValue() {List list=redisTemplate.boundHashOps("nameHash").values();System.out.println(list);
}/*** 根据key取值*/
@Test
public void searchValueByKey() {String string=(String) redisTemplate.boundHashOps("nameHash").get("b");System.out.println(string);
}/*** 移除某个小key的值*/
@Test
public void removeValueByKey() {redisTemplate.boundHashOps("nameHash").delete("c");
}/*** 删除其中一个元素*/
@Test
public void removeValue() {//单独的移除其中一个元素,第一个参数是移除的个数,不是位置的下表redisTemplate.boundSetOps("nameList1").remove(1,"关羽");
}/*** 删除整个List集合*/
@Test
public void delete() {//单独的移除其中一个元素,第一个参数是移除的个数,不是位置的下表redisTemplate.delete("nameList1");
}

}
龙华大道1号http://www.kinghill.cn/LongHuaDaDao1Hao/index.html

这篇关于SpringDataRedis入门到实战的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/891127

相关文章

MySQL DQL从入门到精通

《MySQLDQL从入门到精通》通过DQL,我们可以从数据库中检索出所需的数据,进行各种复杂的数据分析和处理,本文将深入探讨MySQLDQL的各个方面,帮助你全面掌握这一重要技能,感兴趣的朋友跟随小... 目录一、DQL 基础:SELECT 语句入门二、数据过滤:WHERE 子句的使用三、结果排序:ORDE

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Java Spring 中的监听器Listener详解与实战教程

《JavaSpring中的监听器Listener详解与实战教程》Spring提供了多种监听器机制,可以用于监听应用生命周期、会话生命周期和请求处理过程中的事件,:本文主要介绍JavaSprin... 目录一、监听器的作用1.1 应用生命周期管理1.2 会话管理1.3 请求处理监控二、创建监听器2.1 Ser

Python中OpenCV与Matplotlib的图像操作入门指南

《Python中OpenCV与Matplotlib的图像操作入门指南》:本文主要介绍Python中OpenCV与Matplotlib的图像操作指南,本文通过实例代码给大家介绍的非常详细,对大家的学... 目录一、环境准备二、图像的基本操作1. 图像读取、显示与保存 使用OpenCV操作2. 像素级操作3.

Apache 高级配置实战之从连接保持到日志分析的完整指南

《Apache高级配置实战之从连接保持到日志分析的完整指南》本文带你从连接保持优化开始,一路走到访问控制和日志管理,最后用AWStats来分析网站数据,对Apache配置日志分析相关知识感兴趣的朋友... 目录Apache 高级配置实战:从连接保持到日志分析的完整指南前言 一、Apache 连接保持 - 性

MQTT SpringBoot整合实战教程

《MQTTSpringBoot整合实战教程》:本文主要介绍MQTTSpringBoot整合实战教程,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录MQTT-SpringBoot创建简单 SpringBoot 项目导入必须依赖增加MQTT相关配置编写

JavaScript实战:智能密码生成器开发指南

本文通过JavaScript实战开发智能密码生成器,详解如何运用crypto.getRandomValues实现加密级随机密码生成,包含多字符组合、安全强度可视化、易混淆字符排除等企业级功能。学习密码强度检测算法与信息熵计算原理,获取可直接嵌入项目的完整代码,提升Web应用的安全开发能力 目录

Redis迷你版微信抢红包实战

《Redis迷你版微信抢红包实战》本文主要介绍了Redis迷你版微信抢红包实战... 目录1 思路分析1.1hCckRX 流程1.2 注意点①拆红包:二倍均值算法②发红包:list③抢红包&记录:hset2 代码实现2.1 拆红包splitRedPacket2.2 发红包sendRedPacket2.3 抢

springboot项目redis缓存异常实战案例详解(提供解决方案)

《springboot项目redis缓存异常实战案例详解(提供解决方案)》redis基本上是高并发场景上会用到的一个高性能的key-value数据库,属于nosql类型,一般用作于缓存,一般是结合数据... 目录缓存异常实践案例缓存穿透问题缓存击穿问题(其中也解决了穿透问题)完整代码缓存异常实践案例Red

Spring Boot拦截器Interceptor与过滤器Filter深度解析(区别、实现与实战指南)

《SpringBoot拦截器Interceptor与过滤器Filter深度解析(区别、实现与实战指南)》:本文主要介绍SpringBoot拦截器Interceptor与过滤器Filter深度解析... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现与实