您的位置:首页 > 数据库 > Redis

Redis Windows版安装及整合Spring

2016-05-30 17:05 519 查看
简介

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的版本,项目地址是:

https://github.com/MSOpenTech/redis



下载下来解压,双击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"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  redis spring