《快学BigData》--Redis 总结(E)(30)

2023-03-06 21:16:42 浏览数 (1)

Redis 内部工具

一下工具在Redis的安装目录下的src目录下

工具 描述

redis-server 服务端

redis-cli 客户端

redis-benchmark Redis性能测试工具

redis-check-aof AOF文件修复工具

redis-check-dump RDB文件检测工具

redis-sentinel Sentinel服务器(仅在2.8之后)

代码示例

1-1)、链接工具

package com.otsuser.usualpassenger.redis;

import redis.clients.jedis.Jedis;

import redis.clients.jedis.JedisPool;

import redis.clients.jedis.JedisPoolConfig;

public class RedisConnectUtils {

// Redis服务器IP

private static String HOST = "127.0.0.1";

// Redis的端口号

private static int PORT = 6379;

// 可用连接实例的最大数目,默认值为8;

// 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。

private static int MAX_ACTIVE = 1024;

// 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。

private static int MAX_IDLE = 200;

// 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;

private static int MAX_WAIT = 10000;

// 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;

private static boolean TEST_ON_BORROW = true;

private static JedisPool jedisPool = null;

/**

* 初始化Redis连接池

*/

static {

try {

JedisPoolConfig config = new JedisPoolConfig();

config.setMaxTotal(MAX_ACTIVE);

config.setMaxIdle(MAX_IDLE);

config.setMaxWaitMillis(MAX_WAIT);

config.setTestOnBorrow(TEST_ON_BORROW);

jedisPool = new JedisPool(config, HOST, PORT);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 获取Jedis实例

*

* @return

*/

public synchronized static Jedis getJedis() {

try {

if (jedisPool != null) {

Jedis resource = jedisPool.getResource();

return resource;

} else {

return null;

}

} catch (Exception e) {

e.printStackTrace();

return null;

}

}

/**

* 释放jedis资源

*

* @param jedis

*/

public static void returnResource(final Jedis jedis) {

if (jedis != null) {

jedisPool.returnResourceObject(jedis);

}

}

}

1-2)、Redis API使用

package com.otsuser.usualpassenger.redis;

import java.util.List;

import java.util.Map;

import java.util.Set;

import redis.clients.jedis.Jedis;

public class RedisApiClient {

/**

* 检查是否连接成功

*

* @return

*/

public static String ping() {

Jedis jedis = RedisConnectUtils.getJedis();

String str = jedis.ping();

RedisConnectUtils.returnResource(jedis);

return str;

}

/**

* 通过key删除(字节)

*

* @param keys

* @return Integer reply, specifically: an integer greater than 0 if one or

* more keys were removed 0 if none of the specified key existed

*/

public static Long del(byte[] key) {

Jedis jedis = RedisConnectUtils.getJedis();

Long returnResult = jedis.del(key);

RedisConnectUtils.returnResource(jedis);

return returnResult;

}

/**

* 通过key删除

*

* @param key

*/

public static void del(String key) {

Jedis jedis = RedisConnectUtils.getJedis();

jedis.del(key);

RedisConnectUtils.returnResource(jedis);

}

/**

* 添加key value 并且设置存活时间(byte)

*

* @param key

* @param value

* @param liveTime

*/

public static void set(byte[] key, byte[] value, int liveTime) {

Jedis jedis = RedisConnectUtils.getJedis();

jedis.set(key, value);

jedis.expire(key, liveTime);

RedisConnectUtils.returnResource(jedis);

}

/**

* 添加key value 并且设置存活时间

*

* @param key

* @param value

* @param liveTime

*/

public void set(String key, String value, int liveTime) {

Jedis jedis = RedisConnectUtils.getJedis();

jedis.set(key, value);

jedis.expire(key, liveTime);

RedisConnectUtils.returnResource(jedis);

}

/**

* 添加key value

*

* @param key

* @param value

*/

public void set(String key, String value) {

Jedis jedis = RedisConnectUtils.getJedis();

jedis.set(key, value);

RedisConnectUtils.returnResource(jedis);

}

/**

* 添加key value (字节)(序列化)

*

* @param key

* @param value

*/

public void set(byte[] key, byte[] value) {

Jedis jedis = RedisConnectUtils.getJedis();

jedis.set(key, value);

RedisConnectUtils.returnResource(jedis);

}

/**

* 获取redis value (String)

*

* @param key

* @return

*/

public static String get(String key) {

Jedis jedis = RedisConnectUtils.getJedis();

String value = jedis.get(key);

RedisConnectUtils.returnResource(jedis);

return value;

}

/**

* 获取redis value (byte [] )(反序列化)

*

* @param key

* @return

*/

public static byte[] get(byte[] key) {

Jedis jedis = RedisConnectUtils.getJedis();

byte[] value = jedis.get(key);

RedisConnectUtils.returnResource(jedis);

return value;

}

/**

* 通过正则匹配keys

*

* @param pattern

* @return

*/

public static Set<String> keys(String pattern) {

Jedis jedis = RedisConnectUtils.getJedis();

Set<String> value = jedis.keys(pattern);

RedisConnectUtils.returnResource(jedis);

return value;

}

/**

* 检查key是否已经存在

*

* @param key

* @return

*/

public static boolean exists(String key) {

Jedis jedis = RedisConnectUtils.getJedis();

boolean value = jedis.exists(key);

RedisConnectUtils.returnResource(jedis);

return value;

}

/**

* 往list中添加元素

*

* @param key

* @param value

*/

public void lpush(String key, String value) {

Jedis jedis = RedisConnectUtils.getJedis();

jedis.lpush(key, value);

RedisConnectUtils.returnResource(jedis);

}

public void rpush(String key, String value) {

Jedis jedis = RedisConnectUtils.getJedis();

jedis.rpush(key, value);

RedisConnectUtils.returnResource(jedis);

}

/**

* 数组长度

*

* @param key

* @return

*/

public static Long llen(String key) {

Jedis jedis = RedisConnectUtils.getJedis();

Long len = jedis.llen(key);

RedisConnectUtils.returnResource(jedis);

return len;

}

/**

* 获取下标为index的value

*

* @param key

* @param index

* @return

*/

public static String lindex(String key, Long index) {

Jedis jedis = RedisConnectUtils.getJedis();

String str = jedis.lindex(key, index);

RedisConnectUtils.returnResource(jedis);

return str;

}

public static String lpop(String key) {

Jedis jedis = RedisConnectUtils.getJedis();

String str = jedis.lpop(key);

RedisConnectUtils.returnResource(jedis);

return str;

}

public static List<String> lrange(String key, long start, long end) {

Jedis jedis = RedisConnectUtils.getJedis();

List<String> str = jedis.lrange(key, start, end);

RedisConnectUtils.returnResource(jedis);

return str;

}

/**

* @param key

* @param field

* @param value

* @return If the field already exists, and the HSET just produced an update

* of the value, 0 is returned, otherwise if a new field is created

* 1 is returned.

*/

public static Long hset(String key, String field, String value) {

Jedis jedis = RedisConnectUtils.getJedis();

Long alreadyExists = jedis.hset(key, field, value);

RedisConnectUtils.returnResource(jedis);

return alreadyExists;

}

/**

* @param key

* @param field

* @param value

* @return If the field already exists, and the HSET just produced an update

* of the value, 0 is returned, otherwise if a new field is created

* 1 is returned.

*/

public static Long hset(byte[] key, byte[] field, byte[] value) {

Jedis jedis = RedisConnectUtils.getJedis();

Long alreadyExists = jedis.hset(key, field, value);

RedisConnectUtils.returnResource(jedis);

return alreadyExists;

}

/**

* @param key

* @param field

* @return Bulk reply

*/

public static String hget(final String key, final String field) {

Jedis jedis = RedisConnectUtils.getJedis();

String str = jedis.hget(key, field);

RedisConnectUtils.returnResource(jedis);

return str;

}

/**

* @param key

* @param field

* @return Bulk reply

*/

public static byte[] hget(final byte[] key, final byte[] field) {

Jedis jedis = RedisConnectUtils.getJedis();

byte[] bt = jedis.hget(key, field);

jedis.hgetAll(key);

RedisConnectUtils.returnResource(jedis);

return bt;

}

/**

* @param key

* @return All the fields and values contained into a hash.

*/

public static Map<String, String> hgetAll(String key) {

Jedis jedis = RedisConnectUtils.getJedis();

Map<String, String> map = jedis.hgetAll(key);

RedisConnectUtils.returnResource(jedis);

return map;

}

/**

* @param key

* @return All the fields and values contained into a hash.

*/

public static Map<byte[], byte[]> hgetAll(byte[] key) {

Jedis jedis = RedisConnectUtils.getJedis();

Map<byte[], byte[]> map = jedis.hgetAll(key);

RedisConnectUtils.returnResource(jedis);

return map;

}

/**

* @param key

* @param fields

* @return If the field was present in the hash it is deleted and 1 is

* returned, otherwise 0 is returned and no operation is performed.

*/

public static Long hdel(final String key, final String... fields) {

Jedis jedis = RedisConnectUtils.getJedis();

Long returnResult = jedis.hdel(key, fields);

RedisConnectUtils.returnResource(jedis);

return returnResult;

}

/**

* @param key

* @param fields

* @return If the field was present in the hash it is deleted and 1 is

* returned, otherwise 0 is returned and no operation is performed.

*/

public static Long hdel(final byte[] key, final byte[]... fields) {

Jedis jedis = RedisConnectUtils.getJedis();

Long returnResult = jedis.hdel(key, fields);

RedisConnectUtils.returnResource(jedis);

return returnResult;

}

/**

* @param key

* @param field

* @return Return 1 if the hash stored at key contains the specified field.

* Return 0 if the key is not found or the field is not present.

*/

public static Boolean hexists(String key, final String field) {

Jedis jedis = RedisConnectUtils.getJedis();

Boolean returnResult = jedis.hexists(key, field);

RedisConnectUtils.returnResource(jedis);

return returnResult;

}

/**

* @param key

* @param hash

* @return

*/

public static String hmset(final String key, final Map<String, String> hash) {

Jedis jedis = RedisConnectUtils.getJedis();

String str = jedis.hmset(key, hash);

RedisConnectUtils.returnResource(jedis);

return str;

}

/**

* @param key

* @param fields

* @return

*/

public static List<String> hmget(final String key, final String fields) {

Jedis jedis = RedisConnectUtils.getJedis();

List<String> list = jedis.hmget(key, fields);

RedisConnectUtils.returnResource(jedis);

return list;

}

/**

* 清空redis 所有数据

*

* @return

*/

public static String flushDB() {

Jedis jedis = RedisConnectUtils.getJedis();

String str = jedis.flushDB();

RedisConnectUtils.returnResource(jedis);

return str;

}

/**

* 查看redis里有多少数据

*/

public static long dbSize() {

Jedis jedis = RedisConnectUtils.getJedis();

long len = jedis.dbSize();

RedisConnectUtils.returnResource(jedis);

return len;

}

}

0 人点赞