Java uses Redis database

Source: Internet
Author: User

Using Maven to manage jars, add dependencies:

<dependency>

<groupId>redis.clients</groupId>

<artifactId>jedis</artifactId>

<version>2.7.0</version>

</dependency>//the jar needed

Use the configuration of the Redis database (setting the value of the property, depending on your needs), to save the data

Jedispool jedispool = null;

Jedispoolconfig poolconfig = new Jedispoolconfig ();

Poolconfig.setmaxidle (10);

Poolconfig.setmaxtotal (100);

Poolconfig.setmaxwaitmillis (10000);

Poolconfig.settestonborrow (TRUE);

Jedispool = new Jedispool (poolconfig, "192.168.0.109", 6379); Configure Jedis configuration, port, server address

Stored Procedures

List<string> lines = Files.readalllines (Paths.get ("E:/oooooooo/task.txt"));//The data source to be stored, the data format is many JSON form string

Jedis Jedis = Jedispool.getresource ();

Jedis.select (3);//3 refers to the partition on the database, can be set freely

Pipeline pipelined = jedis.pipelined ();

Lines.foreach (L-, {

Pipelined.lpush (database name, L);

});

Pipelined.sync (); The purpose of this code is to bulk store to the Redis database through pipelines (Pipeline)

Get data on Redis via Java

Jedispoolconfig poolconfig = new Jedispoolconfig ();

Poolconfig.setmaxidle (10);

Poolconfig.setmaxtotal (500);

Poolconfig.setmaxwaitmillis (100000);

Poolconfig.settestonborrow (TRUE);

Jedispool = new Jedispool (poolconfig, server address, port, timeout, database password);

Jedis Jedis = Jedispool.getresource ();

Jedis.select (3);//3 refers to the partition on the database, can be set freely

String targetstring = jedis.lpop (database name);; /targetstring is the target data that was taken out.

Jedispool.returnresourceobject (Jedis); After a meal, remember to release Jedis resources.

Java uses Redis database

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.