DownGradeCacheService.java 1.16 KB
package com.yoho.search.common.downgrade;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yoho.search.common.cache.impls.SearchRedis;
import com.yoho.search.common.utils.RedisCacheUtils;

@Service
public class DownGradeCacheService {

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

	@Autowired
	private SearchRedis searchRedisCache;

	public boolean exist(String key) {
		try {
			return RedisCacheUtils.exist(searchRedisCache.searchRedisTemplate, key);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}
	}

	public <T> T safeGet(String key, Class<T> clazz) {
		try {
			return RedisCacheUtils.get(searchRedisCache.searchValueOperations, key, clazz);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return null;
		}
	}

	public <T> void safeAdd(String key, T t, int expiredTimeInMinute) {
		try {
			RedisCacheUtils.add(searchRedisCache.searchValueOperations, key, t, expiredTimeInMinute);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}

	}

}