jedis工具类

发布一个k8s部署视频:https://edu.csdn.net/course/detail/26967

课程内容:各种k8s部署方式。包括minikube部署,kubeadm部署,kubeasz部署,rancher部署,k3s部署。包括开发测试环境部署k8s,和生产环境部署k8s。

腾讯课堂连接地址https://ke.qq.com/course/478827?taid=4373109931462251&tuin=ba64518

第二个视频发布  https://edu.csdn.net/course/detail/27109

腾讯课堂连接地址https://ke.qq.com/course/484107?tuin=ba64518

介绍主要的k8s资源的使用配置和命令。包括configmap,pod,service,replicaset,namespace,deployment,daemonset,ingress,pv,pvc,sc,role,rolebinding,clusterrole,clusterrolebinding,secret,serviceaccount,statefulset,job,cronjob,podDisruptionbudget,podSecurityPolicy,networkPolicy,resourceQuota,limitrange,endpoint,event,conponentstatus,node,apiservice,controllerRevision等。

第三个视频发布:https://edu.csdn.net/course/detail/27574

详细介绍helm命令,学习helm chart语法,编写helm chart。深入分析各项目源码,学习编写helm插件


————————————————------------------------------------------------------------------------------------------------------------------

 

public class JedisUtils
{
    private static final Logger logger = LoggerFactory.getLogger(JedisUtils.class);
    
    /**
     * 私有构造器,防止类的实例化操作
     */
    private JedisUtils()
    {
    }
    
    public static JedisSlotBasedConnectionHandler jedisSlotBasedConnectionHandler = SpringUtils.getBean(JedisSlotBasedConnectionHandler.class);
    
    /**
     * 根据键值从Redis中获取字符串值
     * @param key 缓存键值
     * @return String 缓存数据
     */
    public static String get(String key)
    {
        String value = null;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            if (jedis.exists(key))
            {
                value = jedis.get(key);
                value = StringUtils.isNotBlank(value) && !"nil".equalsIgnoreCase(value) ? value : null;
            }
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, key + "===" + e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return value;
    }
    
    /**
     * 根据键值及缓存生命周期将指定的字符串写入Redis
     * @param key 键值
     * @param value 字符串数据
     * @param cacheSeconds 缓存生命周期
     * @return String 写入结果(OK:表示成功,否则失败)
     */
    public static String set(String key, String value, int cacheSeconds)
    {
        String result = null;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            result = jedis.set(key, value);
            if (cacheSeconds != 0)
            {
                jedis.expire(key, cacheSeconds);// 这里返回的是long,如果为1估计是成功,但具体意义在源码里没有找到注释
            }
        }
        catch (JedisDataException e)
        {
            LoggerUtils.logError(logger, e.getMessage());
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return result;
    }
    
    /**
     * 获取缓存对象
     * @param key 键
     * @return 值
     */
    public static Object getObject(String key)
    {
        Object value = null;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            if (jedis.exists(ObjectUtils.getBytes(key)))
            {
                value = ObjectUtils.toObject(jedis.get(ObjectUtils.getBytes(key)));
            }
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return value;
    }
    
    /**
     * 
     * @param key
     * @param object
     */
    public static String setObject(String key, Object value, Integer cacheSeconds)
    {
        String result = null;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            result = jedis.set(ObjectUtils.getBytes(key), ObjectUtils.toBytes(value));
            if (cacheSeconds != 0)
            {
                jedis.expire(key, cacheSeconds);
            }
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return result;
    }
    
    /**
     * 获取List缓存
     * @param key 键
     * @return 值
     */
    public static List<String> getListString(String key)
    {
        List<String> value = null;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            if (jedis.exists(key))
            {
                value = jedis.lrange(key, 0, -1);
            }
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return value;
    }
    
