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

com.soento.redis.util.RedisUtil Maven / Gradle / Ivy

package com.soento.redis.util;

import com.soento.core.consts.Constants;
import com.soento.core.util.SpringUtil;
import com.soento.redis.support.GenericSerializationStrategy;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import java.util.concurrent.TimeUnit;

/**
 * @author yantao.zeng
 */
public final class RedisUtil {
    private static final GenericSerializationStrategy SERIALIZER = new GenericSerializationStrategy();

    private RedisUtil() {
    }

    public static RedisConnectionFactory getConnectionFactory(String alias) {
        return SpringUtil.getBean(alias + "RedisConnectionFactory", RedisConnectionFactory.class);
    }

    public static RedisConnectionFactory getConnectionFactory() {
        return getConnectionFactory(Constants.DEFAULT_REDIS_ALIAS);
    }

    public static RedisConnection getConnection(String alias) {
        return getConnectionFactory(alias).getConnection();
    }

    public static RedisConnection getConnection() {
        return getConnection(Constants.DEFAULT_REDIS_ALIAS);
    }

    public static byte[] serialize(Object obj) {
        if (obj instanceof String) {
            return SERIALIZER.serialize((String) obj);
        }
        return SERIALIZER.serialize(obj);
    }

    public static  T deserialize(byte[] bytes, Class clazz) {
        if (clazz == String.class) {
            return (T) SERIALIZER.deserialize(bytes);
        }
        return SERIALIZER.deserialize(bytes, clazz);
    }

    public static RedisTemplate redisTemplate(String alias) {
        return SpringUtil.getBean(alias + "RedisTemplate", RedisTemplate.class);
    }

    public static RedisTemplate redisTemplate() {
        return redisTemplate(Constants.DEFAULT_REDIS_ALIAS);
    }

    public static StringRedisTemplate stringRedisTemplate(String alias) {
        return SpringUtil.getBean(alias + "StringRedisTemplate", StringRedisTemplate.class);
    }

    public static StringRedisTemplate stringRedisTemplate() {
        return stringRedisTemplate(Constants.DEFAULT_REDIS_ALIAS);
    }

    public static ValueOperations value(String redisName) {
        return redisTemplate(redisName).opsForValue();
    }

    public static ValueOperations value() {
        return value(Constants.DEFAULT_REDIS_ALIAS);
    }

    public static HashOperations hash(String redisName) {
        return redisTemplate(redisName).opsForHash();
    }

    public static HashOperations hash() {
        return hash(Constants.DEFAULT_REDIS_ALIAS);
    }

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

    public static void set(Object key, Object value) {
        set(Constants.DEFAULT_REDIS_ALIAS, key, value);
    }

    public static void set(String redisName, Object key, Object value, long timeout) {
        value(redisName).set(key, value, timeout, TimeUnit.SECONDS);
    }

    public static void set(Object key, Object value, long timeout) {
        set(Constants.DEFAULT_REDIS_ALIAS, key, value, timeout);
    }

    public static Object get(String redisName, Object key) {
        return value(redisName).get(key);
    }

    public static Object get(Object key) {
        return get(Constants.DEFAULT_REDIS_ALIAS, key);
    }

    public static void hashSet(String redisName, Object name, Object key, Object value) {
        hash(redisName).put(name, key, value);
    }

    public static void hashSet(Object name, Object key, Object value) {
        hashSet(Constants.DEFAULT_REDIS_ALIAS, name, key, value);
    }

    public static Object hashGet(String redisName, Object name, Object key) {
        return hash(redisName).get(name, key);
    }

    public static Object hashGet(Object name, Object key) {
        return hashGet(Constants.DEFAULT_REDIS_ALIAS, name, key);
    }

    public static void delete(String redisName, Object key) {
        redisTemplate(redisName).delete(key);
    }

    public static void delete(Object key) {
        delete(Constants.DEFAULT_REDIS_ALIAS, key);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy