New method for writing redisconfiguration classes in Spring Data Redis 2.x

Source: Internet
Author: User
Tags serialization

In Spring Data Redis 1.x , we might write such a redisconfig class in the project:

@Configuration @enablecaching Public classRedisconfig {@SuppressWarnings ({"Rawtypes", "unchecked"}) @Bean (name= "Redistemplate")     Publicredistemplate initredistemplate () {jedispoolconfig poolconfig=NewJedispoolconfig (); //Maximum idle numberPoolconfig.setmaxidle (50); //Maximum number of connectionsPoolconfig.setmaxtotal (100); //maximum number of milliseconds to waitPoolconfig.setmaxwaitmillis (20000); //Create a Jedis connection factoryJedisconnectionfactory ConnectionFactory =Newjedisconnectionfactory (poolconfig); Connectionfactory.sethostname ("LocalHost"); Connectionfactory.setport (6379); //after invoking the Initialize method, no it throws an exceptionConnectionfactory.afterpropertiesset (); //Customizing the Redis serializerRedisserializer Jdkserializationredisserializer =NewJdkserializationredisserializer (); Redisserializer Stringredisserializer=NewStringredisserializer (); //define Redistemplate, and set up connection engineeringRedistemplate redistemplate =Newredistemplate ();        Redistemplate.setconnectionfactory (ConnectionFactory); //setting up the serializerRedistemplate.setkeyserializer (Stringredisserializer);        Redistemplate.setvalueserializer (Jdkserializationredisserializer);        Redistemplate.sethashkeyserializer (Stringredisserializer);        Redistemplate.sethashvalueserializer (Jdkserializationredisserializer); returnredistemplate; } @SuppressWarnings ("Rawtypes") @Bean (name= "Rediscachemanager")     PublicCacheManager Initrediscachemanager (@Autowired redistemplate redistemplate) {Rediscachemanager CacheManager 
    =NewRediscachemanager (redistemplate); //set the time-out to 10 minutes in secondsCachemanager.setdefaultexpiration (600); //Set the cache nameList<string> Cachenames =NewArraylist<>(); Cachenames.add ("Rediscachemanager");        Cachemanager.setcachenames (Cachenames); returnCacheManager; }    }

However, after spring Data Redis 2.x, we continue to write the following error may be reported:

Visible 2.x After these common configuration Class API has been a certain degree of change, In the discovery of this problem, I once wanted to find a new 2.x by degrees Niang, but did not search too good solution, so he switched to Google, found that there is no too intact solution, finally had to pass through some of the scattered points before, as well as their own view of the new source, groped to get The following are possible new formulations :

/*** Redis configuration. * *@authorChenxinyu*/@Configuration @enablecaching Public classRedisconfig {/*** Obtain the host address of Redis from APPLICATION.YML. */@Value ("${spring.redis.host}")    PrivateString Redishost; /*** Obtain the Redis port number from APPLICATION.YML. */@Value ("${spring.redis.port}")    PrivateInteger Redisport; /*** Jedis Connection factory. *     * @returnwell-configured Jedis connection factory*/@Bean Publicjedisconnectionfactory jedisconnectionfactory () {redisstandaloneconfiguration configuration=Newredisstandaloneconfiguration (Redishost, Redisport); return Newjedisconnectionfactory (configuration); } @Bean (Name= "Redistemplate")     PublicRedistemplate<object, object>redistemplate (Redisconnectionfactory factory) {/** Redis Serializer. * * Redistemplate The default serialization class is Jdkserializationredisserializer, serialized with Jdkserializationredisserializer, * serialized pairs The serializable interface must be implemented as such.         When storing content, other content is stored in addition to the contents of the property, the total length is long, and it is not easy to read. * * Jackson2jsonredisserializer and Genericjackson2jsonredisserializer, both can be serialized into JSON, * but the latter will be added to the JSON @cla The SS attribute, the full path package name of the class, facilitates deserialization. The former if the List is stored in the anti-serialization if not specified * TypeReference will be an error java.util.LinkedHashMap cannot is cast to*/Redisserializer Genericjackson2jsonredisserializer=NewGenericjackson2jsonredisserializer (); Redisserializer Stringredisserializer=NewStringredisserializer (); //define Redistemplate, and set up connection engineeringRedistemplate<object, object> redistemplate =NewRedistemplate<>(); //the serialization of key uses StringredisserializerRedistemplate.setkeyserializer (Stringredisserializer);        Redistemplate.sethashkeyserializer (Stringredisserializer); //the serialization of value values takes Genericjackson2jsonredisserializerRedistemplate.setvalueserializer (Genericjackson2jsonredisserializer);        Redistemplate.sethashvalueserializer (Genericjackson2jsonredisserializer); //setting up a connection factoryredistemplate.setconnectionfactory (Factory); returnredistemplate; } @Bean PublicCacheManager Initrediscachemanager (Redisconnectionfactory factory) {Rediscachemanager.rediscachemanagerbuilde R Builder=Rediscachemanager.        Rediscachemanagerbuilder.fromconnectionfactory (Factory); returnBuilder.build (); }}

This method is available by the author's test.

Of course there's more than one way to look at the new ' CacheManager ' API and find several other ways to do it.

New method for writing redisconfiguration classes in Spring Data Redis 2.x

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.