Getting started with Redis's Java usage

Source: Internet
Author: User
Tags delete key

Because of the project needs, recently learned the use of Redis

Redis installation in a server CentOS environment is relatively straightforward.

If you want to install on Windows, you can refer to someone else's article

http://blog.csdn.net/renfufei/article/details/38474435

Redis login, no password is required by default, port number is 6379 by default

Use visualizer Redis-desktop-manager to view data under Windows

Redis supports the following types of data structures as a cache database:

String (string), hash (hash), list, set (set), and Zset (sorted set: Ordered set)

The project uses Java as the development language, only need to introduce Jedis, commons-pool2 two jar package, if you need to add a spring-data-redis with spring

Jedis is the object in Java that is used to manipulate Redis

The simplest way to create:

Jedis jedis=New Jedis ("192.168.1.101");

Port number is used by default, no password authentication is used

Insert a string:

Jedis.set ("name", "Gary");

Execution results, you can see the insertion of a string gary,key to name

You can also set multiple key-value pairs at once:

Jedis.mset ("name", "Gary", "Age", "$", "email", "xxxxxx.163.com");

If it is an int value, you can also add 1 directly:

JEDIS.INCR ("Age");

Determine if key exists:

System.out.println (jedis.exists ("name")); System.out.println (Jedis.exists ("name2"));

To set the expiration time for a key:

Jedis.expire ("name", 5); Expired after//5s

Second parameter unit is second

Remove the value of key:

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

Delete key:

Jedis.del ("name");

Redis also has a feature for publishing subscriptions that can implement Message Queuing

Getting started with Redis's Java usage

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.