• 对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

        希望大神提一些意见~~,我来改进改进...

  • 相关阅读:
    项目实战【vue,react,微信小程序】(1708E)
    java——集合——List集合——List集合
    java——集合——List集合——LinkedList集合
    Java——集合——泛型——泛型通配符
    Java——集合——泛型——定义和使用含有泛型的接口
    java——集合——List集合——ArrayList集合
    工作效率
    js中__proto__和prototype的区别和关系?
    KubeEdge架构问题汇总
    整数分解
  • 原文地址:https://www.cnblogs.com/qiangweikang/p/reidis-client.html
Copyright © 2020-2023  润新知