spring整合redis简单使用

     2022-03-25     414

关键词:

1.简单介绍

redis 是基于C语言开发。

redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。

redis 是一个 缓存数据库(片面的理解) 既可以做缓存,也可以将数据持久化到磁盘中!

 2.pom.xml 引入相关jar (曾经因jar 版本问题出现报错,请注意)

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.2</version>
</dependency>


<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.7.5.RELEASE</version>
</dependency>

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>


3.spring-redis.xml 配置文件,配置关键bean  redisTemplate

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/util 
   http://www.springframework.org/schema/util/spring-util-3.0.xsd">
    
<!--    <context:property-placeholder location="classpath:redis-config.properties"/>    
      -->
    
    
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    	 <property name="maxIdle" value="${redis.maxIdle}" /> 
	    <property name="maxTotal" value="${redis.maxTotal}" /> 
	    <property name="blockWhenExhausted" value="true" /> 
	    <property name="maxWaitMillis" value="${redis.maxWaitMillis}" /> 
	    <property name="testOnBorrow" value="${redis.testOnBorrow}" /> 
    </bean>
  
  <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> 
    <property name="hostName" value="${redis.hostname}" /> 
    <property name="port" value="${redis.port}"/> 
    <property name="poolConfig" ref="jedisPoolConfig" /> 
    <property name="usePool" value="true"/> 
  </bean> 
  
  <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
    <property name="connectionFactory"  ref="jedisConnectionFactory" />  
    <property name="keySerializer">  
      <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
    </property>   
    <property name="valueSerializer">  
      <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />  
    </property>  
    <property name="hashKeySerializer">   
      <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>   
    </property>  
    <property name="hashValueSerializer">  
      <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>   
    </property> 
   </bean> 
  
  
</beans>



上文中使用到的配置文件 redis-config.properteis

redis.maxIdle=1
redis.maxTotal=5
redis.maxWaitMillis=30000
redis.testOnBorrow=true
redis.hostname=127.0.0.1
redis.port=6379


4.redis 有4个关键的接口如下


private ValueOperations<K, V> valueOps;

private ListOperations<K, V> listOps;

private SetOperations<K, V> setOps;

private ZSetOperations<K, V> zSetOps;



分别对应redis的数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合)


具体使用如下,上代码:

//添加字符串
ValueOperations<String, String> value = this.redisTemplate.opsForValue();
value.set("hello", "讨厌");
System.out.println(value.get("hello"));


//添加 一个 hash集合
HashOperations<String, Object, Object>  hash =redisTemplate.opsForHash();
hash.put("沃尔玛","水果", "苹果");
hash.put("沃尔玛","饮料", "红牛");
System.out.println(hash.entries("沃尔玛"));


//添加一个list 集合
ListOperations<String, Object> list = redisTemplate.opsForList();
list.rightPush("课程", "chinese");
list.rightPush("课程", "englise");
System.out.println(list.range("lpList", 0, 1));


//添加 一个 set 集合
SetOperations<String, Object> set = redisTemplate.opsForSet();
set.add("lpSet", "lp");
set.add("lpSet", "26");
set.add("lpSet", "178cm");
//输出 set 集合
System.out.println(set.members("lpSet"));


//添加有序的 set 集合
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
zset.add("lpZset", "lp", 0);
zset.add("lpZset", "26", 2);
zset.add("lpZset", "178cm", 1);
//输出有序 set 集合
System.out.println(zset.rangeByScore("lpZset", 0, 2));




本文出自 “布拉君君” 博客,请务必保留此出处http://5148737.blog.51cto.com/5138737/1976501

redis整合spring结合使用缓存实例(转)

...net/evankaka     摘要:本文介绍了如何在Spring中配置redis,并通过Spring中AOP的思想,将缓存的方法切入到有需要进入缓存的类或方法前面。一、Redis介绍什么是Redis?&nb 查看详情

ssm整合redis

...经足够简单和轻量级了,但是呢,在此同时,Spring也为Redis提供了支持,就是在Spring-data模块中的Spring-Data-Redis(SDR),它一部分是基于Jedis客户端的API封装,另一部分是对Spring容器的整合。大家应该... 查看详情

spring优雅整合redis缓存

 “小明,多系统的session共享,怎么处理?”“Redis缓存啊!”“小明,我想实现一个简单的消息队列?”“Redis缓存啊!”“小明,分布式锁这玩意有什么方案?”“Redis缓存啊!”“... 查看详情

springboot整合redis以及redis的简单使用

...结构的存储系统,用于缓存还是很方便的, springboot整合redis也很简单。 首先在pom文件里面加入redis的相关依赖: <!--Redis客户端--><dependency><groupId>redis.clients</groupId><artifac 查看详情

springboot整合redis做简单缓存(代码片段)

...依赖<!--引入redis依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!--使用fastjson做序列化和反序列化--><dependency><groupId>com.alibaba</gr... 查看详情

spring整合redis

1,利用spring-data-redis整合项目使用的pom.xml:<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http:/ 查看详情

spring整合redis(代码片段)

Spring整合Redis一,maven导入依赖二,Spring整合(1)SpringBoot(2)Spring三,使用RedisCacheUtil工具类一,maven导入依赖<dependency><groupId>redis.clients</group 查看详情

redis00_springboot整合redisredistemplate使用工具类的抽取(代码片段)

文章目录①.SPRINGBOOT整合REDIS②.RedisTemplate的使用③.关于工具类的抽取①.SPRINGBOOT整合REDIS①.引入data-redis-starter依赖 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></depe... 查看详情

springboot整合redis,基本使用(代码片段)

HiI’mShendiSpringBoot整合Redis,基本使用文章目录简介SpringData引入依赖配置使用自定义序列化方式JSON序列化简单的例子Redis基础在之前学过Redis后,便可以使用SpringBoot来整合Redis了,使用起来非常简单简介之前用Java操作R... 查看详情

ssm整合redis

...经足够简单和轻量级了,但是呢,在此同时,Spring也为Redis 查看详情

springboot访问nosql和简单的thymeleaf-spring-spring-boot整合

SpringBoot访问NoSQLSpringBoot访问Redis在pom.xml添加boot-data-redis定义<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><ver 查看详情

03redis与spring的整合

本文将阐述如何把redis与spring集成。1、前提约束可以使用java调用redis服务https://www.jianshu.com/p/83ef8a80508d2、修改pom.xml在pom.xml中加入以下依赖:<dependency><groupId>org.apache.solr</groupId><artifactId>solr-so 查看详情

spring整合redis客户端及缓存接口设计

一、写在前面缓存作为系统性能优化的一大杀手锏,几乎在每个系统或多或少的用到缓存。有的使用本地内存作为缓存,有的使用本地硬盘作为缓存,有的使用缓存服务器。但是无论使用哪种缓存,接口中的方法... 查看详情

spring整合hibernate实现springdatajpa(简单使用)

直接上代码:pom.xml<!--hibernatestart--><!--springdatajpa--><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-jpa</artifactId><versi 查看详情

redis----springboot和redis整合

SpringBoot和Redis整合非常简单添加pom依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>application. 查看详情

(转)spring整合redis作为缓存

    采用Redis作为Web系统的缓存。用Spring的Cache整合Redis。一、关于redis的相关xml文件的写法<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/bea 查看详情

springboot整合redis共享session-spring-session-data-redis

参考:https://www.cnblogs.com/ityouknow/p/5748830.html   如何使用1、引入spring-boot-starter-redis<dependency><groupId>org.springframework.boot</groupId><artifactId>spring- 查看详情

spring整合redis(代码片段)

Spring整合Redis一,maven导入依赖二,Spring整合(1)SpringBoot(2)Spring三,使用RedisCacheUtil工具类一,maven导入依赖<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><versio... 查看详情