Redis(1)安装 & 配置
Redis(2)基本键值操作
Redis(3)常用维护操作
Redis(4)客户端连接:Java
Spring 集成 Reids(Spring-data-redis)
config get maxclients # 获取 redis-server 的客户端连接上限
config set maxclients millseconds_num # 修改 redis-server 客户端连接上限
client list # 返回连接到 redis-server 的客户端列表
client getname # 返回当前连接名称
client setname # 设置当前的连名称
client pause 10000 # 挂起当前客户端连接 10000 毫秒
client kill addr ip_port # 关闭指定端口的客户端连接,该 ip_port 必须是 client list 中列出的
client kill id client_id # 关闭指定 id 的客户端连接,该 client_id 必须是 client list 中列出的
public class RedisJavaTest {
private final static Logger log = LogManager.getLogger();
public static void main(String[] args) throws IOException {
/* 创建 redis 客户端 */
Jedis jedis = new Jedis("127.0.0.1",6379);
jedis.auth("assad"); //密码登陆
log.debug(jedis.ping()); //进行登陆测试
/* 从 redis-server 读写数据 */
// 写入 Java String 和 基本类型,对应 redis string 储存类型
jedis.set("key1","Hello world!");
jedis.set("key2",2333+"");
jedis.set("key3",2.333+"");
jedis.set("key4",false+"");
// 获取 redis string 类型
String val1 = jedis.get("key1");
int val2 = Integer.parseInt(jedis.get("key2"));
double val3 = Double.parseDouble(jedis.get("key3"));
boolean val4 = Boolean.parseBoolean(jedis.get("key4"));
log.debug("key1: " + val1);
log.debug("key2: " + val2);
log.debug("key3: " + val3);
log.debug("key4: " + val4);
// 写入 Java List 类型, 对应 redis list 类型
List<String> staffs = Arrays.asList("assad","vancy","canndy");
for(String staff : staffs)
jedis.rpush("staffs",staff);
// 获取 redis list 类型
List<String> staffList = jedis.lrange("staffs",0,-1);
log.debug("staffs: " + staffList.stream().collect(Collectors.joining(", ")));
// 写入 Java Set 类型,对应 redis set 类型
Set<String> citys = new HashSet<>(Arrays.asList("Guangzhou", "Shanghai", "Beijing"));
for(String city : citys)
jedis.sadd("citys",city);
// 获取 redis set 类型
Set<String> cityList = jedis.smembers("citys");
log.debug("citys: " + cityList.stream().collect(Collectors.joining(", ")));
// 写入Java Bean :写入为 redis hash 类型
User user1 = new User("assad",22,"Guangzou");
Map<String,String> userMap = new HashMap<>();
userMap.put("name",user1.getName());
userMap.put("age",user1.getAge()+"");
userMap.put("city",user1.getCity());
jedis.hmset("user:assad",userMap);
// 获取 redis hash 类型(Java bean)
Map<String,String> userMapped = jedis.hgetAll("user:assad");
User user2 = new User(userMapped.get("name"), Integer.parseInt(userMapped.get("age")), userMapped.get("city"));
log.debug("user: " + user2);
// 获取所有 keys
Set<String> keyset = jedis.keys("*");
log.debug("keys: " + keyset.stream().collect(Collectors.joining(", ")));
}
static class User implements Serializable{
public final static long serialVersionUID = 2333L;
private String name;
private int age;
private String city;
// getter,setter,contructor,toString
}
}
19:41:51.189 [main] DEBUG redis.RedisJavaTest - PONG
19:41:51.192 [main] DEBUG redis.RedisJavaTest - key1: Hello world!
19:41:51.192 [main] DEBUG redis.RedisJavaTest - key2: 2333
19:41:51.192 [main] DEBUG redis.RedisJavaTest - key3: 2.333
19:41:51.192 [main] DEBUG redis.RedisJavaTest - key4: false
19:41:51.232 [main] DEBUG redis.RedisJavaTest - staffs: assad, vancy, canndy
19:41:51.234 [main] DEBUG redis.RedisJavaTest - citys: Guangzhou, Beijing, Shanghai
19:41:51.235 [main] DEBUG redis.RedisJavaTest - user: User{name='assad', age=22, city='Guangzou'}
19:41:51.235 [main] DEBUG redis.RedisJavaTest - keys: key1, key2, user:assad, key3, key4, citys, staffs
public class RedisUnit {
// 序列化对象为字节
public static byte[] serialize(Object object){
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
byte[] bytes = baos.toByteArray();
return bytes;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
// 反序列化字节为对象
public static Object unserialize(byte[] bytes){
ByteArrayInputStream bais = null;
ObjectInputStream ois = null;
try {
bais = new ByteArrayInputStream(bytes);
ois = new ObjectInputStream(bais);
Object obj = ois.readObject();
return obj;
}
catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
return null;
}
}
// 写入 POJO
User user = new User("assad",22,"Guangzou");
jedis.setbit(user.getName(), 0, RedisUnit.serialize(user));
// 获取 POJO
User user2 = RedisUnit.unserialize(jedis.getbit("assad",0));
public class RedisPipeLineTest {
private final static Logger log = LogManager.getLogger();
public static void main(String[] args) throws IOException {
Jedis jedis = new Jedis("127.0.0.1",6379);
jedis.auth("assad");
final int commandCount = 100000;
//没有使用 pipeline 的情况;
long start = System.currentTimeMillis();
for(int i = 0; i < commandCount; i++){
jedis.ping();
}
long end = System.currentTimeMillis();
log.debug("without pipeline: " + (end - start) / 1000f + "s");
//使用 pipline 的情况;
start = System.currentTimeMillis();
Pipeline pipe = jedis.pipelined();
for(int i = 0; i < commandCount; i++)
pipe.ping();
end = System.currentTimeMillis();
log.debug("with pipeline: " + (end - start) / 1000f + "s");
pipe.clear();
pipe.close();
}
}
20:11:59.045 [main] DEBUG redis.RedisJavaTest - without pipeline: 3.908s
20:11:59.081 [main] DEBUG redis.RedisJavaTest - with pipeline: 0.033s
//JedisPool 参数对象
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(50);
config.setMaxIdle(10);
config.setMaxWaitMillis(1000);
config.setTestOnBorrow(true);
config.setTestOnReturn(true);
String redisIp = "127.0.0.1"; //redis-server ip
int redisPort = 6379; //redis-server port
//创建 Jedis 连接池
JedisPool jedisPool = new JedisPool(config, redisIp, redisPort);
//从 JedisPool 中获取 Jedis
Jedis jedis = jedisPool.getResource();
//use jedis ......
//关闭 Jedis,将 Jedis 资源归还给 JedisPool
jedis.close();
Properties props = new Properties();
props.load(getClassLoader().getResourceAsStream("./jedisPool.properties"));
JedisPool jedisPool = new JedisPool(props.getProperties("redisServer.ip"),props.getProperties("redisServer.port"));
......