Using Jedis to connect to Redis

Source: Internet
Author: User

Keyword: Redis jedis multithreading

Using Jedis as a client connection Redis,jedis encapsulates a number of operations on Reids, which makes it easy to access Redis

1  <Dependency>2         <groupId>Redis.clients</groupId>3         <Artifactid>Jedis</Artifactid>4         <version>2.8.1</version>5     </Dependency>

A Redis instance is installed in the native virtual machine and the IP address is 192.168.1.110, the base object for connecting Reids in Port 6379,jedis is Jedis, creating a simple Jedis object

1 New Jedis ("192.168.1.110", 6379);

Then you can use the basic additions and deletions to the operation, with a string type of key value pairs as an example

1  Public Staticstring getString (String key) {2String result =Jedis.get (key);3     returnresult;4 }5 6  Public Static voidputstring (String key, String valuestr) {7Rowkey =key;8 Jedis.set (ROWKEY,VALUESTR);9 }Ten  One  Public Static Longdelstring (String key) { ARowkey =key; -     returnJedis.del (Rowkey);//Success 1, fail 0 -}

It is worth noting that the Jedis object is thread insecure, so in the case of concurrency to use Jedispool, Jedispool only supports 8 connections by default, so the maximum number of connections to Jedispool is first modified when declaring Jedispool

1  New jedispoolconfig (); 2 // To modify the maximum number of connections 3  Config.setmaxtotal (a); 4 // declaring a pool of threads 5  New Jedispool (config, "192.168.1.102", 6379); 6 7 // get Jedis Object 8 Jedis Tjedis = Pool.getresource ();

Using Jedis to connect to 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.