Redis Windows版安裝及整合Spring
阿新 • • 發佈:2020-11-27
此文轉載自:https://my.oschina.net/u/4518092/blog/4755040
大咖揭祕Java人都栽在了哪?點選免費領取《大廠面試清單》,攻克面試難關~>>>
大咖揭祕Java人都栽在了哪?點選免費領取《大廠面試清單》,攻克面試難關~>>>
- 簡介
1.Redis 是完全開源免費的,遵守BSD協議,是一個高效能的key-value資料庫。
2.Redis支援資料的持久化,可以將記憶體中的資料保持在磁碟中,重啟的時候可以再次載入進行使用。
3.Redis不僅僅支援簡單的key-value型別的資料,同時還提供list,set,zset,hash等資料結構的儲存。
4.Redis支援資料的備份,即master-slave模式的資料備份。 - 安裝
Redis官方是不支援windows的,只是 Microsoft Open Tech group 在 GitHub上開發了一個Win64的版本,專案地址是:
下載下來解壓,雙擊redis-server.exe,即啟動redis服務。
相關的配置可以修改redis.windows.conf。 - 視覺化工具
推薦使用RedisDesktopManager。
整合Spring
需要jar:
redis.clients:jedis:2.8.1
org.springframework.data:spring-data-redis:1.6.4.RELEASE
Spring配置:
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >
<property name="hostName" value="127.0.0.1"/>
<property name="port" value="6379"/>
<property name="usePool" value="true"/>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
<property name="enableTransactionSupport" value="true"/>
</bean>
<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
</bean>
Spring環境下的Redis事務
RedisTemplate可以使用multi, exec, and discard 命令來控制Redis的事務。
//execute a transactionList<Object> txResults = redisTemplate.execute(new SessionCallback<List<Object>>() {
public List<Object> execute(RedisOperations operations) throws DataAccessException {
operations.multi();
operations.opsForSet().add("key", "value1");
// This will contain the results of all ops in the transaction
return operations.exec();
}});System.out.println("Number of items added to set: " + txResults.get(0));
Spring Data 通過SessionCallback 這個介面來保證多次操作時都會是用一個連線(connection)。
當RedisTemplate 啟用事務管理後,會將Redis納入到Spring的事務管理中。
<property name="enableTransactionSupport" value="true"/>