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


  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

    希望大神提一些意見~~,我來改進改進...


免責聲明!

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



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