zoukankan      html  css  js  c++  java
  • Redis Windows版安装及整合Spring

    此文转载自:https://my.oschina.net/u/4518092/blog/4755040
    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"/>
       

    更多内容详见微信公众号:Python测试和开发

    Python测试和开发

  • 相关阅读:
    【转载】关于Java String, StringBuilder, StringBuffer, Hashtable, HashMap的面试题
    LeetCode: 【L4】N-Queens 解题报告
    【转载】在美国找工作秘籍
    Lintcode: Kth Largest Element 解题报告
    LeetCode: Reverse Integer 解题报告
    Lintcode: First Bad Version 解题报告
    九章面试题:Find first K frequency numbers 解题报告
    tomcat之虚拟目录
    百度搜索结果如何屏蔽百家号内容
    CentOS7之Rsync+Inotify架构实现实时同步文件和文件夹
  • 原文地址:https://www.cnblogs.com/phyger/p/14048138.html
Copyright © 2011-2022 走看看