Spring Data Redis 入门(一)

xiaoxiao2021-02-28  32

一、什么是Redis

        Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。 它支持多种类型的数据结构,如 字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 与范围查询, bitmaps, hyperloglogs 和 地理空间(geospatial) 索引半径查询。 Redis 内置了 复制(replication),LUA脚本(Lua scripting), LRU驱动事件(LRU eviction),事务(transactions) 和不同级别的 磁盘持久化(persistence), 并通过 Redis哨兵(Sentinel)和自动 分区(Cluster)提供高可用性(high availability)。

官方网站:

1.Redis中文网站

二、我们需要准备什么

       工欲善其事必先利其器,我们必须要下载Redis,Redis分为Windows版本以及Linux版本,本文选取Linux环境。

我们需要下载稳定的4.0.9版本。点击这里直接连接下载redis.4.0.9

       Linux 下安装Redis 大家自行百度,我们使用 Oracle VM VirtualBox作为虚拟机 ,系统为Centos6.

       如果你已经连接了Redis我们还需要什么,需要图形化操作界面:

       Redis Desktop Manager 由于网络问题我写这个文章没有打开就不复制地址了。大家可以自行下载。

三、如何在Java中使用Redis

     现在比较主流的是 1.使用jedis直接进行操作。2.利用SpringDataRedis进行操作。这俩种都可以,可以根据自己进行。

1.利用jedis实现

   后期会更新。现在主要是讲解Spring Data Redis实现

2.利用Spring Data Redis进行操作

2.1、我们需要构建Maven项目

下面我贴出我的pom.xml 仅供参考:

<dependencies>     <dependency>         <groupId>org.springframework.data</groupId>   <artifactId>spring-data-redis</artifactId>     <version>2.0.7.RELEASE</version>     </dependency>     <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version>     </dependency>     <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.0.6.RELEASE</version> <scope>test</scope>     </dependency>     <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.6.RELEASE</version>     </dependency>     <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope>     </dependency>     <dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> <version>3.8.0</version> <scope>test</scope>     </dependency>     <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.5</version>     </dependency>     <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.5</version>     </dependency> </dependencies>

2.2、我们需要建立几个配置文件。

1.建立redis.properties

#JedisPoolConfig的参数 #最大连接数 redis.pool.maxTotal=30 #最大空闲时间 redis.pool.maxIdle=10 #每次最大连接数 redis.pool.numTestsPerEvictionRun=1024 #释放扫描的扫描间隔 redis.pool.timeBetweenEvictionRunsMillis=30000 #连接的最小空闲时间 redis.pool.minEvictableIdleTimeMillis=1800000 #连接控歘按时间多久后释放,当空闲时间>该值且空闲连接>最大空闲连接数时直接释放 redis.pool.softMinEvictableIdleTimeMillis=10000 #获得链接时的最大等待毫秒数,小于0:阻塞不确定时间,默认-1 redis.pool.maxWaitMillis=1500 #在获得链接的时候检查有效性,默认false redis.pool.testOnBorrow=true #在空闲时检查有效性,默认false redis.pool.testWhileIdle=true #连接耗尽时是否阻塞,false报异常,true阻塞超时,默认true redis.pool.blockWhenExhausted=false #JedisConnectionFactory的参数 #主机地址,默认:localhost 我这里的ip是虚拟机的ip redis.hostName=192.168.56.101 #主机端口,默认:6379 redis.port=6379 #超时时间,默认:2000 redis.timeout=3000 #密码 #redis.password #是否使用连接池,默认true redis.usePool=true #使用数据库的索引,0-15之间的数字,默认:0 redis.dbIndex=1 #是否使用数据类型的转换,默认:true #redis.convertPipelineAndTxResults #哨兵配置 #redis.sentinelConfig #集群配置 #redis.clusterConfig

2.建立applicationContext.xml

<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:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:redis.properties" /> <context:component-scan base-package="com.ly"></context:component-scan> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!-- 最大连接数 --> <property name="maxTotal" value="${redis.pool.maxTotal}"/> <!-- 最大空闲时间 --> <property name="maxIdle" value="${redis.pool.maxIdle}"/> <!-- 每次最大连接数 --> <property name="numTestsPerEvictionRun" value="${redis.pool.numTestsPerEvictionRun}"/> <!-- 释放扫描的扫描间隔 --> <property name="timeBetweenEvictionRunsMillis" value="${redis.pool.timeBetweenEvictionRunsMillis}"/> <!-- 连接的最小空闲时间 --> <property name="minEvictableIdleTimeMillis" value="${redis.pool.minEvictableIdleTimeMillis}"/> <!-- 连接控歘按时间多久后释放,当空闲时间>该值且空闲连接>最大空闲连接数时直接释放 --> <property name="softMinEvictableIdleTimeMillis" value="${redis.pool.softMinEvictableIdleTimeMillis}"/> <!-- 获得链接时的最大等待毫秒数,小于0:阻塞不确定时间,默认-1 --> <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}"/> <!-- 在获得链接的时候检查有效性,默认false --> <property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/> <!-- 在空闲时检查有效性,默认false --> <property name="testWhileIdle" value="${redis.pool.testWhileIdle}"/> <!-- 连接耗尽时是否阻塞,false报异常,true阻塞超时 默认:true--> <property name="blockWhenExhausted" value="${redis.pool.blockWhenExhausted}"/> </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="timeout" value="${redis.timeout}"/> <property name="database" value="${redis.dbIndex}"/> <property name="usePool" value="${redis.usePool}"/> <property name="poolConfig" ref="jedisPoolConfig"/> </bean> <!-- 对String类型处理的RedisTemplate --> <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <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.StringRedisSerializer" /> </property> <property name="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="hashValueSerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> </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="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/> </property> <property name="hashValueSerializer"> <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/> </property> </bean> </beans>

转载请注明原文地址: https://www.6miu.com/read-2633118.html

最新回复(0)