spring和redis集成

spring配置文件
<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" 
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

	<!-- 使用Spring组件扫描的方式来实现自动注入bean -->
	<!-- <context:component-scan base-package="com.zyc"/> -->
	<context:property-placeholder location="classpath:redis.properties" />
	<!-- 隐式地向 Spring 容器注册  -->
	<context:annotation-config/>
	
	<import resource="spring-redis.xml"/>
</beans>
redis.properties 配置文件如下

#redis\u4E2D\u5FC3 
redis.host=127.0.0.1  
redis.port=6379 
redis.password=123456 
redis.maxIdle=100 
redis.maxActive=300 
redis.maxWait=1000 
redis.testOnBorrow=true 
redis.timeout=100000 
 
# \u4E0D\u9700\u8981\u52A0\u5165\u7F13\u5B58\u7684\u7C7B 
targetNames=xxxRecordManager,xxxSetRecordManager,xxxStatisticsIdentificationManager 
# \u4E0D\u9700\u8981\u7F13\u5B58\u7684\u65B9\u6CD5 
methodNames= 
 
#\u8BBE\u7F6E\u7F13\u5B58\u5931\u6548\u65F6\u95F4 
com.service.impl.xxxRecordManager= 60 
com.service.impl.xxxSetRecordManager= 60 
defaultCacheExpireTime=3600 
 
fep.local.cache.capacity =10000 


spring-reids配置文件

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" 
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

	 <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 > 
 
     <!-- cache配置 --> 
   
    <bean id="redisUtil" class="com.zyc.util.RedisUtil" > 
          <property name="redisTemplate" ref="redisTemplate" /> 
    </bean > 
	
</beans>
com.zyc.util.RedisUtil 工具类

package com.zyc.util;

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;

public class RedisUtil {
	private Logger logger = Logger.getLogger(RedisUtil.class); 
	private RedisTemplate<Serializable, Object> redisTemplate; 
	 
	/** 
	  * 批量删除对应的value 
	  * 
	  * @param keys 
	  */ 
	public void remove(final String... keys) { 
	  for (String key : keys) { 
	   remove(key); 
	  } 
	} 
	 
	/** 
	  * 批量删除key 
	  * 
	  * @param pattern 
	  */ 
	public void removePattern(final String pattern) { 
	  Set<Serializable> keys = redisTemplate.keys(pattern); 
	  if (keys.size() > 0) 
	   redisTemplate.delete(keys); 
	} 
	 
	/** 
	  * 删除对应的value 
	  * 
	  * @param key 
	  */ 
	public void remove(final String key) { 
	  if (exists(key)) { 
	   redisTemplate.delete(key); 
	  } 
	} 
	 
	/** 
	  * 判断缓存中是否有对应的value 
	  * 
	  * @param key 
	  * @return 
	  */ 
	public boolean exists(final String key) { 
	  return redisTemplate.hasKey(key); 
	}

       /**
     * 获取所有的key
     * @return
     */
    public Set<Serializable> keys() {
        return redisTemplate.keys("*");
    }

       /** 
	  * 读取缓存 
	  * 
	  * @param key 
	  * @return 
	  */ 
	public Object get(final String key) { 
	  Object result = null; 
	  ValueOperations<Serializable, Object> operations = redisTemplate 
	    .opsForValue(); 
	  result = operations.get(key); 
	  return result; 
	} 
	 
	/** 
	  * 写入缓存 
	  * 
	  * @param key 
	  * @param value 
	  * @return 
	  */ 
	public 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) { 
	   e.printStackTrace(); 
	  } 
	  return result; 
	} 
	 
	/** 
	  * 写入缓存 
	  * 
	  * @param key 
	  * @param value 
	  * @return 
	  */ 
	public 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; 
	} 
	 
	public void setRedisTemplate( 
	   RedisTemplate<Serializable, Object> redisTemplate) { 
	  this.redisTemplate = redisTemplate; 
	} 
}

测试类

package com.zyc;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zyc.service.Data1;
import com.zyc.util.RedisUtil;

public class MainRedisClass {
	public static Logger logger=Logger.getLogger(MainRedisClass.class);
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		PropertyConfigurator.configure("C:/Users/zhao/Desktop/log4j.properties");//这里配置自己的log4j 也可不用注释了也可以
		ApplicationContext context = new ClassPathXmlApplicationContext("spring-applicationContext.xml");//
		
		logger.info("ke yi yong log4j le");
		
		RedisUtil redisUtil= (RedisUtil) context.getBean("redisUtil");
		redisUtil.set("abc", "123");
		
		
	}

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值