Simple use of Redis-based memory databases

Source: Internet
Author: User

Customers using in-memory data in ecplise , if ready to download two jar Packages

Commons-pool2-2.0.jar

Jedis-2.4.2.jar

The premise is ready, so let's start the Redis service , Open a command window and enter the following command:redis-server or Redis-server redis root mesh \redis.conf

The server is turned on, note that the port number is 6377.

2. Create a project in Eclipse , import the package that redist needs into the project

3. write a Jedis Tool class

Public class Jedisutil {

Private  Static            String HOST="127.0.0.1"; //Native address

Private  Static                   Integer PORT= 6379; //Service Port

Private  Static              Jedispoolconfig config; //Connection pool Configuration object

Private  Static                     Jedispool Pool; //Connection pool object

Static {

config = new jedispoolconfig ();

config. Setmaxidle (1024*10); //Set memory size

Pool = new jedispool (config,HOST);

}

/**

*

 *  @return get a Jedis object from the connection pool

*/

Public  Static Jedis Getpooljedis () {

return pool. getresource ();

}

/**

* Jedis objects are manually played back into the connection pool

*/

Public Static void Returnpooljedis (Jedis Jedis) {

pool. Returnresource (Jedis);

}

/**

 *  @return Create a Jedis connection directly

*/

Public Static Jedis Getjedis () {

return New Jedis (HOST, PORT);

}

}

4. Write a client class operation Jedis

Public class Client {

Public Static void Main (string[] args) {

Simpleset ();

MSet ();

}

Private Static void MSet () {

Note If you use Jedisutil.getjedis (); is to create a Jredis object directly, so it is not managed by the connection pool, so it cannot be played back into the connection pool.

Jedis Jedis = Jedisutil.getpooljedis ();

Setting values for multiple values

Jedis.mset ("UserName","user1","pwd","123");

Take value

list<string> list = Jedis.mget ("UserName","pwd");

for (String string:list) {

System. out. println (string);

}

Jedis.flushdb ();

Jedisutil.returnpooljedis (Jedis);

}

Private Static void Simpleset () {

Jedis Jedis = Jedisutil.getpooljedis ();

Setting the value

Jedis.set ("UserName", "user1");

Take value

System. out. println ("userName:"+jedis.get ("userName"));

Emptying the in-memory database

Jedis.flushdb ();

Jedisutil.returnpooljedis (Jedis);

}

}

Data can now be evaluated on the redist client

Open a Command window, enter the command redis-cli Open the client , after the original assignment

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.