Jedis 가 Redis 데이터 베 이 스 를 조작 하 는 방법
NoSQL 에 대한 소 개 는 쓰 지 않 고 코드 로 바로 올 립 니 다.
첫 번 째 가이드 백 은 더 말 하지 않 는 다.
기본 동작:
package demo;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class Demo {
// Java Redis
@Test
public void test1() {
//
Jedis jedis = new Jedis("localhost", 6379);
// 、
jedis.set("username", "yiqing");
String username = jedis.get("username");
System.out.println(username);
}
// Jedis jedis
@Test
public void test2() {
// redis
JedisPoolConfig poolconfig = new JedisPoolConfig();
// ( )
poolconfig.setMaxIdle(30);
poolconfig.setMinIdle(10);
//
poolconfig.setMaxTotal(50);
JedisPool pool = new JedisPool(poolconfig, "localhost", 6379);
//
Jedis jedis = pool.getResource();
jedis.set("username", "yiqing");
String username = jedis.get("username");
System.out.println(username);
//
jedis.close();
//
// pool.close();
}
}
메모:실행 에 실패 하면 이 유 는 하나 뿐 입 니 다.Redis 를 열지 않 았 기 때 문 입 니 다.
좋 습 니 다.우 리 는 시각 화 도구 로 관찰 할 수 있 습 니 다.
저장 성공!!
다음:
우 리 는 조작 하기 편리 한 도구 류 를 뽑 아야 한다.
package demo;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class JedisPoolUtils {
private static JedisPool pool = null;
static {
//
InputStream in = JedisPoolUtils.class.getClassLoader().getResourceAsStream("redis.properties");
Properties pro = new Properties();
try {
pro.load(in);
} catch (IOException e) {
e.printStackTrace();
}
//
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(Integer.parseInt(pro.get("redis.maxIdle").toString()));//
poolConfig.setMinIdle(Integer.parseInt(pro.get("redis.minIdle").toString()));//
poolConfig.setMaxTotal(Integer.parseInt(pro.get("redis.maxTotal").toString()));//
pool = new JedisPool(poolConfig, pro.getProperty("redis.url"),
Integer.parseInt(pro.get("redis.port").toString()));
}
// Jedis
public static Jedis getJedis() {
return pool.getResource();
}
}
src 다음 에 새 파일 을 만 듭 니 다:redis.properties:
redis.maxIdle=30
redis.minIdle=10
redis.maxTotal=100
redis.url=localhost
redis.port=6379
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.