Redis tool class and xml configuration

package com.a.b.util;

import java.io.Serializable;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * Redis工具类 版本号 1.0.0
 * 
 * @author
 * 
 */
public final class RedisUtil {
	// redis操作模板
	private static RedisTemplate<Serializable, Object> redisTemplate;

	public static void setRedisTemplate(
			RedisTemplate<Serializable, Object> redisTemplate) {
		RedisUtil.redisTemplate = redisTemplate;
	}

	/**
	 * 写入缓存
	 * 
	 * @param key
	 *            缓存key
	 * @param value
	 *            缓存值
	 * @param expireTime
	 *            缓存过期时间(秒)
	 * @return 成功标识
	 * @throws Exception
	 */
	public static void set(String key, Object value, long timeout)
			throws Exception {
		redisTemplate.opsForValue().set(key, value);
		if (timeout > 0) {
			redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
		}
	}

	/**
	 * 判断缓存中是否有对应key
	 * 
	 * @param key
	 *            缓存key
	 * @return 是否存在
	 * @throws Exception
	 */
	public static boolean exists(String key) throws Exception {
		return redisTemplate.hasKey(key);
	}

	/**
	 * 读取缓存
	 * 
	 * @param key
	 *            缓存key
	 * @return 缓存值
	 * @throws Exception
	 */
	public static Object get(String key) throws Exception {
		return redisTemplate.opsForValue().get(key);
	}

	/**
	 * 删除缓存
	 * 
	 * @param key
	 *            缓存key
	 * @throws Exception
	 */
	public static void remove(String key) throws Exception {
		if (exists(key)) {
			redisTemplate.delete(key);
		}
	}

	/**
	 * 批量删除缓存
	 * 
	 * @param keys
	 *            缓存key
	 * @throws Exception
	 */
	public static void remove(String... keys) throws Exception {
		if (keys != null && keys.length > 0) {
			for (String key : keys) {
				remove(key);
			}
		}
	}

	/**
	 * 批量删除缓存
	 * 
	 * @param pattern
	 *            key匹配条件
	 * @throws Exception
	 */
	public static void removePattern(String pattern) throws Exception {
		Set<Serializable> keys = redisTemplate.keys(pattern);
		if (keys != null && keys.size() > 0) {
			redisTemplate.delete(keys);
		}
	}
}
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:cache="http://www.springframework.org/schema/cache"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd 
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context.xsd 
	http://www.springframework.org/schema/cache 
	http://www.springframework.org/schema/cache/spring-cache.xsd">

	<!-- 加载资源文件 -->
	<context:property-placeholder location="classpath:redis.properties"
		ignore-unresolvable="true" />

	<!-- 配置连接池 -->
	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<!-- 连接池中最大连接数.高版本:maxTotal,低版本:maxActive -->
		<property name="maxTotal" value="${redis.maxTotal}" />
		<!-- 连接池中最大空闲的连接数 -->
		<property name="maxIdle" value="${redis.maxIdle}" />
		<!-- 连接池中最小空闲的连接数 -->
		<property name="minIdle" value="${redis.minIdle}" />
		<!-- 当连接池资源耗尽时,调用者最大阻塞的时间,超时将跑出异常.毫秒数;默认为-1.表示永不超时.高版本:maxWaitMillis,低版本:maxWait -->
		<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
		<!-- 连接空闲的最小时间,达到此值后空闲连接将可能会被移除.负值(-1)表示不移除 -->
		<property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}" />
		<!-- 对于"空闲链接"检测线程而言,每次检测的链接资源的个数.默认为3 -->
		<property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}" />
		<!-- "空闲链接"检测线程,检测的周期,毫秒数.如果为负值,表示不运行"检测线程".默认为-1 -->
		<property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}" />
		<!-- 向调用者输出"链接"资源时,是否检测是有有效,如果无效则从连接池中移除,并尝试获取继续获取.默认为false.建议保持默认值 -->
		<property name="testOnBorrow" value="${redis.testOnBorrow}" />
		<!-- 向连接池"归还"链接时,是否检测"链接"对象的有效性.默认为false.建议保持默认值 -->
		<property name="testOnReturn" value="${redis.testOnReturn}" />
		<!-- 向调用者输出"链接"对象时,是否检测它的空闲超时;默认为false.如果"链接"空闲超时,将会被移除.建议保持默认值 -->
		<property name="testWhileIdle" value="${redis.testWhileIdle}" />
	</bean>

	<!-- 配置连接工厂 -->
	<bean id="jedisConnectionFactory"
		class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
		destroy-method="destroy">
		<!-- 连接池配置 -->
		<property name="poolConfig" ref="jedisPoolConfig" />
		<!-- redis服务器地址 -->
		<property name="hostName" value="${redis.hostName}" />
		<!-- redis服务端口号 -->
		<property name="port" value="${redis.port}" />
		<!-- redis服务密码 -->
		<property name="password" value="${redis.password}" />
		<!-- 客户端超时时间,单位毫秒 -->
		<property name="timeout" value="${redis.timeout}" />
		<!-- 是否使用连接池 -->
		<property name="usePool" value="${redis.usePool}" />
		<!-- 链接数据库 -->
		<property name="database" value="${redis.database}" />
	</bean>

	<!-- 配置键序列化策略 -->
	<bean id="stringRedisSerializer"
		class="org.springframework.data.redis.serializer.StringRedisSerializer" />

	<!-- 配置缓存模板 -->
	<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
		<property name="connectionFactory" ref="jedisConnectionFactory" />
		<property name="keySerializer" ref="stringRedisSerializer" />
		<property name="hashKeySerializer" ref="stringRedisSerializer" />
	</bean>

	<!-- 配置缓存管理器 -->
	<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
		<constructor-arg name="template" ref="redisTemplate" />
		<!-- 配置缓存过期时间,单位秒 -->
		<property name="defaultExpiration" value="${redis.expireTime}" />
	</bean>
	
	<bean id="redisUtil" class="com.a.b.util.RedisUtil">
		<property name="redisTemplate" ref="redisTemplate" />
	</bean>

	<!-- 启用缓存注解功能 -->
	<cache:annotation-driven cache-manager="cacheManager" />
</beans>
redis.hostName=127.0.0.1
redis.port=6379
redis.password=
redis.timeout=15000
redis.usePool=true
redis.expireTime=3600
redis.database=1

redis.maxTotal=20
redis.maxIdle=10
redis.minIdle=1
redis.maxWaitMillis=5000
redis.testOnBorrow=false
redis.testOnReturn=false
redis.testWhileIdle=false
redis.minEvictableIdleTimeMillis=300000
redis.numTestsPerEvictionRun=3
redis.timeBetweenEvictionRunsMillis=60000

 

Guess you like

Origin blog.csdn.net/qq_32923295/article/details/114586930