zoukankan      html  css  js  c++  java
  • centos7下的Redis安装

    一.概况

      redis是一款开源的Key-Value数据库,运行在内存中,由ANSI C编写。企业开发通常采用Redis来实现缓存。同类的产品还有memcache 、memcached 、MongoDB等。

      我们通常把Redis当做一个缓存系统。把经常访问的数据放在Redis,需要的时候先查Redis,减轻数据库访问压力。

    二.安装与部署

      官网下载:https://redis.io 最新版本redis-5.0.0.tar.gz

      放到服务器/root目录下

      解压:

        cd /root

        tar zxvf redis-5.0.0.tar.gz

      编译、安装:

        cd redis-5.0.0 

        make 

        cd src

        make install PREFIX=/usr/local/redis  

      将配置文件移动到redis目录: 

        mv /root/redis-5.0.0/redis.conf  /usr/local/redis/ect/

      启动redis服务:

        /usr/local/redis/bin/redis-server  /usr/local/redis/ect/redis.conf

        出现下图说明启动正常

        

      停止redis实例:

        /usr/local/redis/bin/redis-cli shutdown

    三.Jedis和Spring Data Redis

      Jedis是Redis官方推出的一款面向Java的客户端,提供了很多接口供Java语言调用。

      spring-data-redis针对jedis提供了如下功能:
        1.连接池自动管理,提供了一个高度封装的“RedisTemplate”类
        2.针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口
          ValueOperations:简单K-V操作
          SetOperations:set类型数据操作
          ZSetOperations:zset类型数据操作
          HashOperations:针对map类型的数据操作
          ListOperations:针对list类型的数据操作

    四.测试demo

      

      1)构建Maven工程  SpringDataRedisDemo

      2)引入Spring相关依赖、引入JUnit依赖

      3)引入Jedis和SpringDataRedis依赖

        

    <!-- 缓存 -->
    <dependency> 
              <groupId>redis.clients</groupId> 
              <artifactId>jedis</artifactId> 
              <version>2.8.1</version> 
    </dependency> 
    <dependency> 
              <groupId>org.springframework.data</groupId> 
              <artifactId>spring-data-redis</artifactId> 
              <version>1.7.2.RELEASE</version> 
    </dependency>    

      4)在src/main/resources下创建properties文件夹,建立redis-config.properties 

    redis.host=192.168.56.102 
    redis.port=6379 
    redis.pass= 
    redis.database=0 
    redis.maxIdle=300 
    redis.maxWait=3000 
    redis.testOnBorrow=true 
    redis.host即服务器地址

    5)在src/main/resources下创建spring文件夹 ,创建applicationContext-redis.xml

      

       <context:property-placeholder location="classpath*:properties/*.properties" />   
       <!-- redis 相关配置 --> 
       <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  
         <property name="maxIdle" value="${redis.maxIdle}" />   
         <property name="maxWaitMillis" value="${redis.maxWait}" />  
         <property name="testOnBorrow" value="${redis.testOnBorrow}" />  
       </bean>  
       <bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" 
           p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/>  
       
       <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
         <property name="connectionFactory" ref="JedisConnectionFactory" />  
       </bean>  

      

      maxIdle :最大空闲数

      maxWaitMillis:连接时的最大等待毫秒数

      testOnBorrow:在提取一个jedis实例时,是否提前进行验证操作;如果为true,则得到的jedis实例均是可用的;

      

      值类型操作

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
    public class TestValue {
        @Autowired
        private RedisTemplate redisTemplate;    
        @Test
        public void setValue(){
            redisTemplate.boundValueOps("name").set("Michel");        
        }    
        @Test
        public void getValue(){
            String str = (String) redisTemplate.boundValueOps("name").get();
            System.out.println(str);
        }    
        @Test
        public void deleteValue(){
            redisTemplate.delete("name");;
        }    
    }

      输出结果:Michel

     

      Set类型操作

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
    public class TestSet {
        
        @Autowired
        private RedisTemplate redisTemplate;
        
        /**
         * 存入值
         */
        @Test
        public void setValue(){
            redisTemplate.boundSetOps("nameset").add("项羽");        
            redisTemplate.boundSetOps("nameset").add("刘邦");    
            redisTemplate.boundSetOps("nameset").add("韩信");
        }
        
        /**
         * 提取值
         */
        @Test
        public void getValue(){
            Set members = redisTemplate.boundSetOps("nameset").members();
            System.out.println(members);
        }
        
        /**
         * 删除集合中的某一个值
         */
        @Test
        public void deleteValue(){
            redisTemplate.boundSetOps("nameset").remove("项羽");
        }
        
        /**
         * 删除整个集合
         */
        @Test
        public void deleteAllValue(){
            redisTemplate.delete("nameset");
        }
    }

      

      还有其他,如List类型操作、Hash类型操作可以参考官网文档

     

    
    

      

      

  • 相关阅读:
    Android第三次作业
    Android第二次作业
    2016-2017-2软件工程课程总结
    软件工程——个人总结
    软件工程——团队答辩
    软件工程——团队作业4
    软件工程——团队作业3
    软件工程——团队作业2
    软件工程——团队作业1
    软件工程第二次作业——结对编程
  • 原文地址:https://www.cnblogs.com/cracker13/p/9961000.html
Copyright © 2011-2022 走看看