Redis开发用例
博客专区 > longload 的博客 > 博客详情
Redis开发用例
longload 发表于1年前
Redis开发用例
  • 发表于 1年前
  • 阅读 44
  • 收藏 1
  • 点赞 0
  • 评论 0

华为云·免费上云实践>>>   

摘要: Redis安装请参照前面

Redis依赖的Jar

基于maven

<!-- 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 进行查看

Spring整合

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

    当然spring的加载文件需要引入该文件

    <?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参照地址

共有 人打赏支持
粉丝 4
博文 66
码字总数 104702
×
longload
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: