添加Jedis简单示例

pull/1/head
Zhang Peng 2018-01-12 09:08:59 +08:00
parent 7cf3464f43
commit 6ded18b246
6 changed files with 317 additions and 3 deletions

14
.gitignore vendored
View File

@ -1,4 +1,16 @@
################ Javascript ################
################ JAVA ################
# temp folders
classes
target
logs
# temp files
*.class
*.jar
*.war
################ JAVASCRIPT ################
# dependencies
node_modules

View File

@ -1,6 +1,6 @@
# db-notes
:books: 个人的数据库学习笔记。
:books: 一名 Java 程序员的数据库学习笔记。
**[gitbook 线上阅读](https://dunwu.gitbooks.io/db-notes/content/)**
@ -34,4 +34,4 @@ $ gitbook install
$ gitbook serve
```
启动成功后,访问 http://localhost:4000 即可看到电子书了。
启动成功后,访问 http://localhost:4000 即可看到电子书了。

View File

@ -0,0 +1,130 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<!-- [Part 1] BASIC SETTINGS BEGIN -->
<!-- MAVEN COORDINATE BEGIN -->
<groupId>io.github.dunwu</groupId>
<artifactId>javadb</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<!-- MAVEN COORDINATE END -->
<!-- RELATIONSHIP SETTINGS BEGIN -->
<modules>
<module>redis</module>
</modules>
<!-- RELATIONSHIP SETTINGS END -->
<!-- RELATIONSHIP SETTINGS BEGIN -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.logback-extensions</groupId>
<artifactId>logback-ext-spring</artifactId>
<version>0.1.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.12</version>
</dependency>
<!-- log end -->
<!-- javaee begin -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<!-- javaee end -->
<!-- spring begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring end -->
</dependencies>
</dependencyManagement>
<!-- RELATIONSHIP SETTINGS END -->
<!-- PROPERTIES BEGIN -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<spring.version>4.3.13.RELEASE</spring.version>
</properties>
<!-- PROPERTIES END -->
<!-- [Part 1] BASIC SETTINGS END -->
<!-- [Part 2] BUILD SETTINGS BEGIN -->
<build>
<finalName>${project.artifactId}</finalName>
</build>
<!-- [Part 2] BUILD SETTINGS END -->
<!-- [Part 3] PROJECT INFO BEGIN -->
<name>${project.artifactId}</name>
<description>Java 工具使用示例</description>
<!-- [Part 3] PROJECT INFO END -->
</project>

View File

@ -0,0 +1,72 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<!-- [Part 1] BASIC SETTINGS BEGIN -->
<!-- MAVEN COORDINATE BEGIN -->
<artifactId>javadb-redis</artifactId>
<packaging>jar</packaging>
<!-- MAVEN COORDINATE END -->
<!-- RELATIONSHIP SETTINGS BEGIN -->
<parent>
<groupId>io.github.dunwu</groupId>
<artifactId>javadb</artifactId>
<version>1.0.0</version>
</parent>
<dependencies>
<!-- db begin -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- db end -->
<!-- log start -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
</dependency>
<!-- log end -->
</dependencies>
<!-- RELATIONSHIP SETTINGS END -->
<!-- [Part 1] BASIC SETTINGS END -->
<!-- [Part 2] BUILD SETTINGS BEGIN -->
<build>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
<includes>
<include>logback.xml</include>
</includes>
<!--<targetPath>/abc</targetPath>-->
</resource>
</resources>
</build>
<!-- [Part 2] BUILD SETTINGS END -->
<!-- [Part 3] PROJECT INFO BEGIN -->
<name>${project.artifactId}</name>
<description>Java 工具使用示例</description>
<!-- [Part 3] PROJECT INFO END -->
</project>

View File

@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!-- logback中一共有5种有效级别分别是TRACE、DEBUG、INFO、WARN、ERROR优先级依次从低到高 -->
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<property name="FILE_NAME" value="javadb"/>
<!-- 将记录日志打印到控制台 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] [%-5p] %c{36}.%M - %m%n</pattern>
</encoder>
</appender>
<!-- RollingFileAppender begin -->
<appender name="ALL" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 根据时间来制定滚动策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${user.dir}/logs/${FILE_NAME}-all.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<!-- 根据文件大小来制定滚动策略 -->
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>30MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] [%-5p] %c{36}.%M - %m%n</pattern>
</encoder>
</appender>
<!-- RollingFileAppender end -->
<!-- logger begin -->
<!-- 本项目的日志记录,分级打印 -->
<logger name="io.github.dunwu" level="TRACE">
<appender-ref ref="ALL"/>
</logger>
<root level="TRACE">
<appender-ref ref="STDOUT"/>
</root>
<!-- logger end -->
</configuration>

View File

@ -0,0 +1,55 @@
package io.github.dunwu.javadb;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.junit.BeforeClass;
import org.junit.Test;
import redis.clients.jedis.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 Jedis jedis = null;
@BeforeClass
public static void beforeClass() {
jedis = new Jedis(REDIS_HOST, REDIS_PORT);
System.out.println("ping redis: " + jedis.ping());
}
@Test
public void testSet() {
jedis.set("first", "hello world");
System.out.println("first:" + jedis.get("first"));
}
@Test
public void testLpush() {
// 存储数据到列表中
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));
}
}
@Test
public void testKeys() {
// 存储数据到列表中
Set<String> keys = jedis.keys("*");
Iterator<String> it = keys.iterator();
while (it.hasNext()) {
String key = it.next();
System.out.println(key);
}
}
}