前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >《快学BigData》--Redis 总结(E)(30)

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

作者头像
小徐
发布2023-03-06 21:16:42
2100
发布2023-03-06 21:16:42
举报
文章被收录于专栏:Greenplum

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;

}

}

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-03-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 河马coding 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Redis 内部工具
  • 代码示例
    • 1-1)、链接工具
      • 1-2)、Redis API使用
      相关产品与服务
      云数据库 Redis®
      腾讯云数据库 Redis®(TencentDB for Redis®)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档