redis整合spring示例一

2015-06-12|来源:

这里使用java操作redis的示例,也是领悟书生网站项目中使用的代码.当然本示例也是参考网上的相关文章做了相应的改动。


首先看看redis所依懒的maven包

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.6.2</version>
</dependency>


spring的配置文件

<context:component-scan base-package="com._656463.redis" />
<context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <property name="maxTotal" value="50" />
     <property name="maxIdle" value="8" />
     <property name="maxWaitMillis" value="1000" />
     <property name="testOnBorrow" value="true"/>
     <property name="testOnReturn" value="true"/>
</bean>
  
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool" scope="singleton">
    <constructor-arg index="0" ref="jedisPoolConfig" />
    <constructor-arg index="1">
        <list>
            <bean class="redis.clients.jedis.JedisShardInfo">
                <constructor-arg name="host" value="${redis.host}" />
                <constructor-arg name="port" value="${redis.port}" />
                 <property name="password" value="${redis.pass}"/>
            </bean>
        </list>
    </constructor-arg>
</bean>

属性文件:

redis.host=121.40.80.59
redis.port=6379
redis.pass=dddddd
  
redis.pool.maxIdle=300
redis.pool.maxActive=600
redis.pool.maxWait=1000
redis.pool.testOnBorrow=true

数据源接口

package com._656463.redis;
  
import redis.clients.jedis.ShardedJedis;
  
public interface RedisDataSource {
  
    /**
     * 获取redis的客户端
     * 
     * @return
     */
    public abstract ShardedJedis getRedisClient();
  
    /**
     * 将资源返还给pool
     * 
     * @param shardedJedis
     */
    public void returnResource(ShardedJedis shardedJedis);
  
    /**
     * 出现异常后,将资源返还给pool (其实不需要第二个方法)
     * 
     * @param shardedJedis
     * @param broken
     */
    public void returnResource(ShardedJedis shardedJedis, boolean broken);
}

实现类

package com._656463.redis;
  
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
  
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
  
@Repository("redisDataSource")
public class RedisDataSourceImpl implements RedisDataSource {
  
    @Autowired
    private ShardedJedisPool shardedJedisPool;
  
