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="192.168.242.135"></constructor-arg>
        <constructor-arg name="port" value="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="192.168.242.135"></constructor-arg>
                    <constructor-arg name="port" value="7001"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="192.168.242.135"></constructor-arg>
                    <constructor-arg name="port" value="7002"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="192.168.242.135"></constructor-arg>
                    <constructor-arg name="port" value="7003"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="192.168.242.135"></constructor-arg>
                    <constructor-arg name="port" value="7004"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="192.168.242.135"></constructor-arg>
                    <constructor-arg name="port" value="7005"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="192.168.242.135"></constructor-arg>
                    <constructor-arg name="port" value="7006"></constructor-arg>
                </bean>
            </set>
        </constructor-arg>
    </bean>
    <bean id="jedisClientCluster" class="com.*.*.JedisClientCluster"></bean>
</beans>

完成以上步驟,使用JedisClientSingle、JedisClientCluster調用其中具體的方法進行操作


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM