Java-no.13 based on Redis distributed cache implementation

Source: Internet
Author: User
Tags throwable

1. Redis implements distributed cache

package com.shma.redis;import java.util.list;import  Org.apache.commons.pool2.impl.genericobjectpoolconfig;import redis.clients.jedis.jedisshardinfo;import  redis.clients.jedis.shardedjedis;import redis.clients.jedis.shardedjedispool;public class  RedisService {private GenericObjectPoolConfig jedisPoolConfig;private List< Jedisshardinfo> jedisshardinfos;private shardedjedispool sharejedispool;public void  init ()  {sharejedispool =new shardedjedispool (Jedispoolconfig, jedisshardinfos);} Public shardedjedis getsharejedispoolconnection ()  {ShardedJedis shardedJedis =  Sharejedispool.getresource (); Return shardedjedis;} Public genericobjectpoolconfig getjedispoolconfig ()  {return jedispoolconfig;} Public void setjedispoolconfig (Genericobjectpoolconfig jedispoolconfig)  { This.jedispoolconfig = jedispoolconfig;} PUblic list<jedisshardinfo> getjedisshardinfos ()  {return jedisshardinfos;} Public void setjedisshardinfos (List<jedisshardinfo> jedisshardinfos)  { This.jedisshardinfos = jedisshardinfos;} Public shardedjedispool getsharejedispool ()  {return sharejedispool;} Public void setsharejedispool (Shardedjedispool sharejedispool)  {this.shareJedisPool =  sharejedispool;}}
Package com.shma.redis;import redis.clients.jedis.shardedjedis;public class rediscache  {private redisservice redisservice;public void set (String key, String  value)  {ShardedJedis shardedJedis = null;try {shardedJedis =  Redisservice.getsharejedispoolconnection (); Shardedjedis.set (Key, value);}  catch  (throwable e)  {e.printstacktrace ();}  finally {shardedjedis.close ();}} Public string get (String key)  {shardedjedis shardedjedis = null;try { Shardedjedis = redisservice.getsharejedispoolconnection (); Return shardedjedis.get (key);}  catch  (throwable e)  {e.printstacktrace ();}  finally {shardedjedis.close ();} Return null;} Public boolean del (String key)  {shardedjedis shardedjedis = null;try { Shardedjedis = redisservice.getsharejedispoolconNection (); Return shardedjedis.del (key)  > 0 ? true : false;}  catch  (throwable e)  {e.printstacktrace ();}  finally {shardedjedis.close ();} Return false;} Public redisservice getredisservice ()  {return redisservice;} Public void setredisservice (Redisservice redisservice)  {this.redisService =  Redisservice;}}

    spring-application.xml configuration file

<?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:aop="/HTTP/ Www.springframework.org/schema/aop "xsi:schemalocation=" http://www.springframework.org/schema/beans      http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp:// www.springframework.org/schema/aop     http://www.springframework.org/schema/aop/ Spring-aop-3.0.xsd "xmlns:p=" http://www.springframework.org/schema/p "><bean id=" Propertyconfigurer "  class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" ><property name = "Location" >    <value>config/redis.properties</value></property>< /bean><bean id= "Jedispoolconfig"  class= "Org.apache.commons.pool2.impl.GenericObjectPoolConfig" ><property name= "Maxtotal"  value= "${redis.mAxtotal} "></property><property name=" Maxidle " value=" ${redis.maxidle} "></property ><property name= "Minidle"  value= "${redis.minidle}" ></property><property name= "Maxwaitmillis"  value= "${redis.maxwaitmillis}" ></property><property name= "TestOnBorrow"  value= "${redis.testonborrow}" ></property></bean><bean id= "JedisShardInfo01"   class= "Redis.clients.jedis.JedisShardInfo" ><constructor-arg value= "${redis.host.shard01}" ></ Constructor-arg><constructor-arg value= "${REDIS.PORT.SHARD01}" ></constructor-arg>< Constructor-arg value= "${redis.timeout}" ></constructor-arg></bean><bean id= " JedisShardInfo02 " class=" Redis.clients.jedis.JedisShardInfo "><constructor-arg value=" ${ REDIS.HOST.SHARD02} "></constructor-arg><constructor-arg value=" ${redis.port.shard02} ">< /constructor-arg><construCtor-arg value= "${redis.timeout}" ></constructor-arg></bean><bean id= "RedisService"  class= "Com.shma.redis.RedisService"  init-method= "init" ><property name= "Jedispoolconfig" ><ref bean= "Jedispoolconfig"/></property><property name= "JedisShardInfos" >< List><ref bean= "jedisShardInfo01"/><ref bean= "jedisShardInfo02"/></list></ Property></bean><bean id= "Rediscache"  class= "Com.shma.redis.RedisCache" ><property  name= "Redisservice" ><ref bean= "Redisservice"/></property></bean></beans>

Redis.properties configuration file

#客户端超时时间单位是毫秒redis. timeout=10000# maximum number of connections redis.maxtotal=5000# minimum idle number redis.minidle=100# maximum idle number redis.maxidle=5000# Maximum Setup connection wait time redis.maxwaitmillis=5000redis.testonborrow=falseredis.host.shard01=183.131.6.62redis.port.shard01= 6379redis.host.shard02=127.0.0.1redis.port.shard02=6379

2, found in the Redis.clients.jedis source code, implementation of distributed if there is a server rock machine, it will lead to the entire distribution can not be used

Modify the Redis.clients.jedis. Jedisshardinfo class

@Override public Jedis Createresource () {Jedis Jedis = new Jedis (this);    try {if ("Pong". Equals (Jedis.ping ())) {jedis.select (db);        return Jedis;    }} catch (Throwable e) {System.out.println ("Connection exception = +" + gethost () + ":" + getport () + ":" + db);  } return null; }

Modifying the Redis.clients.util.Sharded class

Private void initialize (list<s> shards)  {    nodes =  New treemap<long, s> ();    for  (int i = 0; i  != shards.size ();  ++i)  {        final s  shardinfo = shards.get (i);         r r =  shardinfo.createresource ();        //  If a redis failure is created, Automatically rejects the Rock machine server         if  (r != null)  {             if  (Shardinfo.getname ()  == null)         for  (int n = 0; n < 160  * shardinfo.getweight ();  n++)  {    nodes.put (This.algo.hash ("SHARD-")  + i +  "-node-" &NBSP;+&Nbsp;n),  shardinfo);        }    else             for  (int n = 0; n  < 160 * shardinfo.getweight ();  n++)  {    nodes.put ( This.algo.hash (Shardinfo.getname ()  +  "*"  + shardinfo.getweight ()  + n),  Shardinfo);         }    resources.put (ShardInfo, &NBSP;R);         }    }}

Select DB is not supported in the default Redis distributed implementation, you can also modify the constructor, pass in the DB, and distribute in multiple db of a Redis

Java-no.13 based on Redis distributed cache implementation

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.