    /**
     * 获取List缓存
     * @param key 键
     * @return 值
     */
    public static List<Object> getObjectList(String key)
    {
        List<Object> value = null;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            if (jedis.exists(ObjectUtils.getBytes(key)))
            {
                List<byte[]> list = jedis.lrange(ObjectUtils.getBytes(key), 0, -1);
                value = Lists.newArrayList();
                for (byte[] bs : list)
                {
                    value.add(ObjectUtils.toObject(bs));
                }
            }
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return value;
    }
    
    /**
     * 设置List缓存
     * @param key 键
     * @param value 值
     * @param cacheSeconds 超时时间,0为不超时
     * @return
     */
    public static long setList(String key, List<String> value, int cacheSeconds)
    {
        long result = 0;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            if (jedis.exists(key))
            {
                jedis.del(key);
            }
            result = jedis.rpush(key, (String[]) value.toArray());
            if (cacheSeconds != 0)
            {
                jedis.expire(key, cacheSeconds);
            }
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return result;
    }
    
    /**
     * 设置List缓存
     * @param key 键
     * @param value 值
     * @param cacheSeconds 超时时间,0为不超时
     * @return
     */
    public static long setObjectList(String key, List<Object> value, int cacheSeconds)
    {
        long result = 0;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            if (jedis.exists(ObjectUtils.getBytes(key)))
            {
                jedis.del(key);
            }
            List<byte[]> list = Lists.newArrayList();
            for (Object o : value)
            {
                list.add(ObjectUtils.toBytes(o));
            }
            result = jedis.rpush(ObjectUtils.getBytes(key), (byte[][]) list.toArray());
            if (cacheSeconds != 0)
            {
                jedis.expire(key, cacheSeconds);
            }
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return result;
    }
    
    /**
     * 向List缓存中添加值
     * @param key 键
     * @param value 值
     * @return
     */
    public static long listAdd(String key, String ... value)
    {
        long result = 0;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            result = jedis.rpush(key, value);
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return result;
    }
    
    /**
     * 向List缓存中添加值
     * @param key 键
     * @param value 值
     * @return
     */
    public static long listObjectAdd(String key, Object ... value)
    {
        long result = 0;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            List<byte[]> list = Lists.newArrayList();
            for (Object o : value)
            {
                list.add(ObjectUtils.toBytes(o));
            }
            result = jedis.rpush(ObjectUtils.getBytes(key), (byte[][]) list.toArray());
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return result;
    }
    
    /**
     * 获取缓存
     * @param key 键
     * @return 值
     */
    public static Set<String> getSet(String key)
    {
        Set<String> value = null;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            if (jedis.exists(key))
            {
                value = jedis.smembers(key);
            }
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return value;
    }
    
    /**
     * 获取缓存
     * @param key 键
     * @return 值
     */
    public static Set<Object> getObjectSet(String key)
    {
        Set<Object> value = null;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            if (jedis.exists(ObjectUtils.getBytes(key)))
            {
                value = Sets.newHashSet();
                Set<byte[]> set = jedis.smembers(ObjectUtils.getBytes(key));
                for (byte[] bs : set)
                {
                    value.add(ObjectUtils.toObject(bs));
                }
            }
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return value;
    }
    
    /**
     * 设置Set缓存
     * @param key 键
     * @param value 值
     * @param cacheSeconds 超时时间,0为不超时
     * @return
     */
    public static long setSet(String key, Set<String> value, int cacheSeconds)
    {
        long result = 0;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            if (jedis.exists(key))
            {
                jedis.del(key);
            }
            result = jedis.sadd(key, (String[]) value.toArray());
            if (cacheSeconds != 0)
            {
                jedis.expire(key, cacheSeconds);
            }
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return result;
    }
    
    /**
     * 设置Set缓存
     * @param key 键
     * @param value 值
     * @param cacheSeconds 超时时间,0为不超时
     * @return
     */
    public static long setObjectSet(String key, Set<Object> value, int cacheSeconds)
    {
        long result = 0;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            if (jedis.exists(ObjectUtils.getBytes(key)))
            {
                jedis.del(key);
            }
            Set<byte[]> set = Sets.newHashSet();
            for (Object o : value)
            {
                set.add(ObjectUtils.toBytes(o));
            }
            result = jedis.sadd(ObjectUtils.getBytes(key), (byte[][]) set.toArray());
            if (cacheSeconds != 0)
            {
                jedis.expire(key, cacheSeconds);
            }
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return result;
    }
    
    /**
     * 向Set缓存中添加值
     * @param key 键
     * @param value 值
     * @return
     */
    public static long setSetAdd(String key, String ... value)
    {
        long result = 0;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            result = jedis.sadd(key, value);
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return result;
    }
    
    /**
     * 向Set缓存中添加值
     * @param key 键
     * @param value 值
     * @return
     */
    public static long setSetObjectAdd(String key, Object ... value)
    {
        long result = 0;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            Set<byte[]> set = Sets.newHashSet();
            for (Object o : value)
            {
                set.add(ObjectUtils.toBytes(o));
            }
            result = jedis.rpush(ObjectUtils.getBytes(key), (byte[][]) set.toArray());
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return result;
    }
    
    /**
     * 获取Map缓存
     * @param key 键
     * @return 值
     */
    public static Map<String, String> getMap(String key)
    {
        Map<String, String> value = null;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            if (jedis.exists(key))
            {
                value = jedis.hgetAll(key);
            }
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return value;
    }
    
    /**
     * 获取Map缓存
     * @param key 键
     * @return 值
     */
    public static Map<String, Object> getObjectMap(String key)
    {
        Map<String, Object> value = null;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            if (jedis.exists(ObjectUtils.getBytes(key)))
            {
                value = Maps.newHashMap();
                Map<byte[], byte[]> map = jedis.hgetAll(ObjectUtils.getBytes(key));
                for (Map.Entry<byte[], byte[]> e : map.entrySet())
                {
                    value.put(new String(e.getKey(), Charset.forName(CharsetConst.CHARSET_UT)), ObjectUtils.toObject(e.getValue()));
                }
            }
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return value;
    }
    
    /**
     * 设置Map缓存
     * @param key 键
     * @param value 值
     * @param cacheSeconds 超时时间,0为不超时
     * @return
     */
    public static String setMap(String key, Map<String, String> value, int cacheSeconds)
    {
        String result = null;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            if (jedis.exists(key))
            {
                jedis.del(key);
            }
            result = jedis.hmset(key, value);
            if (cacheSeconds != 0)
            {
                jedis.expire(key, cacheSeconds);
            }
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return result;
    }
    
    /**
     * 设置Map缓存
     * @param key 键
     * @param value 值
     * @param cacheSeconds 超时时间,0为不超时
     * @return
     */
    public static String setObjectMap(String key, Map<String, Object> value, int cacheSeconds)
    {
        String result = null;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            if (jedis.exists(ObjectUtils.getBytes(key)))
            {
                jedis.del(key);
            }
            Map<byte[], byte[]> map = Maps.newHashMap();
            for (Map.Entry<String, Object> e : value.entrySet())
            {
                map.put(ObjectUtils.getBytes(e.getKey()), ObjectUtils.toBytes(e.getValue()));
            }
            result = jedis.hmset(ObjectUtils.getBytes(key), (Map<byte[], byte[]>) map);
            if (cacheSeconds != 0)
            {
                jedis.expire(key, cacheSeconds);
            }
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return result;
    }
    
    /**
     * 向Map缓存中添加值
     * @param key 键
     * @param value 值
     * @return
     */
    public static String mapPut(String key, Map<String, String> value)
    {
        String result = null;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            result = jedis.hmset(key, value);
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return result;
    }
    
    /**
     * 向Map缓存中添加值
     * @param key 键
     * @param value 值
     * @return
     */
    public static String mapObjectPut(String key, Map<String, Object> value)
    {
        String result = null;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            Map<byte[], byte[]> map = Maps.newHashMap();
            for (Map.Entry<String, Object> e : value.entrySet())
            {
                map.put(ObjectUtils.getBytes(e.getKey()), ObjectUtils.toBytes(e.getValue()));
            }
            result = jedis.hmset(ObjectUtils.getBytes(key), (Map<byte[], byte[]>) map);
            logger.debug("mapObjectPut {} = {}", key, value);
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return result;
    }
    
