Redis

Posted argor

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis相关的知识,希望对你有一定的参考价值。

 1.QuickStart         last


1.QuickStart

  • 导包:spring-data-redis、jedis、slf4j-nop、junit、spring-test.
    技术分享图片
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.pinyougou.demo.redis</groupId>
        <artifactId>redisdemo1</artifactId>
        <version>1.1</version>
    
        <properties>
            <spring.version>4.2.4.RELEASE</spring.version>
        </properties>
        <dependencies>
            <!-- Spring -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jms</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
            </dependency>
    
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.9</version>
            </dependency>
            <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>
    
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-nop</artifactId>
                <version>1.7.25</version>
                <scope>test</scope>
            </dependency>
    
    
        </dependencies>
    </project>
    View Code

     

    技术分享图片
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>cn.argor</groupId>
        <artifactId>redisdemo</artifactId>
        <version>1.1</version>
        
        <dependencies>
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-redis</artifactId>
                <version>1.7.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.9</version>
            </dependency>
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.8.2</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-nop</artifactId>
                <version>1.7.25</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>4.2.6.RELEASE</version>
            </dependency>
        </dependencies>
    </project>
    View Code

     

  • 配置文件
    • spring
      技术分享图片
      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
          xmlns:context="http://www.springframework.org/schema/context"
          xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:cache="http://www.springframework.org/schema/cache"
          xsi:schemaLocation="http://www.springframework.org/schema/beans   
                  http://www.springframework.org/schema/beans/spring-beans.xsd   
                  http://www.springframework.org/schema/context   
                  http://www.springframework.org/schema/context/spring-context.xsd   
                  http://www.springframework.org/schema/mvc   
                  http://www.springframework.org/schema/mvc/spring-mvc.xsd 
                  http://www.springframework.org/schema/cache  
                  http://www.springframework.org/schema/cache/spring-cache.xsd">
      
          <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>
      
      </beans>  
      View Code
    • redis
      技术分享图片
      # Redis settings 
      # server IP 
      redis.host=127.0.0.1
      # server port 
      redis.port=6379
      # server pass 
      redis.pass=
      # use dbIndex 
      redis.database=0
      # u63A7u5236u4E00u4E2Apoolu6700u591Au6709u591Au5C11u4E2Au72B6u6001u4E3Aidle(u7A7Au95F2u7684)u7684jedisu5B9Eu4F8B 
      redis.maxIdle=300
      # u8868u793Au5F53borrow(u5F15u5165)u4E00u4E2Ajedisu5B9Eu4F8Bu65F6uFF0Cu6700u5927u7684u7B49u5F85u65F6u95F4uFF0Cu5982u679Cu8D85u8FC7u7B49u5F85u65F6u95F4(u6BEBu79D2)uFF0Cu5219u76F4u63A5u629Bu51FAJedisConnectionExceptionuFF1B  
      redis.maxWait=3000
      # u5728borrowu4E00u4E2Ajedisu5B9Eu4F8Bu65F6uFF0Cu662Fu5426u63D0u524Du8FDBu884Cvalidateu64CDu4F5CuFF1Bu5982u679Cu4E3AtrueuFF0Cu5219u5F97u5230u7684jedisu5B9Eu4F8Bu5747u662Fu53EFu7528u7684  
      redis.testOnBorrow=true
      View Code
    • log
      技术分享图片
      ### set ??¥????o§??? - for more verbose logging change ‘info‘ to ‘debug‘ ###
      log4j.rootLogger=info, stdout
      
      ### è????o?????ˉ??°??§?????? ###
      log4j.appender.stdout = org.apache.log4j.ConsoleAppender
      log4j.appender.stdout.Target = System.out
      log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
      log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n
      
      ### è????oDEBUG ?o§?????¥????????¥?????°=E://logs/error.log ###
      #log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
      #log4j.appender.D.File = E://logs/log.log
      #log4j.appender.D.Append = true
      #log4j.appender.D.Threshold = DEBUG 
      #log4j.appender.D.layout = org.apache.log4j.PatternLayout
      #log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
      
      ### è????oERROR ?o§?????¥????????¥?????°=E://logs/error.log ###
      #log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
      #log4j.appender.E.File =E://logs/error.log 
      #log4j.appender.E.Append = true
      #log4j.appender.E.Threshold = ERROR 
      #log4j.appender.E.layout = org.apache.log4j.PatternLayout
      #log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
      View Code

       

  • 测试类
    • 单值
      技术分享图片
      package test;
      
      import org.junit.runner.RunWith;
      import org.junit.Test;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.data.redis.core.RedisTemplate;
      import org.springframework.test.context.ContextConfiguration;
      import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
      
      @RunWith(SpringJUnit4ClassRunner.class)
      @ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
      public class TestValue {
          @Autowired
          private RedisTemplate<String, String> redisTemp;
          
          @Test
          public void setValue() { 
              redisTemp.boundValueOps("name").set("argor");
          }
          
          @Test
          public void getValue() {
              
              String value = redisTemp.boundValueOps("name").get();
              System.out.println(value);
          }
          
          @Test
          public void deleteValue() {
              redisTemp.delete("name");
          }
      }
      View Code

       

 

 

 

 

 

 

 

 

redis .


以上是关于Redis的主要内容,如果未能解决你的问题,请参考以下文章

redis存储session配制方法

Redis实现分布式锁(设计模式应用实战)

Redis实现分布式锁(设计模式应用实战)

Redis缓存:java语言注释符号,附超全教程文档

spring boot 整合 redis

Redis 学习 —— 数据类型及操作