Java中Jedis操作Redis與Spring的整合

來源:互聯網
上載者:User

標籤:sla   generated   todo   isp   rate   remove   lis   pop   操作方法   

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調用其中具體的方法進行操作

Java中Jedis操作Redis與Spring的整合

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.