    /**
     * 移除Map缓存中的值
     * @param key 键
     * @param value 值
     * @return
     */
    public static long mapRemove(String key, String mapKey)
    {
        long result = 0;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            result = jedis.hdel(key, mapKey);
            logger.debug("mapRemove {}  {}", key, mapKey);
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, key + mapKey + "===" + e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return result;
    }
    
    /**
     * 移除Map缓存中的值
     * @param key 键
     * @param value 值
     * @return
     */
    public static long mapObjectRemove(String key, String mapKey)
    {
        long result = 0;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            result = jedis.hdel(ObjectUtils.getBytes(key), ObjectUtils.getBytes(mapKey));
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, key + mapKey + "===" + e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return result;
    }
    
    /**
     * 判断Map缓存中的Key是否存在
     * @param key 键
     * @param value 值
     * @return
     */
    public static boolean mapExists(String key, String mapKey)
    {
        boolean result = false;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            result = jedis.hexists(key, mapKey);
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, key + mapKey + "===" + e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return result;
    }
    
    /**
     * 判断Map缓存中的Key是否存在
     * @param key 键
     * @param value 值
     * @return
     */
    public static boolean mapObjectExists(String key, String mapKey)
    {
        boolean result = false;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            result = jedis.hexists(ObjectUtils.getBytes(key), ObjectUtils.getBytes(mapKey));
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, key + mapKey + "===" + e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return result;
    }
    
    /**
     * 删除缓存
     * @param key 键
     * @return
     */
    public static long del(String key)
    {
        long result = 0;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            if (jedis.exists(key))
            {
                result = jedis.del(key);
            }
            else
            {
                logger.debug("del {} not exists", key);
            }
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, key + "===" + e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return result;
    }
    
    /**
     * 删除缓存
     * @param key 键
     * @return
     */
    public static long delObject(String key)
    {
        long result = 0;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            if (jedis.exists(ObjectUtils.getBytes(key)))
            {
                result = jedis.del(ObjectUtils.getBytes(key));
                logger.debug("delObject {}", key);
            }
            else
            {
                logger.debug("delObject {} not exists", key);
            }
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, key + "===" + e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return result;
    }
    
    /**
     * 缓存是否存在
     * @param key 键
     * @return
     */
    public static boolean exists(String key)
    {
        boolean result = false;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            result = jedis.exists(key);
            logger.debug("exists {}", key);
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, key + "===" + e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return result;
    }
    
    /**
     * 缓存是否存在
     * @param key 键
     * @return
     */
    public static boolean existsObject(String key)
    {
        boolean result = false;
        Jedis jedis = null;
        try
        {
            jedis = getConnection(key);
            result = jedis.exists(ObjectUtils.getBytes(key));
            logger.debug("existsObject {}", key);
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, key + "===" + e.getMessage());
        }
        finally
        {
            releaseConnection(jedis);
        }
        return result;
    }
    
    /**
     * 从Redis集群中根据CRC16算法获取当前key对象的Redis服务器操作对象
     * @param key 缓存key值
     * @return Jedis Jedis
     */
    private static Jedis getConnection(String key)
    {
        Jedis jedis = null;
        try
        {
            jedis = jedisSlotBasedConnectionHandler.getConnectionFromSlot(JedisClusterCRC16.getSlot(key));
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, e.getMessage());
        }
        return jedis;
    }
    
    /**
     * 释放Redis服务器连接
     * @param jedis Jedis
     */
    private static void releaseConnection(Jedis jedis)
    {
        try
        {
            jedisSlotBasedConnectionHandler.returnConnection(jedis);
        }
        catch (JedisHandleException e)
        {
            LoggerUtils.logError(logger, e.getMessage());
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hxpjava1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值