Redis開發用例

Redis依賴的Jar

基於mavenhtml

<!-- redis client Dependency start-->
			<dependency>
				<groupId>redis.clients</groupId>
				<artifactId>jedis</artifactId>
				<version>2.9.0</version>
			</dependency>
			<dependency>
				<groupId>org.apache.commons</groupId>
				<artifactId>commons-pool2</artifactId>
				<version>2.4.2</version>
			</dependency>
<!-- redis client Dependency end -->

Redis客戶端直接鏈接

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.Jedis;

/**
 * Redis測試 .
 * @author longload
 * @date 2016年8月14日下午9:38:32
 */
public class RedisTest {
	private static final Logger LOGGER = LoggerFactory.getLogger(RedisTest.class);

	public static void main(String[] args) {
		
		Jedis jedis = new Jedis("192.168.48.110",6379,1000);
		String key = "test";
		String value = "";
		jedis.del(key); // 刪數據
		jedis.set(key, "test123"); // 存數據
		value = jedis.get(key); // 取數據
		LOGGER.info(key + "=" + value);
	}
}

    而後能夠經過redis-cli 進行查看java

Spring整合

  1.  Xml配置文件(spring-redis.xml)

    固然spring的加載文件須要引入該文件redis

    <?xml version="1.0" encoding="UTF-8"?>
    <beans
    	xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
    	xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns="http://www.springframework.org/schema/beans">
    
    	<!-- Jedis連接池配置 -->
    	<bean class="redis.clients.jedis.JedisPoolConfig" id="jedisPoolConfig">
    		<property value="true" name="testWhileIdle" />
    		<property value="60000" name="minEvictableIdleTimeMillis" />
    		<property value="30000" name="timeBetweenEvictionRunsMillis" />
    		<property value="-1" name="numTestsPerEvictionRun" />
    		<property value="8" name="maxTotal" />
    		<property value="8" name="maxIdle" />
    		<property value="0" name="minIdle" />
    	</bean>
    	<bean class="redis.clients.jedis.ShardedJedisPool" id="shardedJedisPool">
    		<constructor-arg ref="jedisPoolConfig" index="0" />
    		<constructor-arg index="1">
    			<list>
    				<bean class="redis.clients.jedis.JedisShardInfo">
    					<!-- redis服務器地址與端口 -->
    					<constructor-arg value="192.168.48.110" index="0" />
    					<constructor-arg value="6379" index="1" type="int" />
    				</bean>
    			</list>
    		</constructor-arg>
    	</bean>
    </beans>
  2. Spring使用的測試代碼

    import javax.annotation.Resource;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import redis.clients.jedis.ShardedJedis;
    import redis.clients.jedis.ShardedJedisPool;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:spring/spring-context.xml")
    public class RedisSpringTest {
    	@Resource
    	private ShardedJedisPool shardedJedisPool;
    	
    	Logger logger=LoggerFactory.getLogger(this.getClass());
    	@Test
    	 public void testOper(){
    		ShardedJedis shardedJedis =shardedJedisPool.getResource();
    		shardedJedis.set("test22", "11");
    		logger.debug(shardedJedis.get("test22"));
    		shardedJedis.del("test22");
    		logger.debug(shardedJedis.get("test22"));
    		shardedJedis.set("test22", "666");
    		logger.debug(shardedJedis.get("test22"));
    	}
    }

     

  3. redis開發使用的API參照地址

相關文章
相關標籤/搜索