Redis的基本数据类型:
- string
- hash
- list
- set
- zset
{
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
// string
jedis.set("hello", "word");
String hello = jedis.get("hello");
System.out.println(hello);
// hash
jedis.hset("hello:world", "version", "111");
jedis.hset("hello:world", "name", "jedis-redis");
Map<String, String> stringStringMap = jedis.hgetAll("hello:world");
System.out.println(stringStringMap);
// list
jedis.rpush("namelist", "11", "22");
List<String> namelist = jedis.lrange("namelist", 0, 1);
System.out.println(namelist);
// set
jedis.sadd("subject", "math", "Math", "abc", "abc");
Set<String> subject = jedis.smembers("subject");
System.out.println(subject);
// zset
jedis.zadd("nums", 1, "ming");
jedis.zadd("nums", 4, "zhang");
jedis.zadd("nums", 2, "wang");
Set<String> nums = jedis.zrange("nums", 0, 4);
System.out.println(nums);
jedis.close();
}
}
Jedis线程池比Jedis稳定,且易于管理。
{
public static void main(String[] args) {
JedisPoolConfig poolConfig = new JedisPoolConfig();
JedisPool jedisPool = new JedisPool(poolConfig, "127.0.0.1", 6379, 20000);
Jedis jedis = null;
try{
jedis = jedisPool.getResource();
jedis.set("pool", "jeditpool");
String result = jedis.get("pool");
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
} finally {
assert jedis != null;
jedis.close();
}
}
}
JedisPool的Jedis对象最多默认为8个
package com.codeiteasy.service;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.time.Duration;
import java.util.concurrent.atomic.AtomicInteger;
@Service
public class JedisPoolService {
public static void main(String[] args) throws InterruptedException {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxWait(Duration.ofSeconds(2));
JedisPool jedisPool = new JedisPool(poolConfig, "127.0.0.1", 6379, 20000);
AtomicInteger atom = new AtomicInteger(0);
for (int i = 0; i < 15; i++) {
Thread t1 = new Thread(() -> {
try{
Jedis jedis = null;
jedis = jedisPool.getResource();
jedis.set("pool", "jeditpool");
String result = jedis.get("pool");
int x = atom.incrementAndGet();
System.out.println(result+x);
} catch (Exception e) {
e.printStackTrace();
}
});
t1.start();
t1.join();
}
}
}
更改用后关闭
package com.codeiteasy.service;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.time.Duration;
import java.util.concurrent.atomic.AtomicInteger;
@Service
public class JedisPoolService {
public static void main(String[] args) throws InterruptedException {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxWait(Duration.ofSeconds(2));
JedisPool jedisPool = new JedisPool(poolConfig, "127.0.0.1", 6379, 20000);
AtomicInteger atom = new AtomicInteger(0);
for (int i = 0; i < 15; i++) {
Thread t1 = new Thread(() -> {
Jedis jedis = null;
try{
jedis = jedisPool.getResource();
jedis.set("pool", "jeditpool");
String result = jedis.get("pool");
int x = atom.incrementAndGet();
System.out.println(result+x);
} catch (Exception e) {
e.printStackTrace();
} finally {
jedis.close();
}
});
t1.start();
t1.join();
}
}
}