增加redis、memcached用例
This commit is contained in:
		@@ -0,0 +1,25 @@
 | 
			
		||||
package com.heibaiying.bean;
 | 
			
		||||
 | 
			
		||||
import lombok.AllArgsConstructor;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import lombok.NoArgsConstructor;
 | 
			
		||||
 | 
			
		||||
import java.util.Date;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author : heibaiying
 | 
			
		||||
 * @description :
 | 
			
		||||
 */
 | 
			
		||||
@Data
 | 
			
		||||
@NoArgsConstructor
 | 
			
		||||
@AllArgsConstructor
 | 
			
		||||
public class Programmer {
 | 
			
		||||
 | 
			
		||||
    private String name;
 | 
			
		||||
 | 
			
		||||
    private int age;
 | 
			
		||||
 | 
			
		||||
    private float salary;
 | 
			
		||||
 | 
			
		||||
    private Date birthday;
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,5 @@
 | 
			
		||||
redis.host=127.0.0.1
 | 
			
		||||
redis.port=6379
 | 
			
		||||
redis.timeout=2000
 | 
			
		||||
redis.maxIdle=8
 | 
			
		||||
redis.maxTotal=16
 | 
			
		||||
							
								
								
									
										28
									
								
								spring/spring-redis/src/main/resources/jedis/jedis.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								spring/spring-redis/src/main/resources/jedis/jedis.xml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
			
		||||
<?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:context="http://www.springframework.org/schema/context"
 | 
			
		||||
       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-4.1.xsd">
 | 
			
		||||
 | 
			
		||||
    <!--指定配置文件的位置-->
 | 
			
		||||
    <context:property-placeholder location="classpath:jedis/jedis.properties"/>
 | 
			
		||||
 | 
			
		||||
    <!--初始化连接池配置-->
 | 
			
		||||
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
 | 
			
		||||
        <property name="maxIdle" value="${redis.maxIdle}"/>
 | 
			
		||||
        <property name="maxTotal" value="${redis.maxTotal}"/>
 | 
			
		||||
    </bean>
 | 
			
		||||
 | 
			
		||||
    <!--配置jedis连接池-->
 | 
			
		||||
    <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
 | 
			
		||||
        <constructor-arg name="poolConfig" ref="jedisPoolConfig"/>
 | 
			
		||||
        <constructor-arg name="host" value="${redis.host}"/>
 | 
			
		||||
        <constructor-arg name="port" value="${redis.port}"/>
 | 
			
		||||
        <constructor-arg name="timeout" value="${redis.timeout}"/>
 | 
			
		||||
    </bean>
 | 
			
		||||
 | 
			
		||||
    <!--把 jedis 创建与销毁交给spring 来管理 这个类建议声明为非单例模式 不然每次获得的就是同一个链接-->
 | 
			
		||||
    <bean id="jedis" factory-bean="jedisPool" factory-method="getResource" destroy-method="close" scope="prototype"/>
 | 
			
		||||
 | 
			
		||||
</beans>
 | 
			
		||||
@@ -0,0 +1,39 @@
 | 
			
		||||
<?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:context="http://www.springframework.org/schema/context"
 | 
			
		||||
       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-4.1.xsd">
 | 
			
		||||
 | 
			
		||||
    <!--指定配置文件的位置-->
 | 
			
		||||
    <context:property-placeholder location="classpath:jedis/jedis.properties"/>
 | 
			
		||||
 | 
			
		||||
    <!--初始化连接池配置-->
 | 
			
		||||
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
 | 
			
		||||
        <property name="maxIdle" value="${redis.maxIdle}"/>
 | 
			
		||||
        <property name="maxTotal" value="${redis.maxTotal}"/>
 | 
			
		||||
    </bean>
 | 
			
		||||
 | 
			
		||||
    <!--配置jedis连接池(集群)-->
 | 
			
		||||
    <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
 | 
			
