Java uses the redis database, javaredis Database

Source: Internet
Author: User

Java uses the redis database, javaredis Database

Use maven to manage jar and add dependency:

<Dependency>

<GroupId> redis. clients </groupId>

<ArtifactId> jedis </artifactId>

<Version> 2.7.0 </version>

</Dependency> // The required jar

Use the redis database configuration (set the attribute value as needed) to store 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 the Jedis configuration, port, and server address.

Stored Procedure

List <String> lines = Files. readAllLines (Paths. get ("E:/oooooooo/task.txt"); // the data source to be stored. The data format is mostly a json String.

Jedis jedis = jedisPool. getResource ();

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

Pipeline pipelined = jedis. pipelined ();

Lines. forEach (l-> {

Pipelined. lpush (Database Name, l );

});

Pipelined. sync (); // This code is used to store data in redis databases in batches through pipelines.

Obtain redis data through 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, which can be set freely.

String targetString = jedis. lpop (Database Name); // targetString is the retrieved target data.

JedisPool. returnResourceObject (jedis); // after an operation, remember to release jedis resources.

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.