mirror of https://github.com/dunwu/db-tutorial.git
🔖 jedis 示例
parent
155dd8e23b
commit
e38cc241ed
|
@ -32,6 +32,16 @@
|
|||
<artifactId>logback-classic</artifactId>
|
||||
</dependency>
|
||||
<!-- log end -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context-support</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<!-- RELATIONSHIP SETTINGS END -->
|
||||
|
||||
|
|
|
@ -1,50 +1,147 @@
|
|||
package io.github.dunwu.javadb;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.exceptions.JedisConnectionException;
|
||||
|
||||
/**
|
||||
* Jedis 测试例
|
||||
* @see https://github.com/xetorthio/jedis
|
||||
* @author Zhang Peng
|
||||
*/
|
||||
public class JedisDemoTest {
|
||||
private static final String REDIS_HOST = "192.168.58.170";
|
||||
private static final int REDIS_PORT = 6379;
|
||||
private static final String REDIS_PASSWORD = "zp";
|
||||
private static Jedis jedis = null;
|
||||
private static Logger logger = LoggerFactory.getLogger(JedisDemoTest.class);
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() {
|
||||
// Jedis 有多种构造方法,这里选用最简单的一种情况
|
||||
jedis = new Jedis(REDIS_HOST, REDIS_PORT);
|
||||
jedis.auth(REDIS_PASSWORD);
|
||||
System.out.println("ping redis: " + jedis.ping());
|
||||
|
||||
// 触发 ping 命令
|
||||
try {
|
||||
jedis.ping();
|
||||
logger.debug("jedis 连接成功。");
|
||||
} catch (JedisConnectionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSet() {
|
||||
jedis.set("first", "hello world");
|
||||
System.out.println("first:" + jedis.get("first"));
|
||||
logger.debug("first: {}", jedis.get("first"));
|
||||
@AfterClass
|
||||
public static void afterClass() {
|
||||
if (null != jedis) {
|
||||
jedis.close();
|
||||
logger.debug("jedis 关闭连接。");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 增删改 string 类型
|
||||
*/
|
||||
@Test
|
||||
public void testLpush() {
|
||||
public void testString() {
|
||||
final String key = "word";
|
||||
final String value1 = "first";
|
||||
final String value2 = "second";
|
||||
|
||||
// 新增 key
|
||||
jedis.set(key, value1);
|
||||
Assert.assertEquals(value1, jedis.get(key));
|
||||
|
||||
// 修改 key
|
||||
jedis.set(key, value2);
|
||||
Assert.assertEquals(value2, jedis.get(key));
|
||||
|
||||
Assert.assertEquals(true, jedis.exists(key));
|
||||
|
||||
// 删除 key
|
||||
jedis.del(key);
|
||||
Assert.assertEquals(null, jedis.get(key));
|
||||
Assert.assertEquals(false, jedis.exists(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 增删改 byte[] 类型(本质也是 string 类型)
|
||||
*/
|
||||
@Test
|
||||
public void testBytes() {
|
||||
final byte[] key = "word".getBytes();
|
||||
final byte[] value1 = "first".getBytes();
|
||||
final byte[] value2 = "second".getBytes();
|
||||
|
||||
// 新增 key
|
||||
jedis.set(key, value1);
|
||||
Assert.assertArrayEquals(value1, jedis.get(key));
|
||||
|
||||
// 修改 key
|
||||
jedis.set(key, value2);
|
||||
Assert.assertArrayEquals(value2, jedis.get(key));
|
||||
|
||||
// 删除 key
|
||||
jedis.del(key);
|
||||
Assert.assertArrayEquals(null, jedis.get(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 增删改 Hash 类型
|
||||
*/
|
||||
@Test
|
||||
public void testHash() {
|
||||
final String key = "zpkey";
|
||||
final String field1 = "first";
|
||||
final String value1 = "一";
|
||||
final String value1_1 = "1";
|
||||
final String field2 = "second";
|
||||
final String value2 = "二";
|
||||
|
||||
// 新增 field
|
||||
jedis.hset(key, field1, value1);
|
||||
jedis.hset(key, field2, value2);
|
||||
Assert.assertEquals(value1, jedis.hget(key, field1));
|
||||
Assert.assertEquals(value2, jedis.hget(key, field2));
|
||||
|
||||
// 修改 field
|
||||
jedis.hset(key, field1, value1_1);
|
||||
Assert.assertEquals(value1_1, jedis.hget(key, field1));
|
||||
|
||||
jedis.hdel(key, field1, value1_1);
|
||||
Assert.assertEquals(null, jedis.hget(key, field1));
|
||||
|
||||
Assert.assertEquals(false, jedis.hexists(key, field1));
|
||||
Assert.assertEquals(true, jedis.hexists(key, field2));
|
||||
|
||||
Map<String, String> results = jedis.hgetAll(key);
|
||||
Assert.assertEquals(1, results.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* set & get 命令
|
||||
*/
|
||||
@Test
|
||||
public void testList() {
|
||||
final String key = "colors";
|
||||
// 存储数据到列表中
|
||||
jedis.lpush("colors", "Red");
|
||||
jedis.lpush("colors", "Yellow");
|
||||
jedis.lpush("colors", "Blue");
|
||||
jedis.lpush(key, "Red");
|
||||
jedis.lpush(key, "Yellow");
|
||||
jedis.lpush(key, "Blue");
|
||||
Assert.assertEquals(3L, jedis.llen(key).longValue());
|
||||
|
||||
// 获取存储的数据并输出
|
||||
List<String> list = jedis.lrange("colors", 0, 2);
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
System.out.println("列表项为: " + list.get(i));
|
||||
for (String aList : list) {
|
||||
System.out.println("列表项为: " + aList);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -52,9 +149,7 @@ public class JedisDemoTest {
|
|||
public void testKeys() {
|
||||
// 存储数据到列表中
|
||||
Set<String> keys = jedis.keys("*");
|
||||
Iterator<String> it = keys.iterator();
|
||||
while (it.hasNext()) {
|
||||
String key = it.next();
|
||||
for (String key : keys) {
|
||||
System.out.println(key);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,72 @@
|
|||
package io.github.dunwu.javadb;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author Zhang Peng
|
||||
*/
|
||||
@ActiveProfiles("test")
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "classpath:/applicationContext.xml" })
|
||||
public class JedisPoolDemoTest {
|
||||
private static Logger logger = LoggerFactory.getLogger(JedisPoolDemoTest.class);
|
||||
|
||||
@Autowired
|
||||
private JedisPool jedisPool;
|
||||
|
||||
@Test
|
||||
public void testSet() {
|
||||
Jedis jedis = jedisPool.getResource();
|
||||
jedis.set("first", "hello world");
|
||||
System.out.println("first:" + jedis.get("first"));
|
||||
logger.debug("first: {}", jedis.get("first"));
|
||||
jedis.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLpush() {
|
||||
Jedis jedis = jedisPool.getResource();
|
||||
|
||||
// 存储数据到列表中
|
||||
jedis.lpush("colors", "Red");
|
||||
jedis.lpush("colors", "Yellow");
|
||||
jedis.lpush("colors", "Blue");
|
||||
// 获取存储的数据并输出
|
||||
List<String> list = jedis.lrange("colors", 0, 2);
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
System.out.println("列表项为: " + list.get(i));
|
||||
}
|
||||
|
||||
jedis.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeys() {
|
||||
Jedis jedis = jedisPool.getResource();
|
||||
|
||||
// 存储数据到列表中
|
||||
Set<String> keys = jedis.keys("*");
|
||||
Iterator<String> it = keys.iterator();
|
||||
while (it.hasNext()) {
|
||||
String key = it.next();
|
||||
System.out.println(key);
|
||||
}
|
||||
|
||||
jedis.close();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
<?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"
|
||||
default-lazy-init="false">
|
||||
|
||||
<description>Spring基础配置</description>
|
||||
|
||||
<import resource="classpath:/config.xml"/>
|
||||
<import resource="classpath:/redis.xml"/>
|
||||
|
||||
</beans>
|
|
@ -0,0 +1,19 @@
|
|||
<?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.xsd">
|
||||
|
||||
<!-- 开发环境配置文件 -->
|
||||
<beans profile="dev">
|
||||
<context:property-placeholder ignore-resource-not-found="true" location="classpath*:/properties/application.properties,
|
||||
classpath*:/properties/application-dev.properties"/>
|
||||
</beans>
|
||||
|
||||
<!-- 测试环境配置文件 -->
|
||||
<beans profile="test">
|
||||
<context:property-placeholder ignore-resource-not-found="true" location="classpath*:/properties/application.properties,
|
||||
classpath*:/properties/application-test.properties"/>
|
||||
</beans>
|
||||
|
||||
</beans>
|
|
@ -0,0 +1,8 @@
|
|||
redis.name=redis-default
|
||||
redis.host=127.0.0.1
|
||||
redis.port=6379
|
||||
redis.timeout=3000
|
||||
redis.password=zp
|
||||
redis.database=0
|
||||
|
||||
log.path=./
|
|
@ -0,0 +1,8 @@
|
|||
redis.name=redis-default
|
||||
redis.host=192.168.58.170
|
||||
redis.port=6379
|
||||
redis.timeout=3000
|
||||
redis.password=zp
|
||||
redis.database=0
|
||||
|
||||
log.path=/home/zp/log
|
|
@ -0,0 +1,5 @@
|
|||
# jedis pool
|
||||
jedis.pool.maxTotal=200
|
||||
jedis.pool.maxIdle=10
|
||||
jedis.pool.maxWaitMillis=1000
|
||||
jedis.pool.testOnBorrow=true
|
|
@ -0,0 +1,22 @@
|
|||
<?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.xsd">
|
||||
|
||||
<description>redis configuration</description>
|
||||
|
||||
<!-- redis配置 -->
|
||||
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
|
||||
<property name="maxTotal" value="${jedis.pool.maxTotal}"/>
|
||||
<property name="maxIdle" value="${jedis.pool.maxIdle}"/>
|
||||
<property name="maxWaitMillis" value="${jedis.pool.maxWaitMillis}"/>
|
||||
<property name="testOnBorrow" value="${jedis.pool.testOnBorrow}"/>
|
||||
</bean>
|
||||
|
||||
<!-- jedis pool配置 -->
|
||||
<bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="destroy" depends-on="jedisPoolConfig">
|
||||
<constructor-arg ref="jedisPoolConfig"/>
|
||||
<constructor-arg type="java.lang.String" value="${redis.host}"/>
|
||||
<constructor-arg type="int" value="${redis.port}"/>
|
||||
</bean>
|
||||
</beans>
|
Loading…
Reference in New Issue