简介
相信来到这里的朋友都已经对Redis有了基本的了解,基础命令都已掌握,文中就不做过多赘述,避免文章过于冗余,只上干货,干就完了bro。Let's go!!
Spring官网中整合redis地址:Spring Data Redis
Redis因其性能比较高,所以经常在项目中用于缓存数据,因此将Redis和Spring以及项目进行整合。
Spring整合Redis
| 1.创建Maven项目工程
本文所用项目结构如图所示(尚未整合web项目,入门以测试为主):
| 2.pom.xml中引入redis依赖
<!-- 连接redis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>${spring-data-redis-version}</version>
</dependency>
依赖完成后刷新打包至本地仓库:(如图所示)
| 3.redis.conf配置
对配置文件还不了解的兄弟们请参考:
- 1)默认情况下redis运行在保护模式(这种模式下,访问不需要密码),但是这种模式只允许本地访问
- protected-mode yes 改为 no
- 2)初始配置了只在127.0.0.1上绑定监听,将其注释
- 默认 bind 127.0.0.1 改为 #bind 127.0.0.1或 0.0.0.0
| 4.防火墙配置
开放redis的默认端口号6379(或你设置的redis端口号)
这里不做展示,对防火墙不了解的兄弟们请参考(很重要):
| 5.配置spring-redis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--1 配置工厂类 -->
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<!--1.1 服务器地址 -->
<property name="hostName" value="192.168.21.132"></property>
<!--1.2 端口号-->
<property name="port" value="6379"></property>
</bean>
<!-- 3 redisTemplate -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<!--3.1注入工厂-->
<property name="connectionFactory" ref="jedisConnectionFactory"></property>
<!--key序列化-->
<property name="keySerializer" ref="stringRedisSerializer"></property>
<!--value序列化-->
<property name="valueSerializer" ref="genericJackson2JsonRedisSerializer"></property>
<!--hashkey-->
<property name="hashKeySerializer" ref="stringRedisSerializer"></property>
<!--hashvalue-->
<property name="hashValueSerializer" ref="genericJackson2JsonRedisSerializer"></property>
</bean>
<!--stringRedisSerializer-->
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
<!--JdkSerializationRedisSerializer-->
<bean id="jdkSerializationRedisSerializer"
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>
<!--GenericJackson2JsonRedisSerializer-->
<bean id="genericJackson2JsonRedisSerializer"
class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"></bean>
<!--Jackson2JsonRedisSerializer-->
<bean id="jackson2JsonRedisSerializer"
class="org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer">
<constructor-arg value="java.lang.Object"></constructor-arg>
</bean>
</beans>
此xml已经配好,拿去使用时只需修改ip地址,如图所示:
| 6.常见序列化反序列化处理类
对spring-redis.xml的简单解释:
StringRedisSerializer | 一般key的序列化使用 |
JdkSerializationRedisSerializer | 默认的序列化方式 |
GenericJackson2JsonRedisSerializer (选择) | 支持对象转json,json转对象的序列和反序列化,支持带泛型的对象,效率比Jackson2JsonRedisSerializer低 |
Jackson2JsonRedisSerializer | 支持对象转json,json转对象的序列和反序列化,不支持泛型,效率高 |
我们选择使用GenericJackson2JsonRedisSerializer ,
测试(string、list、set、zset、hash)
| 1.测试String
测试String存对象
使用命令查看数据库的对应值:
对应可视化数据库值如图所示:
| 2.测试List
测试集合的批量添加数据及获取
| 3.测试set
测试交集、并集、差集。
| 4.测试sorted_set
| 5.测试hash
关于hash的使用还有很多,比如使用hash实现购物车、计数器以及在线人数等等。
关于Spring和Redis的整合及入门测试练习就到此一节,勤加练习,打好基础,以熟能生巧。
转载请注明出处
如有错误,欢迎指正
Thanks