Redis Windows版安装及整合Spring

  1. 简介
    1.Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库。
    2.Redis支持数据的持久化,可以将内存中的数据保持在磁盘中,重启的时候可以再次加载进行使用。
    3.Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存储。
    4.Redis支持数据的备份,即master-slave模式的数据备份。
  2. 安装
    Redis官方是不支持windows的,只是 Microsoft Open Tech group 在 GitHub上开发了一个Win64的版本,项目地址是:
    https://github.com/MSOpenTech/redis
    这里写图片描述
    下载下来解压,双击redis-server.exe,即启动redis服务。
    这里写图片描述
    相关的配置可以修改redis.windows.conf。
  3. 可视化工具
    推荐使用RedisDesktopManager。
    这里写图片描述
  4. 整合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"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值