Spring-boot Redis工具类

该工具类实现了RedisOperations的全部方法,同时添加了一些方法糖,减少Redis操作的代码量。

import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.core.query.SortQuery;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.data.redis.core.types.RedisClientInfo;
import org.springframework.data.redis.hash.HashMapper;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;

import java.io.Closeable;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * RedisOperations的代理类,同时提供了一些方法糖,减少Redis操作的代码量
 * @see org.springframework.data.redis.core.RedisOperations
 */
@Component
public class Redis {

    private static RedisTemplate<String, Object> redisTemplate;

    public Redis(RedisTemplate<String, Object> redisTemplate) {
        Redis.redisTemplate = redisTemplate;
    }

    // ---------------------------------------------------------------------------
    //                     获取value、list、set、hash等类型的操作对象
    // ---------------------------------------------------------------------------

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

    public static BoundValueOperations<String, Object> value(String key) {
        return redisTemplate.boundValueOps(key);
    }

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

    public static BoundListOperations<String, Object> list(String key) {
        return redisTemplate.boundListOps(key);
    }

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

    public static BoundSetOperations<String, Object> set(String key) {
        return redisTemplate.boundSetOps(key);
    }

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

    public static BoundZSetOperations<String, Object> zSet(String key) {
        return redisTemplate.boundZSetOps(key);
    }

    public static <HK, HV> HashOperations<String, HK, HV> hash() {
        return redisTemplate.opsForHash();
    }

    public static <HK, HV> BoundHashOperations<String, HK, HV> hash(String key) {
        return redisTemplate.boundHashOps(key);
    }

    public static GeoOperations<String, Object> geo() {
        return redisTemplate.opsForGeo();
    }

    public static BoundGeoOperations<String, Object> geo(String key) {
        return redisTemplate.boundGeoOps(key);
    }

    public static <HK, HV> StreamOperations<String, HK, HV> stream() {
        return redisTemplate.opsForStream();
    }

    public static <HK, HV> StreamOperations<String, HK, HV>
    stream(HashMapper<? super String, ? super HK, ? super HV> hashMapper) {
        return redisTemplate.opsForStream(hashMapper);
    }

    public static <HK, HV> BoundStreamOperations<String, HK, HV> stream(String key) {
        return redisTemplate.boundStreamOps(key);
    }

    public static ClusterOperations<String, Object> cluster() {
        return redisTemplate.opsForCluster();
    }

    // ---------------------------------------------------------------------------
    //                                实现代理方法
    // ---------------------------------------------------------------------------

    public static <T> T execute(RedisCallback<T> action) {
        return redisTemplate.execute(action);
    }

    public static <T> T execute(SessionCallback<T> session) {
        return redisTemplate.execute(session);
    }

    public static List<Object> executePipelined(RedisCallback<?> action) {
        return redisTemplate.executePipelined(action);
    }

    public static List<Object> executePipelined(RedisCallback<?> action, RedisSerializer<?> resultSerializer) {
        return redisTemplate.executePipelined(action, resultSerializer);
    }

    public static List<Object> executePipelined(SessionCallback<?> session) {
        return redisTemplate.executePipelined(session);
    }

    public static List<Object> executePipelined(SessionCallback<?> session, RedisSerializer<?> resultSerializer) {
        return redisTemplate.executePipelined(session, resultSerializer);
    }

    public static <T> T execute(RedisScript<T> script, List<String> keys, Object... args) {
        return redisTemplate.execute(script, keys, args);
    }

    public static <T> T execute(RedisScript<T> script, RedisSerializer<?> argsSerializer,
                         RedisSerializer<T> resultSerializer, List<String> keys, Object... args) {
        return redisTemplate.execute(script, argsSerializer, resultSerializer, keys, args);
    }

    public static <T extends Closeable> T executeWithStickyConnection(RedisCallback<T> callback) {
        return redisTemplate.executeWithStickyConnection(callback);
    }

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

    public static Long countExistingKeys(Collection<String> keys) {
        return redisTemplate.countExistingKeys(keys);
    }

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

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

    public static Boolean unlink(String key) {
        return redisTemplate.unlink(key);
    }

    public static Long unlink(Collection<String> keys) {
        return redisTemplate.unlink(keys);
    }

    public static DataType type(String key) {
        return redisTemplate.type(key);
    }

    public static Set<String> keys(String pattern) {
        return redisTemplate.keys(pattern);
    }

    public static String randomKey() {
        return redisTemplate.randomKey();
    }

    public static void rename(String oldKey, String newKey) {
        redisTemplate.rename(oldKey, newKey);
    }

    public static Boolean renameIfAbsent(String oldKey, String newKey) {
        return redisTemplate.renameIfAbsent(oldKey, newKey);
    }

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

    public static Boolean expireAt(String key, Date date) {
        return redisTemplate.expireAt(key, date);
    }

    public static Boolean persist(String key) {
        return redisTemplate.persist(key);
    }

    public static Boolean move(String key, int dbIndex) {
        return redisTemplate.move(key, dbIndex);
    }

