Redis 和 spring 的整合

         Redis是一个数据缓存的数据库,他的优点和缺点和memcash的比较都在redis分享.rar ppt中介绍,我就不多说了。

         redis和Spring的整合的bean的配置文件为:

        

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
	
<!-- POOL配置 --> 
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> 
    <property name="maxActive"  value="20" /> 
    <property name="maxIdle" value="10" /> 
    <property name="maxWait" value="1000" /> 
    <property name="testOnBorrow"  value="true"/> 
</bean> 
 
<!-- jedis shard信息配置 --> 
<bean id="jedis.shardInfo" class="redis.clients.jedis.JedisShardInfo"> 
    <constructor-arg index="0" value="192.168.10.34" /> 
    <constructor-arg index="1" value="6379" />
</bean> 
 
<!-- jedis shard pool配置 --> 
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool"> 
    <constructor-arg index="0" ref="jedisPoolConfig" /> 
    <constructor-arg index="1"> 
        <list> 
            <ref bean="jedis.shardInfo" /> 
        </list> 
    </constructor-arg> 
</bean> 
 <bean id="redisUtil" class="com.xingkongyongheng.trade.util.RedisUtil"> 
    <property name="shardedJedisPool" ref="shardedJedisPool" /> 
</bean> 
 <bean id="httpServerPipelineFactory" class="org.jboss.netty.example.http.snoop.HttpServerPipelineFactory" scope="prototype"> 
    <property name="httpRequestHandler" ref="httpRequestHandler" /> 
 </bean> 
 <bean id="httpRequestHandler" class="org.jboss.netty.example.http.snoop.HttpRequestHandler" scope="prototype"> 
    <property name="databaseUtil" ref="databaseUtil" /> 
    <property name="redisUtil" ref="redisUtil" /> 
 </bean> 
</beans>

 可以单独写到一个xml文件中。

  下面是一个获取ShardedJedis的一个类和一些简单方法的封装。

import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

public class RedisUtil {
	/**  
     * 数据源  
     */ 
    private static ShardedJedisPool shardedJedisPool = (ShardedJedisPool) SpringHelper.getBean("shardedJedisPool");  
      
    
      
    /**  
     * 获取数据库连接  
     * @return conn  
     */ 
    public static ShardedJedis getConnection() {  
        ShardedJedis jedis=null;  
        try {  
            jedis=shardedJedisPool.getResource();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return jedis;  
    }  
      
    /**  
     * 关闭数据库连接  
     * @param conn  
     */ 
    public static void closeConnection(ShardedJedis jedis) {  
        if (null != jedis) {  
            try {  
                shardedJedisPool.returnResource(jedis);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
    }  
 
    /**  
     * 设置数据  
     * @param conn  
     */ 
    public boolean setData(String key,String value) {  
            try {  
                ShardedJedis jedis=shardedJedisPool.getResource();  
                jedis.set(key,value);  
                shardedJedisPool.returnResource(jedis);  
                return true;  
            } catch (Exception e) {  
                e.printStackTrace();  
                  
            }  
        return false;  
    }  
      
    /**  
     * 获取数据  
     * @param conn  
     */ 
    public String getData(String key) {  
        String value=null;  
            try {  
                ShardedJedis jedis=shardedJedisPool.getResource();  
                value=jedis.get(key);  
                shardedJedisPool.returnResource(jedis);  
                return value;  
            } catch (Exception e) {  
                e.printStackTrace();  
                  
            }  
        return value;  
    }  
      
    /**  
     * 设置连接池  
     * @return 数据源  
     */ 
    public void setShardedJedisPool(ShardedJedisPool shardedJedisPool) {  
        this.shardedJedisPool = shardedJedisPool;  
    }  
 
    /**  
     * 获取连接池  
     * @return 数据源  
     */ 
    public ShardedJedisPool getShardedJedisPool() {  
        return shardedJedisPool;  
    }   
   
}

 2.怎样存储一个对象的list,这个是很纠结,因为redis的参数只有String和byte[]数组,我的做法是把那个list变成的一个json的字符串放进去了。下面是我写的例子:

  public void setList(String key,List  list){
       jedis.lpush("user", JSON.toJSONString(list)); 
       pool.returnResource(jedis);
   }

 其他的方法不会,要是谁有更好的方法,方便的话在评论上写一下思路或者参考资料,谢谢!

猜你喜欢

转载自1025358610.iteye.com/blog/2102583