您的位置:首页 > 数据库 > Redis

redis - Java操作

2016-05-25 21:05 561 查看
Jedis 客户端实现

Maven pom文件 加入依赖

[html] view
plain copy

print?

<dependencies>

<dependency>

<groupId>redis.clients</groupId>

<artifactId>jedis</artifactId>

<version>2.1.0</version>

</dependency>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.8.2</version>

<scope>test</scope>

</dependency>

</dependencies>

Jedis 简单使用

[java] view
plain copy

print?

/*

* JedisTest.java

*/

package com.x.java2000_wl;

import org.junit.Before;

import org.junit.Test;

import redis.clients.jedis.Jedis;

/**

* jedis 简单使用

* @author http://blog.csdn.net/java2000_wl
* @version <b>1.0</b>

*/

public class JedisSimpleTest {

private Jedis jedis;

/**

* 初始化连接

* <br>------------------------------<br>

*/

@Before

public void beforeClass() {

jedis = new Jedis("127.0.0.1");

jedis.auth("java2000_wl");

}

/**

* set 新增

* <br>------------------------------<br>

*/

@Test

public void testSet() {

jedis.set("blog", "java2000_wl");

}

/**

* 获取

* <br>------------------------------<br>

*/

@Test

public void testGet() {

System.out.println(jedis.get("blog"));

}

/**

* 修改key

* <br>------------------------------<br>

*/

@Test

public void testRenameKey() {

jedis.rename("blog", "blog_new");

}

/**

* 按key删除

* <br>------------------------------<br>

*/

@Test

public void testDel() {

jedis.del("blog_new");

}

/**

* 获取所有的key

* <br>------------------------------<br>

*/

@Test

public void testKeys() {

System.out.println(jedis.keys("*"));

}

}

使用commons-pool连接池

[java] view
plain copy

print?

/*

* JedisPoolTest.java

*/

package com.x.java2000_wl;

import java.util.ResourceBundle;

import org.junit.Assert;

import org.junit.BeforeClass;

import org.junit.Test;

import redis.clients.jedis.Jedis;

import redis.clients.jedis.JedisPool;

import redis.clients.jedis.JedisPoolConfig;

/**

* jedis Pool 操作

* @author http://blog.csdn.net/java2000_wl
* @version <b>1.0</b>

*/

public class JedisPoolTest {

private static JedisPool jedisPool;

/**

* initPoolConfig

* <br>------------------------------<br>

* @return

*/

private static JedisPoolConfig initPoolConfig() {

JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();

// 控制一个pool最多有多少个状态为idle的jedis实例

jedisPoolConfig.setMaxActive(1000);

// 最大能够保持空闲状态的对象数

jedisPoolConfig.setMaxIdle(300);

// 超时时间

jedisPoolConfig.setMaxWait(1000);

// 在borrow一个jedis实例时,是否提前进行alidate操作;如果为true,则得到的jedis实例均是可用的;

jedisPoolConfig.setTestOnBorrow(true);

// 在还会给pool时,是否提前进行validate操作

jedisPoolConfig.setTestOnReturn(true);

return jedisPoolConfig;

}

/**

* 初始化jedis连接池

* <br>------------------------------<br>

*/

@BeforeClass

public static void before() {

JedisPoolConfig jedisPoolConfig = initPoolConfig();

// 属性文件读取参数信息

ResourceBundle bundle = ResourceBundle.getBundle("redis_config");

String host = bundle.getString("redis.host");

int port = Integer.valueOf(bundle.getString("redis.port"));

int timeout = Integer.valueOf(bundle.getString("redis.timeout"));

String password = bundle.getString("redis.password");

// 构造连接池

jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);

}

@Test

public void testSet() {

Jedis jedis = null;

// 从池中获取一个jedis实例

try {

jedis = jedisPool.getResource();

jedis.set("blog_pool", "java2000_wl");

} catch (Exception e) {

// 销毁对象

jedisPool.returnBrokenResource(jedis);

Assert.fail(e.getMessage());

} finally {

// 还会到连接池

jedisPool.returnResource(jedis);

}

}

@Test

public void testGet() {

Jedis jedis = null;

try {

// 从池中获取一个jedis实例

jedis = jedisPool.getResource();

System.out.println(jedis.get("blog_pool"));

} catch (Exception e) {

// 销毁对象

jedisPool.returnBrokenResource(jedis);

Assert.fail(e.getMessage());

} finally {

// 还会到连接池

jedisPool.returnResource(jedis);

}

}

}

切记: 当出现异常时 要销毁对象 returnBrokenResource, 使用完之后要 还会连接returnResource

原文地址:/article/1599522.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: