Redis --- 02 Redis Java client

1. Attention

  To connect to Redis installed in Linux, you need to perform the following steps:

  ① Disable firewall (CentOS 7): systemctl stop firewalld.service

  ② Comment out bind 127.0.0.1 in redis.conf and then protect-mode no

Second, the use of Java

  1. Import Jedis-2.1.0.jar

  2. Write code:

package com.jenne.redis; 

import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util. *;

public class RedisDemo01 {
Jedis jedis;

@Before
public void init ( ) {
// Connect to local Redis service
jedis = new Jedis ("192.168.7.128", 6379);
}

@Test
public void testPing () {
init ();
// Check if the service is running, type pong to indicate OK
System.out .println ("connection is OK ==========>:" + jedis.ping ());
}

@Test
public void testKey () {
Set <String> keys = jedis.keys ("*" );
for (Iterator iterator = keys.iterator(); iterator.hasNext(); ) {
String key = (String) iterator.next();
System.out.println(key);
}
System.out.println("jedis.exists====>" + jedis.exists("k2"));
System.out.println(jedis.ttl("k1"));

}

@Test
public void testString() {
System.out.println(jedis.get("k1"));
jedis.set("k4", "k4_Redis");
System.out.println("----------------------------------------");
jedis.mset("str1", "v1", "str2", "v2", "str3", "v3");
System.out.println(jedis.mget("str1", "str2", "str3"));
}

@Test
public void testList() {
List<String> list = jedis.lrange("mylist", 0, -1);
for (String element : list) {
System.out.println(element);
}
}

@Test
public void testSet() {
jedis.sadd("orders", "jd001");
jedis.sadd("orders", "jd002");
jedis.sadd("orders", "jd003");
Set<String> set1 = jedis.smembers("orders");
for (Iterator iterator = set1.iterator(); iterator.hasNext(); ) {
String string = (String) iterator.next();
System.out.println(string);
}
jedis.srem("orders", "jd002");
}

@Test
public void testHash() {
jedis.hset("hash1", "userName", "lisi");
System.out.println(jedis.hget("hash1", "userName"));
Map<String, String> map = new HashMap<String, String>();
map.put("telphone", "13810169999");
map.put("address", "atguigu");
map.put("email", "[email protected]");
jedis.hmset("hash2", map);
List<String> result = jedis.hmget("hash2", "telphone", "email");
for (String element : result) {
System.out.println(element);
}
}

@Test
public void testZset() {
jedis.zadd("zset01", 60d, "v1");
jedis.zadd("zset01", 70d, "v2");
jedis.zadd("zset01", 80d, "v3");
jedis.zadd("zset01", 90d, "v4");
Set<String> s1 = jedis.zrange("zset01", 0, -1);
for (Iterator iterator = s1.iterator(); iterator.hasNext(); ) {
String string = (String) iterator.next();
System.out.println(string);
}
}
}

 3. Use of Redis connection pool

  1. Import commons-pool2-2.4.2.jar

  2. Write code:

package com.jenne.redis;

import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisDemo02 {
JedisPool jedisPool;
Jedis jedis;

@Before
public void init() {
//设置到JedisPoolConfig中
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(50);
config.setMaxIdle(10);

//初始化JedisPool
jedisPool = new JedisPool(config, "192.168.7.128", 6379);
jedis = jedisPool.getResource();
}

@Test
public void testPool() {
String ping = jedis.ping();
System.out.println(ping);
}
}

Guess you like

Origin www.cnblogs.com/jenne-blog/p/12689080.html