spring-data-redis操作redis集群

在这里我就不说redis到底是如何集群的了,具体可以看我的博客,集群的好处大家可以看看
http://www.jianshu.com/p/ee2aa7fe341b
pom.xml

    <!-- jedis 配置 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="2048" />
        <property name="maxIdle" value="200" />
        <property name="numTestsPerEvictionRun" value="1024" />
        <property name="timeBetweenEvictionRunsMillis" value="30000" />
        <property name="minEvictableIdleTimeMillis" value="-1" />
        <property name="softMinEvictableIdleTimeMillis" value="10000" />
        <property name="maxWaitMillis" value="1500" />
        <property name="testOnBorrow" value="true" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnReturn" value="false" />
        <property name="jmxEnabled" value="true" />
        <property name="jmxNamePrefix" value="chbigdata" />
        <property name="blockWhenExhausted" value="false" />
    </bean>

    <!-- 单个节点 -->
    <bean id="connectionFactory"
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="poolConfig" ref="jedisPoolConfig" />
        <property name="hostName" value="192.168.0.134" />
        <property name="port" value="6379"></property>
    </bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory">
            <ref bean="connectionFactory" />
        </property>
    </bean>
    <!-- 集群配置 -->
    <bean class="redis.clients.jedis.JedisCluster">
        <constructor-arg index="3">
            <ref bean="jedisPoolConfig" />
        </constructor-arg>
        <constructor-arg index="2" type="int" value="6" />
        <constructor-arg index="1" type="int" value="5000" />
        <constructor-arg index="0">
            <set>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="192.168.0.133" />
                    <constructor-arg index="1" value="7000" />
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="192.168.0.133" />
                    <constructor-arg index="1" value="7002" />
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="192.168.0.133" />
                    <constructor-arg index="1" value="7003" />
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="192.168.0.133" />
                    <constructor-arg index="1" value="7004" />
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="192.168.0.133" />
                    <constructor-arg index="1" value="7005" />
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="192.168.0.133" />
                    <constructor-arg index="1" value="7006" />
                </bean>
            </set>
        </constructor-arg>
    </bean>
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import redis.clients.jedis.JedisCluster;

@Component
public class JedisClusterUtil {
    @Autowired
    private JedisCluster jedisCluster;

    private static Logger logger = LoggerFactory.getLogger(JedisClusterUtil.class);

    /**
     * 批量删除对应的value
     * 
     * @param keys
     */
    public void remove(final String... keys) {
        for (String key : keys) {
            remove(key);
        }
    }

    /**
     * 批量删除key
     * 
     * @param pattern
     */
    public void removePattern(final String pattern) {
        jedisCluster.del(pattern);
        logger.debug("del key >" + pattern);
    }

    /**
     * 删除对应的value
     * 
     * @param key
     */
    public void remove(final String key) {
        if (exists(key)) {
            jedisCluster.del(key);
            logger.debug("del key >" + key);
        } else {
            logger.debug("del key >" + key + "not exist");
        }
    }

    /**
     * 判断缓存中是否有对应的value
     * 
     * @param key
     * @return
     */
    public boolean exists(final String key) {
        return jedisCluster.exists(key);
    }

    /**
     * 读取缓存
     * 
     * @param key
     * @return
     */
    public Object get(final String key) {
        return jedisCluster.get(key);
    }

    /**
     * 写入缓存
     * 
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, String value) {
        boolean result = false;
        try {
            jedisCluster.set(key, value);
            logger.debug("set key >" + key + "  value >" + value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 写入缓存并设置缓存有效期
     * 
     * @param key
     * @param value
     * @param expireTime
     *            单位是秒
     * @return
     */
    public boolean set(final String key, String value, int expireTime) {
        boolean result = false;
        try {
            jedisCluster.setex(key, expireTime, value);
            logger.debug("set key >" + key + "  value >" + value + "  expireTime>" + expireTime);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}
/**
 * 
 * 类描述:redis缓存类
 * 
 * @author aiqi.gong 创建时间 2016年4月22日
 */
@Component
public final class RedisUtil {

    @Autowired(required=false)
    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);
    }

    /**
     * 读取缓存
     * 
     * @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
     * @param expireTime
     *            单位是秒
     * @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;
    }

}

源码地址http://download.csdn.net/detail/u011703657/9571075

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值