Redis概述

Redis的java API:

public class JedisTest {
	
	public void testJedis() {
		Jedis jedis = testPool().getResource();
		// Jedis jedis = new Jedis("localhost", 6379); //连接Redis
		// jedis.auth("password");//如果需密码
		int i = 0;// 记录操作次数
		try {
			long start = System.currentTimeMillis();// 开始毫秒数
			while (true) {
				long end = System.currentTimeMillis();
				if (end - start >= 1000) {// 当大于等于1000毫秒(相当于1秒)时,结束操作
					break;
				}
				i++;
				jedis.set("test" + i, i + "");
			}
		} finally {// 关闭连接
			jedis.close();
		}
		System.out.println("redis每秒操作:" + i + "次");// 打印1秒内对Redis的操作次数
		
	}

	private JedisPool testPool() {
          //JAVA 配置 pool JedisPoolConfig poolCfg = new JedisPoolConfig(); // 最大空闲数 poolCfg.setMaxIdle(50); // 最大连接数 poolCfg.setMaxTotal(100); // 最大等待毫秒数 poolCfg.setMaxWaitMillis(20000); // 使用配置创建连接池 JedisPool pool = new JedisPool(poolCfg, "localhost"); // 从连接池中获取单个连接 //Jedis jedis = pool.getResource(); // 如果需密码 // jedis.auth("password"); return pool; } }

 java API对于sring可行,如果要保存类则需要自己编写规则序列化转换,可以用Spring中的RedisTemplate redis;

   XML配置:

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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: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-4.0.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<!--最大空闲数 -->
		<property name="maxIdle" value="50" />
		<!--最大连接数 -->
		<property name="maxTotal" value="100" />
		<!--最大等待时间 -->
		<property name="maxWaitMillis" value="20000" />
	</bean>

	<bean id="connectionFactory"
		class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
		<property name="hostName" value="localhost" />
		<property name="port" value="6379" />
		<!--<property name="password" value="paasword"/> -->
		<property name="poolConfig" ref="poolConfig" />
	</bean>
      <!--序列化-->
	<bean id="jdkSerializationRedisSerializer" 
		class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
		
	<bean id="stringRedisSerializer"
		class="org.springframework.data.redis.serializer.StringRedisSerializer" />
		
	<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
		<property name="connectionFactory" ref="connectionFactory" />
		<property name="keySerializer" ref="stringRedisSerializer" />
		<property name="valueSerializer" ref="jdkSerializationRedisSerializer" />
	</bean>
</beans>

 测试:

public class Chapter17Main {

	public static void main(String[] args) {
		testSessionCallback();
	}

	private static void testJedis() {
		JedisTest jedisTest = new JedisTest();
		jedisTest.testJedis();
	}
      //template 的set, get 可能来自同一pool的不同连接
	private static void testSpring() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);
		Role role = new Role();
		role.setId(1L);
		role.setRoleName("role_name_1");
		role.setNote("note_1");
		redisTemplate.opsForValue().set("role_1", role);
		Role role1 = (Role) redisTemplate.opsForValue().get("role_1");
		System.out.println(role1.getRoleName());
	}

    //SessionCallback可以保证set与get方法来自同一连接 private static void testSessionCallback() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class); Role role = new Role(); role.setId(1); role.setRoleName("role_name_1"); role.setNote("role_note_1"); SessionCallback callBack = new SessionCallback<Role>() { @Override public Role execute(RedisOperations ops) throws DataAccessException { ops.boundValueOps("role_1").set(role); return (Role) ops.boundValueOps("role_1").get(); } }; Role savedRole = (Role) redisTemplate.execute(callBack); System.out.println(savedRole.getId()); } }

  

 

 

原文地址:https://www.cnblogs.com/daxiong225/p/9977429.html