SearchRedis.java 3.56 KB
package com.yoho.search.cache.impls;

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.cache.model.CacheObject;
import com.yoho.search.common.utils.RedisCacheUtils;
import com.yoho.search.core.redis.components.YohoSearchRedisComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.BitSet;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service("searchRedis")
public class SearchRedis implements CacheInterface {

    private static final Logger logger = LoggerFactory.getLogger(SearchRedis.class);

    @Autowired
    private YohoSearchRedisComponent yohoSearchRedisComponent;

    @Override
    public boolean exist(RedisKeyBuilder redisKeyBuilder) {
        try {
            YHRedisTemplate redisTemplate = yohoSearchRedisComponent.getRedisTemplate();
            return RedisCacheUtils.exist(redisTemplate, redisKeyBuilder);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return false;
        }
    }

    @Override
    public CacheObject get(RedisKeyBuilder redisKeyBuilder) {
        try {
            YHValueOperations valueOperations = yohoSearchRedisComponent.getValueOperations();
            return RedisCacheUtils.get(valueOperations, redisKeyBuilder, CacheObject.class);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        }
    }

    @Override
    public void addOrUpdate(RedisKeyBuilder redisKeyBuilder, CacheObject value, int expiredTimeInMinute) {
        try {
            YHValueOperations valueOperations = yohoSearchRedisComponent.getValueOperations();
            RedisCacheUtils.add(valueOperations, redisKeyBuilder, value, expiredTimeInMinute);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    @Override
    public void mutiSet(Map<RedisKeyBuilder, String> map, int expiredTimeInMinute) {
        YHRedisTemplate redisTemplate = yohoSearchRedisComponent.getRedisTemplate();
        RedisCacheUtils.mutiSet(redisTemplate, map, expiredTimeInMinute);
    }

    @Override
    public List<String> mutiGet(Collection<RedisKeyBuilder> redisKeyBuilders) {
        YHValueOperations valueOperations = yohoSearchRedisComponent.getValueOperations();
        return RedisCacheUtils.mutiGet(valueOperations, redisKeyBuilders);
    }


    @Override
    public boolean setBitSet(RedisKeyBuilder redisKeyBuilder, BitSet bitSet, int expiredTimeInMinute) {
        try {
            return RedisCacheUtils.setBitSet(yohoSearchRedisComponent.getRedisTemplate(), redisKeyBuilder, bitSet, expiredTimeInMinute);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return false;
        }
    }

    @Override
    public Map<Integer, Boolean> getFromBitSet(RedisKeyBuilder key, Collection<Integer> offsets) {
        try {
            YHRedisTemplate redisTemplate = yohoSearchRedisComponent.getRedisTemplate();
            if (!redisTemplate.hasKey(key)) {
                return null;
            }
            return RedisCacheUtils.getBits(redisTemplate, key, offsets);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        }
    }

}