一、添加Redis Maven依赖
- <span style="white-space:pre;"> </span><!-- redis -->
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>2.6.2</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.data</groupId>
- <artifactId>spring-data-redis</artifactId>
- <version>1.7.1.RELEASE</version>
- </dependency>
- redis.host=192.168.0.254
- redis.port=6379
- redis.password=jedis21tb
- redis.maxIdle=300
- redis.maxWait=1000
- redis.testOnBorrow=true
- redis.timeout=3000
三、spring配置文件(applicationContext.xml):
- <!--引入配置属性文件 -->
- <context:property-placeholder location="classpath:config.properties" />
- <!-- redis -->
- <!-- jedis 配置 -->
- <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig" >
- <property name="maxIdle" value="${redis.maxIdle}" />
- <property name="maxWaitMillis" value="${redis.maxWait}" />
- <property name="testOnBorrow" value="${redis.testOnBorrow}" />
- </bean >
- <!-- redis服务器中心 -->
- <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >
- <property name="poolConfig" ref="poolConfig" />
- <property name="port" value="${redis.port}" />
- <property name="hostName" value="${redis.host}" />
- <property name="password" value="${redis.password}" />
- <property name="timeout" value="${redis.timeout}" ></property>
- </bean >
- <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" >
- <property name="connectionFactory" ref="connectionFactory" />
- <property name="keySerializer" >
- <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
- </property>
- <!-- <property name="valueSerializer" >
- <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
- </property> -->
- </bean >
- <bean id="redisUtil" class="com.kttx.common.RedisUtil" >
- <property name="redisTemplate" ref="redisTemplate" />
- </bean >
- package com.kttx.common;
- import java.io.Serializable;
- import java.util.Set;
- import java.util.concurrent.TimeUnit;
- import org.apache.log4j.Logger;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.data.redis.core.ValueOperations;
- /**
- * redis
- * @author txs
- *
- */
- public class RedisUtil {
- private static Logger logger = Logger.getLogger(RedisUtil.class);
- private static RedisTemplate<Serializable, Object> redisTemplate;
- /**
- * 写入或更新缓存
- * @param key
- * @param value
- * @return
- */
- public static boolean set(final String key, Object value)
- {
- boolean result = false;
- try {
- ValueOperations<Serializable, Object> operations = redisTemplate
- .opsForValue();
- operations.set(key, value);
- result = true;
- } catch (Exception e) {
- logger.error("write redis is faill");
- e.printStackTrace();
- }
- return result;
- }
- /**
- * 写入缓存
- * 设置失效时间
- * @param key
- * @param value
- * @return
- */
- public static boolean set(final String key, Object value, Long expireTime) {
- boolean result = false;
- try {
- ValueOperations<Serializable, Object> operations = redisTemplate
- .opsForValue();
- operations.set(key, value);
- redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
- result = true;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return result;
- }
- /**
- * 读取缓存
- * @param key
- * @return
- */
- public static Object get(final String key)
- {
- Object result = null;
- ValueOperations<Serializable, Object> operations = redisTemplate
- .opsForValue();
- result = operations.get(key);
- return result;
- }
- /**
- * 删除对应的value
- * @param key
- */
- public static void remove(final String key)
- {
- if (exists(key)) {
- redisTemplate.delete(key);
- }
- }
- /**
- * 批量删除对应的value
- *
- * @param keys
- */
- public static void remove(final String... keys) {
- for (String key : keys) {
- remove(key);
- }
- }
- /**
- * 批量删除key
- *
- * @param pattern 正则表达式
- */
- public static void removePattern(final String pattern) {
- Set<Serializable> keys = redisTemplate.keys(pattern);
- if (keys.size() > 0)
- redisTemplate.delete(keys);
- }
- /**
- * 判断缓存中是否有对应的value
- *
- * @param key
- * @return
- */
- public static boolean exists(final String key) {
- return redisTemplate.hasKey(key);
- }
- public void setRedisTemplate(
- RedisTemplate<Serializable, Object> redisTemplate) {
- this.redisTemplate = redisTemplate;
- }
- }