RedisCacheUtils.java 3.21 KB
package com.yoho.search.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.yoho.core.redis.cluster.operations.nosync.YHRedisTemplate;
import com.yoho.core.redis.cluster.operations.nosync.YHValueOperations;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.search.base.utils.SearchCollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.concurrent.TimeUnit;

public class RedisCacheUtils {

	public static <T> T get(YHValueOperations valueOperations, RedisKeyBuilder redisKeyBuilder, Class<T> clazz) {
		RedisKeyBuilder keyBuilder = RedisKeyBuilder.newInstance().appendFixed(redisKeyBuilder);
		String compressedVal = valueOperations.get(keyBuilder);
		if (StringUtils.isBlank(compressedVal)) {
			return null;
		}
		String uncompressStr = SnappyUtils.uncompress(compressedVal);
		return unserializeFromString(uncompressStr, clazz);
	}

	public static <T> void add(YHValueOperations valueOperations, RedisKeyBuilder redisKeyBuilder, T t, int expiredTimeInMinute) {
		String uncompressStr = serializeToString(t);
		String compressedVal = SnappyUtils.compress(uncompressStr);
		if (StringUtils.isBlank(compressedVal)) {
			return;
		}
		valueOperations.set(redisKeyBuilder, compressedVal, expiredTimeInMinute, TimeUnit.MINUTES);
	}

	public static boolean exist(YHRedisTemplate redisTemplate, RedisKeyBuilder redisKeyBuilder) {
		return redisTemplate.hasKey(redisKeyBuilder);
	}

	public static void mutiSet(YHRedisTemplate redisTemplate, Map<RedisKeyBuilder, String> map, int expiredTimeInMinute) {
		redisTemplate.mset(map, expiredTimeInMinute*60);
	}

	public static List<String> mutiGet(YHValueOperations valueOperations, Collection<RedisKeyBuilder> redisKeyBuilders) {
		return valueOperations.multiGet(redisKeyBuilders);
	}

	/**
	 * 设置bitset-整个设置
	 * @param redisTemplate
	 * @param redisKeyBuilder
	 * @param bitSet
	 * @param expiredTimeInMinute
	 * @return
	 */
	public static boolean setBitSet(YHRedisTemplate redisTemplate, RedisKeyBuilder redisKeyBuilder, BitSet bitSet, int expiredTimeInMinute) {
		return redisTemplate.setBits(redisKeyBuilder,bitSet,expiredTimeInMinute * 60);
	}

	/**
	 * 获取bitset
	 * @param redisTemplate
	 * @param redisKeyBuilder
	 * @param offsets
	 * @return
	 */
	public static Map<Integer, Boolean> getBits(YHRedisTemplate redisTemplate, RedisKeyBuilder redisKeyBuilder, Collection<Integer> offsets) {
		List<Long> LongOffsets = SearchCollectionUtils.toList(offsets,(a)->a.longValue());
		Map<Long, Boolean> redisResults =  redisTemplate.getBits(redisKeyBuilder,LongOffsets);
		if(redisResults==null || redisResults.isEmpty()){
			return null;
		}
		Map<Integer, Boolean> results = new HashMap<>();
		for (Map.Entry<Long, Boolean> entry: redisResults.entrySet()) {
			results.put(entry.getKey().intValue(),entry.getValue());
		}
		return results;
	}

	/**
	 * 序列化保持null值
	 *
	 * @param t
	 * @return
	 */
	private static <T> String serializeToString(T t) {
		return JSON.toJSONString(t, SerializerFeature.WriteMapNullValue);
	}

	private static <T> T unserializeFromString(String cacheObjectValue, Class<T> clazz) {
		return JSON.parseObject(cacheObjectValue, clazz);
	}

}