    public static byte[] dump(String key) {
        return redisTemplate.dump(key);
    }

    public static void restore(String key, byte[] value, long timeToLive, TimeUnit unit, boolean replace) {
        redisTemplate.restore(key, value, timeToLive, unit, replace);
    }

    public static Long getExpire(String key) {
        return redisTemplate.getExpire(key);
    }

    public static Long getExpire(String key, TimeUnit timeUnit) {
        return redisTemplate.getExpire(key, timeUnit);
    }

    public static List<Object> sort(SortQuery<String> query) {
        return redisTemplate.sort(query);
    }

    public static <T> List<T> sort(SortQuery<String> query, RedisSerializer<T> resultSerializer) {
        return redisTemplate.sort(query, resultSerializer);
    }

    public static <T> List<T> sort(SortQuery<String> query, BulkMapper<T, Object> bulkMapper) {
        return redisTemplate.sort(query, bulkMapper);
    }

    public static <T, S> List<T> sort(SortQuery<String> query, BulkMapper<T, S> bulkMapper, RedisSerializer<S> resultSerializer) {
        return redisTemplate.sort(query, bulkMapper, resultSerializer);
    }

    public static Long sort(SortQuery<String> query, String storeKey) {
        return redisTemplate.sort(query, storeKey);
    }

    public static void watch(String key) {
        redisTemplate.watch(key);
    }

    public static void watch(Collection<String> keys) {
        redisTemplate.watch(keys);
    }

    public static void unwatch() {
        redisTemplate.unwatch();
    }

    public static void multi() {
        redisTemplate.multi();
    }

    public static void discard() {
        redisTemplate.discard();
    }

    public static List<Object> exec() {
        return redisTemplate.exec();
    }

    public static List<Object> exec(RedisSerializer<?> valueSerializer) {
        return redisTemplate.exec(valueSerializer);
    }

    public static List<RedisClientInfo> getClientList() {
        return redisTemplate.getClientList();
    }

    public static void killClient(String host, int port) {
        redisTemplate.killClient(host, port);
    }

    public static void slaveOf(String host, int port) {
        redisTemplate.slaveOf(host, port);
    }

    public static void slaveOfNoOne() {
        redisTemplate.slaveOfNoOne();
    }

    public static void convertAndSend(String destination, Object message) {
        redisTemplate.convertAndSend(destination, message);
    }

    public static ClusterOperations<String, Object> opsForCluster() {
        return redisTemplate.opsForCluster();
    }

    public static GeoOperations<String, Object> opsForGeo() {
        return redisTemplate.opsForGeo();
    }

    public static BoundGeoOperations<String, Object> boundGeoOps(String key) {
        return redisTemplate.boundGeoOps(key);
    }

    public static <HK, HV> HashOperations<String, HK, HV> opsForHash() {
        return redisTemplate.opsForHash();
    }

    public static <HK, HV> BoundHashOperations<String, HK, HV> boundHashOps(String key) {
        return redisTemplate.boundHashOps(key);
    }

    public static HyperLogLogOperations<String, Object> opsForHyperLogLog() {
        return redisTemplate.opsForHyperLogLog();
    }

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

    public static BoundListOperations<String, Object> boundListOps(String key) {
        return redisTemplate.boundListOps(key);
    }

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

    public static BoundSetOperations<String, Object> boundSetOps(String key) {
        return redisTemplate.boundSetOps(key);
    }

    public static <HK, HV> StreamOperations<String, HK, HV> opsForStream() {
        return redisTemplate.opsForStream();
    }

    public static <HK, HV> StreamOperations<String, HK, HV> opsForStream(HashMapper<? super String, ? super HK, ? super HV> hashMapper) {
        return redisTemplate.opsForStream(hashMapper);
    }

    public static <HK, HV> BoundStreamOperations<String, HK, HV> boundStreamOps(String key) {
        return redisTemplate.boundStreamOps(key);
    }

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

    public static BoundValueOperations<String, Object> boundValueOps(String key) {
        return redisTemplate.boundValueOps(key);
    }

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

    public static BoundZSetOperations<String, Object> boundZSetOps(String key) {
        return redisTemplate.boundZSetOps(key);
    }

    public static RedisSerializer<?> getKeySerializer() {
        return redisTemplate.getKeySerializer();
    }

    public static RedisSerializer<?> getValueSerializer() {
        return redisTemplate.getValueSerializer();
    }

    public static RedisSerializer<?> getHashKeySerializer() {
        return redisTemplate.getHashKeySerializer();
    }

    public static RedisSerializer<?> getHashValueSerializer() {
        return redisTemplate.getHashValueSerializer();
    }

}

使用案例:

Redis.value().set("key", "value");
Redis.value().get("key");

Redis.list().rightPush("key", "value");
Redis.list().leftPop("key");
Redis.list().range("key", 0, 10);

Redis.hash().put("key", "hash_key", "value");
Redis.hash().get("key", "hash_key");
Redis.hash().keys("key");
Redis.hash().values("key");

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值