Redis Learning (5)-jedis (Java Operations Redis database technology)

Source: Internet
Author: User
Tags connection pooling

Java Connection Redis

One, import jar package

What are the commands for Redis, Jedis? How to set up a firewall

Run the following code on Linux:

Single instance: Jedis instance:

 PackageCom.jedis.demo;Importorg.junit.Test;ImportRedis.clients.jedis.Jedis; Public classDemo1 {/** Single Instance connection to Redis database **/@Test Public voidrun () {//parameters: IP address, port numberJedis jedis=NewJedis ("192.168.239.137", 6379); Jedis.set ("Name", "Zhang San"); System.out.println (The value of "name" is: "+jedis.get (" name ")); }}

Jedis Connection Pool

/** Jedis Connection Pool **/@Test Public voidrun2 () {//1. Setting the connection pool configuration object        jedispoolconfig config=new jedispoolconfig (); //set the maximum number of connections in the pool "optional"        Config.setmaxtotal (); //set the maximum number of connections retained in the pool when idle "optional"        Config.setmaxidle (ten); //Setting up Connection Objects        jedispool pool=new jedispool (config, "192.168.239.137", 6379); //get the Connection object in the pool        Jedis jedis= Pool.getresource (); System.out.println (The value of "name" is: "+jedis.get (" name ")); //connect to the return pool jedis.close (); }

Extract Connection Pooling Tool

To facilitate the use of connection pooling, extract the connection pooling tool:

 PackageCom.jedis.demo;ImportRedis.clients.jedis.Jedis;ImportRedis.clients.jedis.JedisPool;ImportRedis.clients.jedis.JedisPoolConfig; Public classJedisutill {//Define a Connection pool object ()    Private Final StaticJedispool POOL; //initializing a connection pool object inside a static block of code    Static {        //1. Setting the connection pool configuration objectJedispoolconfig config=NewJedispoolconfig (); //set the maximum number of connections in the pool "optional"Config.setmaxtotal (50); //set the maximum number of connections retained in the pool when idle "optional"Config.setmaxidle (10); //Setting up Connection ObjectsPool=NewJedispool (config, "192.168.239.137", 6379); }        /** Get connections from the pool **/     Public StaticJedis Getjedis () {returnPool.getresource (); }}

Redis Learning (5)-jedis (Java Operations Redis database technology)

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.