		||||
        <constructor-arg name="nodes">
 | 
			
		||||
            <set>
 | 
			
		||||
                <bean class="redis.clients.jedis.HostAndPort">
 | 
			
		||||
                    <constructor-arg name="host" value="127.0.0.1"/>
 | 
			
		||||
                    <constructor-arg name="port" value="6379"/>
 | 
			
		||||
                </bean>
 | 
			
		||||
                <bean class="redis.clients.jedis.HostAndPort">
 | 
			
		||||
                    <constructor-arg name="host" value="127.0.0.1"/>
 | 
			
		||||
                    <constructor-arg name="port" value="6380"/>
 | 
			
		||||
                </bean>
 | 
			
		||||
                <bean class="redis.clients.jedis.HostAndPort">
 | 
			
		||||
                    <constructor-arg name="host" value="127.0.0.1"/>
 | 
			
		||||
                    <constructor-arg name="port" value="6381"/>
 | 
			
		||||
                </bean>
 | 
			
		||||
            </set>
 | 
			
		||||
        </constructor-arg>
 | 
			
		||||
        <constructor-arg name="timeout" value="${redis.timeout}"/>
 | 
			
		||||
    </bean>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
</beans>
 | 
			
		||||
							
								
								
									
										28
									
								
								spring/spring-redis/src/main/resources/redisson/redisson.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								spring/spring-redis/src/main/resources/redisson/redisson.xml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
			
		||||
<?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:redisson="http://redisson.org/schema/redisson"
 | 
			
		||||
       xsi:schemaLocation="http://www.springframework.org/schema/beans
 | 
			
		||||
       http://www.springframework.org/schema/beans/spring-beans.xsd
 | 
			
		||||
       http://redisson.org/schema/redisson
 | 
			
		||||
       http://redisson.org/schema/redisson/redisson.xsd">
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    <redisson:client>
 | 
			
		||||
        <!--关于配置说明见官方文档 2.6.2. 通过JSON、YAML和Spring XML文件配置单节点模式
 | 
			
		||||
         <a src="https://github.com/redisson/redisson/wiki/2.-%E9%85%8D%E7%BD%AE%E6%96%B9%E6%B3%95#26-%E5%8D%95redis%E8%8A%82%E7%82%B9%E6%A8%A1%E5%BC%8F"> -->
 | 
			
		||||
        <redisson:single-server
 | 
			
		||||
                address="redis://127.0.0.1:6379"
 | 
			
		||||
                idle-connection-timeout="10000"
 | 
			
		||||
                ping-timeout="1000"
 | 
			
		||||
                connect-timeout="10000"
 | 
			
		||||
                timeout="3000"
 | 
			
		||||
                retry-attempts="3"
 | 
			
		||||
                retry-interval="1500"
 | 
			
		||||
                connection-minimum-idle-size="10"
 | 
			
		||||
                connection-pool-size="64"
 | 
			
		||||
                database="0"
 | 
			
		||||
        />
 | 
			
		||||
    </redisson:client>
 | 
			
		||||
 | 
			
		||||
</beans>
 | 
			
		||||
@@ -0,0 +1,18 @@
 | 
			
		||||
<?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:redisson="http://redisson.org/schema/redisson"
 | 
			
		||||
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 | 
			
		||||
          http://redisson.org/schema/redisson http://redisson.org/schema/redisson/redisson.xsd">
 | 
			
		||||
 | 
			
		||||
    <!-- 最基本配置 -->
 | 
			
		||||
    <redisson:client>
 | 
			
		||||
        <!--集群更多配置参数见官方文档 2.4.2 通过JSON、YAML和Spring XML文件配置集群模式
 | 
			
		||||
         <a src="https://github.com/redisson/redisson/wiki/2.-%E9%85%8D%E7%BD%AE%E6%96%B9%E6%B3%95#24-%E9%9B%86%E7%BE%A4%E6%A8%A1%E5%BC%8F"> -->
 | 
			
