對redis用戶端jedis2.8.0的進一步封裝

來源:互聯網
上載者:User

標籤:

 jedis2.8.0的進一步封裝:

    1.序列化儲存物件

    2.結合spring,建立redis串連池

    3.提供了基礎的單個實體操作,有序list操作和一對多關聯性list的操作,對list提供了分頁的封裝

    4.封裝了簡單的邏輯(如:重建緩衝的情境,定序,具體方法需要重寫~)

 具體使用的時候,只需要繼承符合你的業務的類(ICacheT,ICachtList,ICacheRelated),並重寫下排序,重建時需要的具體資料等方法就可以啦

  (1).單個緩衝(ICacheT)

public class CachePerson extends CacheTBase<Integer,Person> {    @Override    protected Person rebuild(Integer tkey) {        return null;    }}

 

 使用時:

    Person p  = new Person();    cachePerson.deleteByKey(Person.class,1,2);    cachePerson.get(Person.class, 1);    cachePerson.delete(p);    cachePerson.set(p);

 

  (2).list緩衝(ICachtList)

public class CacheCityList extends CacheListBase<City>{    @Override    protected String getKey() {        return "common:city";    }    @Override    protected List<City> rebuildList() {        return null;    }    @Override    protected List<City> rebuildPageList(Page page) {        return null;    }    @Override    protected Double score(City item) {        return (double)item.getId();    }}

 

  使用時:

    //批量list添加        list.addTioList(cityList);        //一個或多個添加        list.addToList(city1,city2);        //通過key刪除        list.removeFromListByKey(City.class,"1","2","3","4","5");        //一個或多個刪除        list.removeFromList(city1,city2);        //批量list刪除        list.removeFromList(cityList);        //尋找list        List<City> cityList = list.getList(City.class, true);        //尋找帶分頁的list        List<City> cityList = list.getListPage(City.class, page, true);        //清空list        list.clearList();

 

  (3).一對多關聯性(ICacheRelated)

public class CacheUserPersonList extends CacheRelatedBase<Person>{    @Override    protected String getKey(String mainkey) {        return null;    }    @Override    protected List<Person> rebuildList(String mainkey) {        return null;    }    @Override    protected List<Person> rebuildPageList(String mainkey, Page page) {        return null;    }    @Override    protected double score(Person item) {        return 0;    }}

 

  使用時:

     //添加        list.addRelated(person, "qiang");        //批量list添加方法        list.addRelateds("qiang", personList);        //添加一個或多個        list.addRelateds("qiang",p1,p2,p3,p4,p5);        //大量刪除關係        list.removeRelated("qiang", personList);        //刪除一個或多個關係        list.removeRelated("qiang", p1,p2,p3,p4,p5);        //根據id刪除        list.removeFromListByKey("qiang", Person.class, "2","3");        //擷取list,desc=true 為倒序(score 越大,排序越靠前~)        list.getRelatedList("qiang", desc)        //擷取帶分頁的list,desc=true 為倒序        list.getRelatedListPage("qiang", page, desc);        //清空        list.clearList("qiang");

 

 

spring固定配置:

1.注入:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans.xsd">            <!--序列化 -->    <bean id="iserializer" class="com.cjdz.test.cache.redis.JsonSerializer"></bean>    <!--緩衝基本操作 -->    <bean id="redisCache" class="com.cjdz.test.cache.redis.RedisCache">        <property name="iserializer" ref="iserializer"></property>    </bean>    <bean id="cache" class="com.cjdz.test.cache.redis.RedisCache"></bean>        <!-- 單個-->    <bean id="icacheT" class="com.cjdz.test.catchdata.impl.CacheTBase" abstract="true">        <property name="cache" ref="cache"></property>    </bean>    <!-- list-->    <bean id="icacheList" class="com.cjdz.test.catchdata.impl.CacheListBase" abstract="true">        <property name="cache" ref="cache"></property>    </bean>    <!-- 一對多-->    <bean id="icacheRelated" class="com.cjdz.test.catchdata.impl.CacheRelatedBase" abstract="true">        <property name="cache" ref="cache"></property>    </bean>    <!-- 業務-->    <bean id="cacheCity" class="com.cjdz.test.catchdata.test.CacheCity" parent="icacheT"></bean>    <bean id="cachePerson" class="com.cjdz.test.catchdata.test.CachePerson" parent="icacheT"></bean>    <bean id="cacheCityList" class="com.cjdz.test.catchdata.test.CacheCityList" parent="icacheList"></bean>    <bean id="cacheUserPersonList" class="com.cjdz.test.catchdata.test.CacheUserPersonList" parent="icacheRelated"></bean></beans>

 

2.串連池:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd        http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.2.xsd"    default-lazy-init="true">    <description>Jedis Configuration</description>    <!-- 載入配置屬性檔案 -->    <context:property-placeholder ignore-unresolvable="true" location="classpath:redis.properties" />        <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">        <property name="maxIdle" value="300" /> <!-- 最大能夠保持idel狀態的對象數  -->        <property name="maxTotal" value="60000" /> <!-- 最大分配的對象數 -->        <property name="testOnBorrow" value="true" /> <!-- 當調用borrow Object方法時,是否進行有效性檢查 -->    </bean>        <bean id="jedisPool" class="redis.clients.jedis.JedisPool">        <constructor-arg index="0" ref="jedisPoolConfig" />        <constructor-arg index="1" value="${redis.host}" />        <constructor-arg index="2" value="${redis.port}" type="int" />        <constructor-arg index="3" value="${redis.timeout}"  type="int" />        <constructor-arg index="4" value="${redis.auth}"/>    </bean></beans>

 

最後在RedisDesktop看到的大概是這樣式滴:

 

 這個是隨便寫的一個小東東,類似於helper吧,在使用時,既能規範開發時對緩衝的操作,不至於最後緩衝都亂掉,也能方便維護緩衝。

 有很多的局限性,寫的時候,對象在redis中儲存時,直接用了(對象類名:id) 的方式作為key,而list和related的key則交給開發人員重寫。之後慢慢改進吧  :)

  檔案地址:http://files.cnblogs.com/files/qiangweikang/reids-client.rar

對redis用戶端jedis2.8.0的進一步封裝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.