Redis |
redis是一款開源的高性能的Key-Value數據庫,運行在內存中,由ANSI C編寫。企業開發通常採用Redis來實現緩存。同類的產品還有memcached 、MongoDB等。
Jedis |
Jedis是Redis官方推出的一款面向Java的客戶端,提供了很多接口供Java語言調用。可以在Redis官網下載,當然還有一些開源愛好者提供的客戶端,如Jredis、Redisson等等
SpringDataRedis |
Spring提供了SpringDataRedis用來實現通過配置文件的方式訪問redis服務。SpringDataRedis對Redis底層開發包進行了高度封裝,RedisTemplate提供了redis各種操作、異常處理及序列化。
Spring-Data-Redis針對jedis提供瞭如下功能:
1. 連接池自動管理,提供了一個高度封裝的“RedisTemplate”類
2. 針對jedis客戶端中大量api進行了歸類封裝,將同一類型操作封裝爲operation
搭建 |
<dependencies>
<!-- jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- spring-data-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.6.RELEASE</version>
</dependency>
<!-- spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.10.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
創建applicationContext-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: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/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- ################ 配置Redis單機版 #################-->
<!-- 加載屬性文件 -->
<context:property-placeholder
location="classpath:redis-config.properties"/>
<!-- 配置連接工廠 -->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}"/>
<property name="port" value="${redis.port}"/>
</bean>
<!-- 配置RedisTemplate -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
</bean>
</beans>
創建redis-config.properties
redis.host=192.168.12.131
redis.port=6379