		||||
        <redisson:cluster-servers>
 | 
			
		||||
            <redisson:node-address value="redis://127.0.0.1:6379"/>
 | 
			
		||||
            <redisson:node-address value="redis://127.0.0.1:6380"/>
 | 
			
		||||
            <redisson:node-address value="redis://127.0.0.1:6381"/>
 | 
			
		||||
        </redisson:cluster-servers>
 | 
			
		||||
    </redisson:client>
 | 
			
		||||
</beans>
 | 
			
		||||
							
								
								
									
										18
									
								
								spring/spring-redis/src/main/resources/springApplication.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								spring/spring-redis/src/main/resources/springApplication.xml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,18 @@
 | 
			
		||||
<?xml version="1.0" encoding="UTF-8"?>
 | 
			
		||||
<beans xmlns="http://www.springframework.org/schema/beans"
 | 
			
		||||
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 | 
			
		||||
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 | 
			
		||||
 | 
			
		||||
    <!--jedis 连接单机redis-->
 | 
			
		||||
    <import resource="classpath:jedis/jedis.xml"/>
 | 
			
		||||
 | 
			
		||||
    <!--jedis 连接集群redis-->
 | 
			
		||||
    <!--<import resource="classpath:jedis/jedisCluster.xml"/>-->
 | 
			
		||||
 | 
			
		||||
    <!--redisson 连接单机redis-->
 | 
			
		||||
    <import resource="classpath:redisson/redisson.xml"/>
 | 
			
		||||
 | 
			
		||||
    <!--redisson 连接集群redis-->
 | 
			
		||||
   <!--<import resource="classpath:redisson/redissonCluster.xml"/>-->
 | 
			
		||||
 | 
			
		||||
</beans>
 | 
			
		||||
@@ -0,0 +1,45 @@
 | 
			
		||||
package com.heibaiying.jedis;
 | 
			
		||||
 | 
			
		||||
import org.junit.After;
 | 
			
		||||
import org.junit.Before;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.junit.runner.RunWith;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.test.context.ContextConfiguration;
 | 
			
		||||
import org.springframework.test.context.junit4.SpringRunner;
 | 
			
		||||
import redis.clients.jedis.Jedis;
 | 
			
		||||
import redis.clients.jedis.JedisCluster;
 | 
			
		||||
import redis.clients.jedis.JedisPool;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author : heibaiying
 | 
			
		||||
 * @description :redis 集群测试
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@RunWith(SpringRunner.class)
 | 
			
		||||
@ContextConfiguration({"classpath:springApplication.xml"})
 | 
			
