redis在项目中的配置及使用

1、配置redis

	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath*:redis.properties</value>
            </list>
        </property>
    </bean>
	<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxIdle" value="${redis.maxIdle}" />
		<property name="maxTotal" value="${redis.maxTotal}" />
		<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
		<property name="testOnBorrow" value="${redis.testOnBorrow}" />
	</bean>

	<bean id="connectionFactory"
		class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
		p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.password}"
		p:pool-config-ref="poolConfig" />

	<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
		<property name="connectionFactory" ref="connectionFactory" />
	</bean>

2、redis使用

import com.google.common.base.Function;
/**
 * Redis工具类
 * 
 * /
public class RedisUtil {
	private static StringRedisTemplate redisTemplate = null;
	private static ApplicationContext app = null;
	static{  
		app = new ClassPathXmlApplicationContext(new String[]{"classpath:redis-context.xml"});  
	        redisTemplate = (StringRedisTemplate) app.getBean("redisTemplate"); 
	}
	
	/**
	 *
	 * @param key 需实现方法入参
	 * @param clazz 序列化/反序列化的类
	 * @param redisKey 需要存取的redis KEY
	 * @param timeout redis超时
	 * @param function 数据源方法
	 * @param <K> 入参
	 * @param <V> 出参
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <K, V> V getCachedData(K key, Class<V> clazz,
			String redisKey, Integer timeout, Function<K, V> function) {
		boolean redisValidFlag = true;
		String storeValue;
		V storeObject = null;
		try {
			BoundValueOperations<String, String> op = redisTemplate
					.boundValueOps(redisKey);
			storeValue = op.get();
			if (StringUtils.isNotBlank(storeValue)) {
				if (!clazz.equals(String.class)) {
					storeObject = JSON.parseObject(storeValue, clazz);
				} else {
					storeObject = (V) storeValue;
				}
			} else {
				storeObject = function.apply(key);
				if (!clazz.equals(String.class)) {
					storeValue = JSON.toJSONString(storeObject);
				} else {
					storeValue = (String) storeObject;
				}
				if (StringUtils.isNotBlank(storeValue)) {
					op.set(storeValue);
					if (op.getExpire() < 0) {
						op.expire(timeout, TimeUnit.MINUTES);
					}
				}
			}
		} catch (Exception e) {
			redisValidFlag = false;
		}
		if (!redisValidFlag) {
			storeObject = function.apply(key);
		}
		return storeObject;
	}

	/**
	 * 删除key
	 * 
	 * @param key
	 */
	public static void deleteKey(String key) {
		try {
			redisTemplate.delete(key);
		} catch (Exception e) {

		}
	}

	/**
	 * 更新key过期时间
	 * 
	 * @param key
	 */
	public static Boolean updateKeyExpire(String key, Integer timeOut) {
		try {
			return redisTemplate.expire(key, timeOut, TimeUnit.MINUTES);
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 获取key内容
	 * 
	 * @param key
	 */
	public static String getKeyContent(String key) {
		try {
			BoundValueOperations<String, String> stringStringBoundValueOperations = redisTemplate
					.boundValueOps(key);
			return stringStringBoundValueOperations.get();
		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 设置key内容
	 * 
	 * @param key
	 */
	public static void setKeyContent(String key, String content) {
		try {
			BoundValueOperations<String, String> stringStringBoundValueOperations = redisTemplate
					.boundValueOps(key);
			stringStringBoundValueOperations.set(content);
		} catch (Exception e) {

		}
	}

	/**
	 * 模糊删除key
	 * 
	 * @param pattern("*"+suffix)
	 */
	public static void deleteKeyByPattern(String pattern) {
		Set<String> keys = redisTemplate.keys(pattern);
		if (!keys.isEmpty()) {
			redisTemplate.delete(keys);
		}
	}
	
	/**
	 *批量删除指定key
	 */
	public static void deleteByKeys(String... keys) { 
		redisTemplate.delete(Arrays.asList(keys)); 
	}
}

转载于:https://my.oschina.net/okqq/blog/738595

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值