NOSQL
redis基本语法
javaSE 使用jedis操作redis
@Test
public void test1() throws Exception {
// 连接的redis服务器地址
String address = "localhost";
// 端口号
Integer port = 6379;
// 连接超时时间
Integer timeout = 1000;
Jedis jedis = new Jedis(address, port, timeout);
jedis.auth("1234566");
jedis.set("val1", "吉星文");
System.out.println(jedis.get("val1"));
// 关闭连接
jedis.close();
}
javaSE 连接池操作redis
@Test
public void testPool() throws Exception {
//1 创建jedispool配置对象
JedisPoolConfig config = new JedisPoolConfig();
//2 做配置-四个
config.setMaxIdle(2);
config.setMaxTotal(10);
config.setMaxWaitMillis(1*1000); //创建连接超时
config.setTestOnBorrow(true);//获取连接是测试连接是否畅通
//3 创建jedispool
//1*1000 获取连接超时时间
JedisPool pool = new JedisPool(config,
"127.0.0.1",6379,1*1000);
// 从连接池获取连接对象
Jedis resource = pool.getResource();
System.out.println(resource.get("val1"));
// 将连接对象返还给连接池
resource.close();
// 摧毁连接池
// pool.destroy();
}
redis持久化