		||||
public class JedisClusterSamples {
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private JedisCluster jedisCluster;
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void Set() {
 | 
			
		||||
        jedisCluster.set("hello", "spring");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void Get() {
 | 
			
		||||
        String s = jedisCluster.get("hello");
 | 
			
		||||
        System.out.println(s);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void setEx() {
 | 
			
		||||
        String s = jedisCluster.setex("spring", 10, "我会在10秒后过期");
 | 
			
		||||
        System.out.println(s);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,43 @@
 | 
			
		||||
package com.heibaiying.jedis;
 | 
			
		||||
 | 
			
		||||
import org.junit.After;
 | 
			
		||||
import org.junit.Before;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.junit.runner.RunWith;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.test.context.ContextConfiguration;
 | 
			
		||||
import org.springframework.test.context.junit4.SpringRunner;
 | 
			
		||||
import redis.clients.jedis.Jedis;
 | 
			
		||||
import redis.clients.jedis.JedisPool;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author : heibaiying
 | 
			
		||||
 * @description :redis 单机版测试
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@RunWith(SpringRunner.class)
 | 
			
		||||
@ContextConfiguration({"classpath:springApplication.xml"})
 | 
			
		||||
public class JedisSamples {
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private Jedis jedis;
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void Set() {
 | 
			
		||||
        jedis.set("hello", "spring");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void Get() {
 | 
			
		||||
        String s = jedis.get("hello");
 | 
			
		||||
        System.out.println(s);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void setEx() {
 | 
			
		||||
        String s = jedis.setex("spring", 10, "我会在10秒后过期");
 | 
			
		||||
        System.out.println(s);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,49 @@
 | 
			
		||||
package com.heibaiying.redisson;
 | 
			
		||||
 | 
			
		||||
import com.heibaiying.bean.Programmer;
 | 
			
		||||
import org.junit.After;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.junit.runner.RunWith;
 | 
			
		||||
import org.redisson.api.RBucket;
 | 
			
		||||
import org.redisson.api.RedissonClient;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.test.context.ContextConfiguration;
 | 
			
		||||
import org.springframework.test.context.junit4.SpringRunner;
 | 
			
		||||
 | 
			
		||||
import java.util.Date;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author : heibaiying
 | 
			
		||||
 * @description :redisson 对象序列化与反序列化
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@RunWith(SpringRunner.class)
 | 
			
		||||
@ContextConfiguration({"classpath:springApplication.xml"})
 | 
			
		||||
public class RedissonObjectSamples {
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private RedissonClient redissonClient;
 | 
			
		||||
 | 
			
		||||
    // Redisson的对象编码类是用于将对象进行序列化和反序列化 默认采用Jackson
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void Set() {
 | 
			
		||||
        RBucket<Programmer> rBucket = redissonClient.getBucket("programmer");
 | 
			
		||||
        rBucket.set(new Programmer("xiaoming", 12, 5000.21f, new Date()));
 | 
			
		||||
        redissonClient.shutdown();
 | 
			
		||||
        //存储结果: {"@class":"com.heibaiying.bean.Programmer","age":12,"birthday":["java.util.Date",1545714986590],"name":"xiaoming","salary":5000.21}
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void Get() {
 | 
			
		||||
        RBucket<Programmer> rBucket = redissonClient.getBucket("programmer");
 | 
			
		||||
        System.out.println(rBucket.get());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @After
 | 
			
		||||
    public void close() {
 | 
			
		||||
        redissonClient.shutdown();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,54 @@
 | 
			
		||||
package com.heibaiying.redisson;
 | 
			
		||||
 | 
			
		||||
import org.junit.After;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.junit.runner.RunWith;
 | 
			
		||||
import org.redisson.api.RBucket;
 | 
			
		||||
import org.redisson.api.RedissonClient;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.test.context.ContextConfiguration;
 | 
			
		||||
import org.springframework.test.context.junit4.SpringRunner;
 | 
			
		||||
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author : heibaiying
 | 
			
		||||
 * @description :redisson 操作普通对象
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@RunWith(SpringRunner.class)
 | 
			
		||||
@ContextConfiguration({"classpath:springApplication.xml"})
 | 
			
		||||
public class RedissonSamples {
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private RedissonClient redissonClient;
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void Set() {
 | 
			
		||||
        // key 存在则更新 不存在则删除
 | 
			
		||||
        RBucket<String> rBucket = redissonClient.getBucket("redisson");
 | 
			
		||||
        rBucket.set("firstValue");
 | 
			
		||||
        redissonClient.shutdown();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void Get() {
 | 
			
		||||
        // key 存在则更新 不存在则删除
 | 
			
		||||
        RBucket<String> rBucket = redissonClient.getBucket("redisson");
 | 
			
		||||
        System.out.println(rBucket.get());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void SetEx() {
 | 
			
		||||
        // key 存在则更新 不存在则删除
 | 
			
		||||
        RBucket<String> rBucket = redissonClient.getBucket("redissonEx");
 | 
			
		||||
        rBucket.set("我在十秒后会消失", 10, TimeUnit.SECONDS);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @After
 | 
			
		||||
    public void close() {
 | 
			
		||||
        redissonClient.shutdown();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user