Further encapsulation of the Redis client jedis2.8.0

Source: Internet
Author: User

Further encapsulation of the jedis2.8.0:

1. Serialization of storage objects

2. Create a Redis connection pool with spring

3. Provides a basic single entity operation, an ordered list operation, and a one-to-many relationship list, which provides a paging package for list

4. Encapsulates simple logic (e.g., rebuilding cached scenes, sorting rules, specific methods need to rewrite ~)

When you use it, just inherit the class (icachet,icachtlist,icacherelated) that matches your business, and rewrite the ordering, the exact data you need to rebuild, and so on.

(1). Single Cache (Icachet)

public class Cacheperson extends cachetbase<integer,person> {    @Override    protected person rebuild ( Integer TKey) {        return null;    }}

When used:

Person P  = new Person ();    Cacheperson.deletebykey (person.class,1,2);    Cacheperson.get (Person.class, 1);    Cacheperson.delete (P); Cacheperson.set (P);

(2). List cache (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 ();    } }

When used:

Batch list Add        list.addtiolist (citylist);        One or more add        list.addtolist (city1,city2);        Delete List.removefromlistbykey via key        (City.class, "1", "2", "3", "4", "5");        One or more delete        list.removefromlist (city1,city2);        Bulk list Delete        list.removefromlist (citylist);        Find List        list<city> citylist = List.getlist (City.class, true);        Find the List        list<city> citylist = list.getlistpage (City.class, page, true) with pagination;        Empty list        list.clearlist ();

(3). One-to-many relationship (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;}    }

When used:

     Add        list.addrelated (person, "Qiang");        Bulk List Add method        list.addrelateds ("Qiang", personlist);        Add one or more        list.addrelateds ("Qiang", P1,P2,P3,P4,P5);        Bulk Delete Relationship        list.removerelated ("Qiang", personlist);        Delete one or more relationships        list.removerelated ("Qiang", P1,P2,P3,P4,P5);        Delete List.removefromlistbykey according to ID        ("Qiang", Person.class, "2", "3");        Get List,desc=true to reverse (score larger, sort more forward ~)        list.getrelatedlist ("Qiang", desc)        //Get list,desc=true with pagination in reverse order        list.getrelatedlistpage ("Qiang", page, desc);        Empty        list.clearlist ("Qiang");

Spring Fixed configuration:

1. Inject:

<?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 "> <!--Serialization--<bean id=" Iserializer "class=" Com.cjdz.test.cache.redis.JsonSerializer "></bean> <!--Cache basic operation-<bean id=" re Discache "class=" Com.cjdz.test.cache.redis.RedisCache "> <property name=" Iserializer "ref=" Iserializer "&GT;&L        t;/property> </bean> <bean id= "cache" class= "Com.cjdz.test.cache.redis.RedisCache" ></bean> <!--single--<bean id= "Icachet" class= "Com.cjdz.test.catchdata.impl.CacheTBase" abstract= "true" > &L T;property name= "Cache" ref= "cache" ></properTy> </bean> <!--list--> <bean id= "icachelist" class= "Com.cjdz.test.catchdata.impl.CacheListBas E "abstract=" true "> <property name=" Cache "ref=" cache "></property> </bean> <!--one-to-many-- > <bean id= "icacherelated" class= "Com.cjdz.test.catchdata.impl.CacheRelatedBase" abstract= "true" > <p Roperty name= "Cache" ref= "cache" ></property> </bean> <!--business--<bean id= "Cachecity" Clas s= "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. Connection pool:

<?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> <!--load Configuration Properties File--<context:property-placeholder ignore-unresolvable= "true" location= "classpath: Redis.properties "/> <bean id=" jedispoolconfig "class=" Redis.clients.jedis.JedisPoolConfig "> &LT;PR  Operty name= "Maxidle" value= "/> <!--maximum number of objects capable of maintaining Idel status--<property name=" maxtotal "value=" 60000 " /> <!--The maximum number of objects allocated--<property name= "TestonborRow "value=" true "/> <!--when the borrow object method is called, does the validation check-</bean> <bean id=" Jedispool "class = "Redis.clients.jedis.JedisPool" > <constructor-arg index= "0" ref= "jedispoolconfig"/> <construct        Or-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>

The last thing you see in redisdesktop is this type of drop:

This is casually write a small east, similar to the helper bar, when used, can not only standardize the development of the operation of the cache, not the final cache is not messy, but also easy to maintain the cache.

There are many limitations, when writing, when objects are stored in Redis, they are used directly as key (object class name: ID), and list and related keys are given to the developer for rewriting. Then slowly improve it:)

File Address: Http://files.cnblogs.com/files/qiangweikang/reids-client.rar

Further encapsulation of the Redis client jedis2.8.0

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.