All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gc.easy.redis.util.RedisUtil Maven / Gradle / Ivy

The newest version!
package com.gc.easy.redis.util;

import org.redisson.api.*;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {
    private final RedissonClient redissonClient;

    public RedisUtil(RedissonClient redissonClient) {
        this.redissonClient = redissonClient;
    }


    public Long increment(String key) {
        RAtomicLong atomicLong = redissonClient.getAtomicLong(key);
        return atomicLong.incrementAndGet();
    }

    public Long decrement(String key) {
        RAtomicLong atomicLong = redissonClient.getAtomicLong(key);
        return atomicLong.decrementAndGet();
    }
    /**
     * 发布订阅的消息
     * @param topic
     * @param message
     */
    public void pushSubscriberMessage(String topic,String message) {
        RTopic rtopic = redissonClient.getTopic(topic);
        rtopic.publish(message);
    }

    /**
     * 发布消息队列
     * @param topic
     * @param message
     */
    public void pushQueueMessage(String topic,String message) {
        RBlockingQueue queue = redissonClient.getBlockingQueue(topic);
        queue.offer(message);
    }

    public  T get(String key, Class clazz) {
        RBucket bucket = redissonClient.getBucket(key);
        return bucket.get();
    }
    public boolean set(String key, Object value) {
        RBucket bucket = redissonClient.getBucket(key);
        return bucket.trySet(value);
    }
    public boolean set(String key, Object value, int expireTime) {
        RMap map = redissonClient.getMap(key);
        map.expire(expireTime, TimeUnit.SECONDS);
        return map.fastPut("", value);
    }
    public boolean delete(String key) {
        RBucket bucket = redissonClient.getBucket(key);
        return bucket.delete();
    }
    public boolean hasKey(String key) {
        RBucket bucket = redissonClient.getBucket(key);
        return bucket.isExists();
    }
}