    public ShardedJedis getRedisClient() {
        try {
            ShardedJedis shardJedis = shardedJedisPool.getResource();
            return shardJedis;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
  
    public void returnResource(ShardedJedis shardedJedis) {
        shardedJedisPool.returnResource(shardedJedis);
    }
  
    public void returnResource(ShardedJedis shardedJedis, boolean broken) {
        if (broken) {
            shardedJedisPool.returnBrokenResource(shardedJedis);
        } else {
            shardedJedisPool.returnResource(shardedJedis);
        }
    }
}

redis连接模板类RedisClientTemplate

package com._656463.redis;
  
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
  
import redis.clients.jedis.ShardedJedis;
  
@Repository("redisClientTemplate")
public class RedisClientTemplate {
  
    @Autowired
    private RedisDataSource redisDataSource;
  
    public void disconnect() {
        ShardedJedis shardedJedis = redisDataSource.getRedisClient();
        shardedJedis.disconnect();
    }
      
    public void delKey(String key){
        ShardedJedis shardedJedis = redisDataSource.getRedisClient();
        boolean broken = false;
        try {
            if (shardedJedis != null) {
                shardedJedis.del(key);
            }
        } catch (Exception e) {
            e.printStackTrace();
            broken = true;
        } finally {
            redisDataSource.returnResource(shardedJedis, broken);
        }
    }
      
    public void delKey(byte[] key){
        ShardedJedis shardedJedis = redisDataSource.getRedisClient();
        boolean broken = false;
        try {
            if (shardedJedis != null) {
                shardedJedis.del(key);
            }
        } catch (Exception e) {
            e.printStackTrace();
            broken = true;
        } finally {
            redisDataSource.returnResource(shardedJedis, broken);
        }
    }
      
    public void setExpire(String key,int seconds){
        ShardedJedis shardedJedis = redisDataSource.getRedisClient();
        boolean broken = false;
        try {
            if (shardedJedis != null) {
                shardedJedis.expire(key, seconds);
            }
        } catch (Exception e) {
            e.printStackTrace();
            broken = true;
        } finally {
            redisDataSource.returnResource(shardedJedis, broken);
        }
    }
      
    public void setExpire(byte[] key,int seconds){
        ShardedJedis shardedJedis = redisDataSource.getRedisClient();
        boolean broken = false;
        try {
            if (shardedJedis != null) {
                shardedJedis.expire(key, seconds);
            }
        } catch (Exception e) {
            e.printStackTrace();
            broken = true;
        } finally {
            redisDataSource.returnResource(shardedJedis, broken);
        }
    }
  
    /**
     * 设置单个值
     * 
     * @param key
     * @param value
     * @return
     */
    public String set(String key, String value) {
        String result = null;
  
        ShardedJedis shardedJedis = redisDataSource.getRedisClient();
        if (shardedJedis == null) {
            return result;
        }
        boolean broken = false;
        try {
            result = shardedJedis.set(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            broken = true;
        } finally {
            redisDataSource.returnResource(shardedJedis, broken);
        }
        return result;
    }
  
    /**
     * 获取单个值
     * 
     * @param key
     * @return
     */
    public String get(String key) {
        String result = null;
        ShardedJedis shardedJedis = redisDataSource.getRedisClient();
        if (shardedJedis == null) {
            return result;
        }
  
        boolean broken = false;
        try {
            result = shardedJedis.get(key);
  
        } catch (Exception e) {
            e.printStackTrace();
            broken = true;
        } finally {
            redisDataSource.returnResource(shardedJedis, broken);
        }
        return result;
    }
  
    /**
     * 存储byte对象
     * 
     * @param key
     * @param value
     */
    public void set(byte[] key, byte[] value) {
  
        ShardedJedis shardedJedis = redisDataSource.getRedisClient();
        if (shardedJedis == null) {
            return;
        }
        boolean broken = false;
        try {
            shardedJedis.set(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            broken = true;
        } finally {
            redisDataSource.returnResource(shardedJedis, broken);
        }
    }
  
    /**
     * 获取byte[]对象
     * 
     * @param key
     * @return
     */
    public byte[] get(byte[] key) {
        ShardedJedis shardedJedis = redisDataSource.getRedisClient();
        if (shardedJedis == null) {
            return null;
        }
  
        boolean broken = false;
        try {
            return shardedJedis.get(key);
        } catch (Exception e) {
            e.printStackTrace();
            broken = true;
        } finally {
            redisDataSource.returnResource(shardedJedis, broken);
        }
        return null;
    }
}


先到这,下篇继续


相关问答

更多

spring mvc+mybatis怎么整合redis

基于这段时间折腾redis遇到了各种问题,想着整理一下。本文主要介绍基于Spring+Mybatis以注解的形式整合Redis。

跪求:struts2+hibernete+spring+extjs4.0 整合框架最好上面有个简单示例!!!谢谢满意了再加100分

我觉得搭建框架必须要自己来动手做!可能前期会很困扰,出现很多错误。但是只要有一次搭建成功的话,你就会豁然开朗。因为每个人的思路,方法都不一样。单纯的借用别人的框架去完成功能,之后会更困扰的!

谁有spring2.5的项目示例

在 Spring官网下jar包,里面自带了个jpetstore 项目,是用iBATIS + Spring + Struts技术实现的。

spring 和 redis 整合 需要哪些jar包

java使用redis缓存可以使用jedis框架,jedis操作简单,没有什么复杂的东西需要学习,网上资料很多,随便看看就会了. 将spring与redis缓存集成,其实也是使用jedis框架,只不过spring对它进行了一层封装,并将这层封装库命名为spring-data-redis. 下面将要使用spring-data-redis与jedis的jar包,并通过spring的aop功能,将redis缓存无缝无侵入的整合进来. 可以到http://download.csdn.net/detail/ ...

spring 和 redis 整合 需要哪些jar包

java使用redis缓存可以使用jedis框架,jedis操作简单,没有什么复杂的东西需要学习,网上资料很多,随便看看就会了. 将spring与redis缓存集成,其实也是使用jedis框架,只不过spring对它进行了一层封装,并将这层封装库命名为spring-data-redis. 下面将要使用spring-data-redis与jedis的jar包,并通过spring的aop功能,将redis缓存无缝无侵入的整合进来. 可以到http://download.csdn.net/detail/ ...

专题教程

相关文章

更多

最近更新

更多