最无用的工具类封装——RedisClient

本文介绍了如何封装一个无用的RedisClient工具类,旨在简化每次使用RedisTemplate或StringRedisTemplate时的注入过程。目前实现了基础操作,未来将扩展更多功能。
摘要由CSDN通过智能技术生成
最无用的工具类封装——RedisClient

封装该工具类主要是为了每次使用的时候都需要注入RedisTemplate 或者StringRedisTemplate
暂时只实现了基础的方法,后续会进行扩展

社会我 T 哥,人狠话不多 。上代码
RedisClientConfig
package cn.texous.test.demo.config;

import cn.texous.test.demo.commons.utils.RedisClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * redis config
 *
 * @author texousliu
 * @since 2019-08-28 13:41:12
 */
@Configuration
@ConditionalOnProperty(prefix = "spring.redis", name = "enable", havingValue = "true")
public class RedisConfig {
   

    /**
     * redis template 配置
     *
     * @param factory factory
     * @return
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
   
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer();
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        template.setDefaultSerializer(serializer);
        template.setKeySerializer(stringRedisSerializer);
        template.setValueSerializer(serializer);
        template.setHashKeySerializer(stringRedisSerializer);
        template.setHashValueSerializer(serializer);
        template.afterPropertiesSet();
        return template;
    }

    @Bean
    public Object redisClient(RedisTemplate<String, Object> redisTemplate) {
   
        RedisClient.init(redisTemplate);
        return new Object();
    }

}

RedisClient
package cn.texous.test.demo.commons.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.BitFieldSubCommands;
import org.springframework.data.redis.connection.RedisZSetCommands;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * insert description here
 *
 * @author Showa.L
 * @since 2020/7/17 15:34
 */
@Slf4j
public class RedisClient {
   

    private static RedisTemplate<String, Object> TEMPLATE;

    public static void init(RedisTemplate<String, Object> redisTemplate) {
   
        log.info("[INIT]: init RedisClient...");
        TEMPLATE = redisTemplate;
    }

    public static RedisTemplate<String, Object> template() {
   
        return TEMPLATE;
    }

    public static ValueOperations<String, Object> string() {
   
        return TEMPLATE.opsForValue();
    }

    public static HashOperations<String, Object, Object> hash() {
   
        return TEMPLATE.opsForHash();
    }

    public static ListOperations<String, Object> list() {
   
        return TEMPLATE.opsForList();
    }

    public static SetOperations<String, Object> set() {
   
        return TEMPLATE.opsForSet();
    }

    public static ZSetOperations<String, Object> zSet() {
   
        return TEMPLATE.opsForZSet();
    }

    public static Boolean del(String key) {
   
        return TEMPLATE.delete(key);
    }

    public static Long del(Collection<String> keys) {
   
        return TEMPLATE.delete(keys);
    }

    public static Boolean exists(String key) {
   
        return TEMPLATE.hasKey(key);
    }

    public static Boolean expire(String key, long timeout, TimeUnit timeUnit) {
   
        return TEMPLATE.expire(key, timeout, timeUnit);
    }

    public static Boolean expire(String key, Duration duration) {
   
        return TEMPLATE.expire(key, duration);
    }

    public static void set(String key, Object value) {
   
        string().set(key, value);
    }

    /**
     * setRange 命令用指定的字符串覆盖给定 key 所储存的字符串值,覆盖的位置从偏移量 offset 开始。
     *
     * @param key    key
     * @param value  value
     * @param offset 偏移量
     */
    public static void setRange(String key, Object value, long offset) {
   
        string().set(key, value, offset);
    }

    public static void setEx(String key, Object value, Duration duration) {
   
        string().set(key, value, duration);
    }

    public static void setEx(String key, Object value, long second) {
   
        setEx(key, value, second, TimeUnit.SECONDS);
    }

    public static void setEx(String key, Object value, long timeout, TimeUnit timeUnit) {
   
        string().set(key, value, timeout, timeUnit);
    }

    public static Object get(String key) {
   
        return string().get(key);
    }

    public static String getString(String key) {
   
        return (String) get(key);
    }

    /**
     * getRange 命令用于获取存储在指定 key 中字符串的子字符串。字符串的截取范围由 start 和 end 两个偏移量决定(包括 start 和 end 在内)。
     *
     * @param key   key
     * @param start 开始下标
     * @param end   结束下标
     * @return
     */
    public static String getRange(String key, long start, long end) {
   
        return string().get(key, start, end);
    }

    public static Object getSet(String key, Object value) {
   
        return string().getAndSet(key, value);
    }

    public static String getAndSetString(String key, String value) {
   
        return (String) getSet(key, value);
    }

    /**
     * getBit 命令用于对 key 所储存的字符串值,获取指定偏移量上的位(bit)。
     * # 对不存在的 key 或者不存在的 offset 进行 getBit, 返回 0
     * # 对已存在的 offset 进行 getBit
     *
     * @param key    key
     * @param offset 偏移量
     * @return
     */
    public static Boolean getBit(String key, long offset) {
   
        return string().getBit(key, offset);
    }

    public static List<Object> mGet(Collection<String> keys) {
   
        return string().multiGet(keys);
    }

    public static List<String> mGetString(Collection<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值