首页 > 代码库 > Java中Jedis操作Redis与Spring的整合

Java中Jedis操作Redis与Spring的整合

Redis是一个key-value存储系统。它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)和zset(有序集合)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。为了保证效率,数据都是缓存在内存中。redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。以下是Jedis操作Redis与Spring的整合的单机版和集群版:

首先准备单击版和集群版的操作方法:JedisClientSingle、JedisClientCluster

import org.springframework.beans.factory.annotation.Autowired;

import com.taotao.rest.dao.JedisClient;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;


public class JedisClientSingle {

    @Autowired
    private JedisPool jedisPool;
    
    public String get(String key) {//获取指定 key 的值。如果 key 不存在,返回 nil 
        Jedis jedis = jedisPool.getResource();
        String string = jedis.get(key);
        jedis.close();
        return string;
    }

    public String set(String key, String value) {//设置一些字符串值
        Jedis jedis = jedisPool.getResource();
        String string = jedis.set(key, value);
        jedis.close();
        return string;
    }

    public String hget(String hkey, String key) {//获取哈希表中指定字段的值
        Jedis jedis = jedisPool.getResource();
        String string = jedis.hget(hkey, key);
        jedis.close();
        return string;
    }

    public long hset(String hkey, String key, String value) {//为哈希表中的字段赋值
        Jedis jedis = jedisPool.getResource();
        long result = jedis.hset(hkey, key, value);
        jedis.close();
        return result;
    }


    public long incr(String key) {//将 key 中储存的数字值增一,如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行INCR操作
        Jedis jedis = jedisPool.getResource();
        long result = jedis.incr(key);
        jedis.close();
        return result;
    }

    public long expire(String key, int second) {//设置key的到期时间
        Jedis jedis = jedisPool.getResource();
        long result = jedis.expire(key, second);
        jedis.close();
        return result;
    }

    public long ttl(String key) {//以秒为单位返回 key 的剩余过期时间
        Jedis jedis = jedisPool.getResource();
        long result = jedis.ttl(key);
        jedis.close();
        return result;
    }

    public long del(String key) {//根据key删除
        Jedis jedis = jedisPool.getResource();
        long result = jedis.del(key);
        jedis.close();
        return result;
    }

    public long hdel(String hkey, String key) {//删除哈希表key中的一个或多个指定字段
        Jedis jedis = jedisPool.getResource();
        long result = jedis.hdel(hkey, key);
        jedis.close();
        return result;
    }

}
import org.springframework.beans.factory.annotation.Autowired;

import com.taotao.rest.dao.JedisClient;

import redis.clients.jedis.JedisCluster;

public class JedisClientCluster {
    
    @Autowired
    private JedisCluster jedisCluster;
    
    public String get(String key) {
        return jedisCluster.get(key);
    }

    public String set(String key, String value) {
        // TODO Auto-generated method stub
        return jedisCluster.set(key, value);
    }

    public String hget(String hkey, String key) {
        // TODO Auto-generated method stub
        return jedisCluster.hget(hkey, key);
    }

    public long hset(String hkey, String key, String value) {
        // TODO Auto-generated method stub
        return jedisCluster.hset(hkey, key, value);
    }

    public long incr(String key) {
        // TODO Auto-generated method stub
        return jedisCluster.incr(key);
    }

    public long expire(String key, int second) {
        // TODO Auto-generated method stub
        return jedisCluster.expire(key, second);
    }

    public long ttl(String key) {
        // TODO Auto-generated method stub
        return jedisCluster.ttl(key);
    }

    public long del(String key) {
        // TODO Auto-generated method stub
        return jedisCluster.del(key);
    }

    public long hdel(String hkey, String key) {
        // TODO Auto-generated method stub
        return jedisCluster.hdel(hkey, key);
    }

配置文件:applicationContext-jedis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    <!-- jedis单机版 -->
    <bean id="redisClient" class="redis.clients.jedis.JedisPool">
        <constructor-arg name="host" value="http://www.mamicode.com/192.168.242.135"></constructor-arg>
        <constructor-arg name="port" value="http://www.mamicode.com/6379"></constructor-arg>
    </bean> 
    <bean id="jedisClient" class="com.*.*.JedisClientSingle"></bean> 
    
    
    
    <!-- jedis集群版 -->
    <bean id="redisClient" class="redis.clients.jedis.JedisCluster">
        <constructor-arg name="nodes">
            <set>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="http://www.mamicode.com/192.168.242.135"></constructor-arg>
                    <constructor-arg name="port" value="http://www.mamicode.com/7001"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="http://www.mamicode.com/192.168.242.135"></constructor-arg>
                    <constructor-arg name="port" value="http://www.mamicode.com/7002"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="http://www.mamicode.com/192.168.242.135"></constructor-arg>
                    <constructor-arg name="port" value="http://www.mamicode.com/7003"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="http://www.mamicode.com/192.168.242.135"></constructor-arg>
                    <constructor-arg name="port" value="http://www.mamicode.com/7004"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="http://www.mamicode.com/192.168.242.135"></constructor-arg>
                    <constructor-arg name="port" value="http://www.mamicode.com/7005"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="http://www.mamicode.com/192.168.242.135"></constructor-arg>
                    <constructor-arg name="port" value="http://www.mamicode.com/7006"></constructor-arg>
                </bean>
            </set>
        </constructor-arg>
    </bean>
    <bean id="jedisClientCluster" class="com.*.*.JedisClientCluster"></bean>
</beans>

完成以上步骤,使用JedisClientSingle、JedisClientCluster调用其中具体的方法进行操作

Java中Jedis操作Redis与Spring的整合