Spring Integrated Redis

Source: Internet
Author: User

1. environment

Need spring.jar, Common-pool.jar.jedisjar

2. Introduction

Sharding (shardedjedis) distributes different keys to different Redis servers for horizontal scaling Purposes.

3. Configure Application.xml

<!--loading Redis profiles--
<location="classpath:redis.properties"/>
<!--connection Pool configuration--
<BeanID= "poolcfg"class= "redis.clients.jedis.JedisPoolConfig"> < propertyname= "maxtotal"value= "${redis.pool.maxactive}" /> < propertyname= "maxidle"value= "${redis.pool.maxidle}" /> < propertyname= "minidle"value= "1"/> < propertyname= "maxwaitmillis"value= "${redis.pool.maxwait}" /> < propertyname= "testonborrow"value= "true"/> < propertyname= "testonreturn"value= "true"/> </Bean>
<!--port Remember to specify type int--<BeanID= "shardedjedispool"class= "redis.clients.jedis.ShardedJedisPool"> <Constructor-argIndex= "0"ref= "poolcfg"></Constructor-arg> <Constructor-argIndex= "1"> <List> <Beanname= "master"class= "redis.clients.jedis.JedisShardInfo"> <Constructor-argIndex= "0"value= "${redis1.ip}"></Constructor-arg> <Constructor-argIndex= "1"value= "${redis1.port}"type= "int"></Constructor-arg> < propertyname= "password"value= "${redis2.auth}"/> </Bean> <Beanname= "slaver"class= "redis.clients.jedis.JedisShardInfo"> <Constructor-argIndex= "0"value= "${redis2.ip}"></Constructor-arg> <Constructor-argIndex= "1"value= "${redis2.port}"type= "int"></Constructor-arg> < propertyname= "password"value= "${redis2.auth}"/> </Bean> </List> </Constructor-arg></Bean>

4. Get resources, Release resources

@Repository ("jedisdataresource") public classJedisdataresourceimplImplementsJedisdataresource{@Resource (name= "shardedjedispool")    PrivateShardedjedispool sharededjedispool; @Override publicShardedjedis getresource () {shardedjedis Shardedjedis=NULL; Try{shardedjedis=Sharededjedispool.getresource (); returnshardedjedis; } Catch(Exception E) {if(NULL!=Sharededjedispool) Sharededjedispool.close (); }        return NULL; } @Override public voidReturnresource (shardedjedis Shardedjedis) {if(shardedjedis!=NULL) Shardedjedis.close (); }    }

5.dao Layer

 public Interface Userredisdao {        /** Get user name *    /double  getusername (String Key) ; }

@Repository ("userredisdao") public classUserredisdaoimplImplementsUserredisdao{@ResourcePrivateJedisdataresource jedisdataresource;@Override public Stringgetusername (String key) {shardedjedis Shardedjedis=Jedisdataresource.getresource (); if(NULL==Shardedjedis)returnNull; Try {            returnShardedjedis.get (key);        } Catch(Exception e) {e.printstacktrace ();        } finally{jedisdataresource.returnresource (shardedjedis); }    }
}

6.service

 public Interface commodityredisservice {        string getusername (string key);    }
@Service ("userredisservice")publicclassimplements  userredisservice{                @Autowired         Private Userredisdao userredisdao;        @Override        public  string increasecollectnum (string key) {            return userredisdao.getusername (key);}        }

Spring Integrated Redis

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.