Redis使用随笔

Posted suheng

tags:

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

库配置

<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
   <groupId>redis.clients</groupId>
   <artifactId>jedis</artifactId>
   <version>2.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
<dependency>
   <groupId>org.springframework.data</groupId>
   <artifactId>spring-data-redis</artifactId>
   <version>1.6.2.RELEASE</version>
</dependency>

Redis属性配置

redis.properties

#redis setting  
redis.host=127.0.0.1
redis.port=6379
redis.pass=123456
redis.maxIdle=100
redis.maxActive=300
redis.maxWait=1000
redis.testOnBorrow=true
redis.timeout=100000
redis.dbIndex=0
fep.local.cache.capacity =10000

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:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
     http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.2.xsd">
   <context:property-placeholder location="classpath:redis.properties" />
   <bean
      id="poolConfig"
      class="redis.clients.jedis.JedisPoolConfig">
      <property
         name="maxIdle"
         value="${redis.maxIdle}" />
      <property
         name="maxTotal"
         value="${redis.maxActive}" />
      <property
         name="maxWaitMillis"
         value="${redis.maxWait}" />
      <property
         name="testOnBorrow"
         value="${redis.testOnBorrow}" />
   </bean>
   <bean
      id="connectionFactory"
      class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
      <property
         name="hostName"
         value="${redis.host}" />
      <property
         name="port"
         value="${redis.port}" />
      <property
         name="database"
         value="${redis.dbIndex}" />
      <property
         name="poolConfig"
         ref="poolConfig" />
   </bean>
   <bean
      id="redisTemplate"
      class="org.springframework.data.redis.core.RedisTemplate">
      <property
         name="connectionFactory"
         ref="connectionFactory" />
      <property name="keySerializer">
         <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
      </property>
      <property name="valueSerializer">
         <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
      </property>
   </bean>
</beans>

测试

public class App {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        RedisTemplate rt = ctx.getBean(RedisTemplate.class);
        rt.opsForValue().set("Test", "Redis test!", 10, TimeUnit.SECONDS);
        String x = (String) rt.opsForValue().get("Test");
        System.out.println(x);
    }
}

 

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

Redis随笔JedisjedisCluster的使用

Python 随笔之Redis

redis数据库随笔

redis事务随笔

Redis内存模型及应用解读 读后随笔

redis随笔