Introduction to the basic operations of Redis in Springboot such as set, map, list, value, entity class, etc.

Source: Internet
Author: User
Tags redis
Today, I would like to introduce you how to use the basic operations such as set, map, list and value of Redis in Springboot .

In the previous section, we showed you how to build a Redis cache database in Springboot, a section that describes the specific operations of Redis.

The history of Redis, and the extent to which it is now, should all be clearer than I am. As a NoSQL database for Key-value, Redis features are particularly powerful.

Here's a look at some of the most common things about Redis:

1. Case description for storing set values:

        Set<string>set1=new hashset<string> ();
        Set1.add ("Set1");
        Set1.add ("Set2");
        Set1.add ("Set3");
        Redistemplate.opsforset (). Add ("Set1", Set1);
        Set<string> ResultSet =redistemplate.opsforset (). Members ("Set1");
        System.out.println ("ResultSet:" +resultset);
The result of the operation is:

Resultset:[[set3, Set2, Set1]]
2. Case description for storing map values:
  Map<string,string> map=new hashmap<string,string> ();
        Map.put ("Key1", "value1");
        Map.put ("Key2", "value2");
        Map.put ("Key3", "Value3");
        Map.put ("Key4", "value4");
        Map.put ("Key5", "Value5");
        Redistemplate.opsforhash (). Putall ("Map1", map);
        Map<string,string> resultmap= Redistemplate.opsforhash (). Entries ("Map1");
        List<string>reslutmaplist=redistemplate.opsforhash (). VALUES ("Map1");
        Set<string>resultmapset=redistemplate.opsforhash (). Keys ("Map1");
        String value= (String) Redistemplate.opsforhash (). Get ("Map1", "Key1");
        System.out.println ("Value:" +value);
        System.out.println ("Resultmapset:" +resultmapset);
        System.out.println ("Resultmap:" +resultmap);
        System.out.println ("Resulreslutmaplisttmap:" +reslutmaplist);
The result of the operation is:

Value:value1
resultmapset:[key1, Key2, Key5, Key3, Key4]
resultmap:{key3=value3, key2=value2, Key1=value1, Key5=value5, key4=value4}
resulreslutmaplisttmap:[value1, value2, Value5, Value3, Value4]
3. Case description for storing list values:

List<string> list1=new arraylist<string> ();
        List1.add ("A1");
        List1.add ("A2");
        List1.add ("A3");

        List<string> list2=new arraylist<string> ();
        List2.add ("B1");
        List2.add ("B2");
        List2.add ("B3");
        Redistemplate.opsforlist (). Leftpush ("Listkey1", list1);
        Redistemplate.opsforlist (). Rightpush ("Listkey2", list2);
        List<string> resultlist1= (list<string>) redistemplate.opsforlist (). Leftpop ("Listkey1");
        List<string> resultlist2= (list<string>) redistemplate.opsforlist (). Rightpop ("Listkey2");
        System.out.println ("ResultList1:" +resultlist1);
        System.out.println ("ResultList2:" +resultlist2);
The result of the operation is:

RESULTLIST1:[A1, A2, A3]
resultlist2:[b1, B2, B3]
Here's an explanation: either Leftpush or Rightpush can use Leftpop or rightpop any one of them to get the value, but the traversal direction is not the same. People who have learned the data structure know that the loop link list can be traversed before and after, just as the scene here is. If you do not understand the words can go to see this part of the source code, in fact, the traverse direction is different, so the efficiency is also different. So it is best to leftpush with Leftpop traversal, rightpush with Rightpop traversal.

4. Case description for storing key-value values:

        System.out.println ("Cache is setting .....") ");
        Redistemplate.opsforvalue (). Set ("Key1", "value1");
        Redistemplate.opsforvalue (). Set ("Key2", "value2");
        Redistemplate.opsforvalue (). Set ("Key3", "Value3");
        Redistemplate.opsforvalue (). Set ("Key4", "value4");
        System.out.println ("The cache has been set up ....") ");
        String Result1=redistemplate.opsforvalue (). Get ("Key1"). toString ();
        String Result2=redistemplate.opsforvalue (). Get ("Key2"). toString ();
        String Result3=redistemplate.opsforvalue (). Get ("Key3"). toString ();
        SYSTEM.OUT.PRINTLN ("Cached results: Result:" +result1+ ""  +result2+ ""   +result3 ");

The result of the operation is:

The cache is setting ... The
cache has been set up ....
The cache result is: result:value1  value2   value3
4. The case description of the storage entity class:
        List<blacklist> Blacklist=blacklistdao.findall ();
        Redistemplate.opsforvalue (). Set ("Blacklist", blacklist);
        List<blacklist> resultblacklist= Redistemplate.opsforvalue (). Get ("blacklist");
        For (blacklist blacklist:resultblacklist) {
            System.out.println ("IP:" +blacklist.getip ());
        }

entity class:

Package example.entity;
Import javax.persistence.*;
Import java.io.Serializable;
Import Java.util.Date; @Entity @Table (name = "Blacklist") public class blacklist implements Serializable {private static final long serialve
    Rsionuid = -1l;

    @Id @GeneratedValue (strategy = generationtype.auto) private int Id;

    @Column (name = "IP", nullable = true, length = +) private String IP; @Temporal (temporaltype.timestamp) private Date iptime; Date type, format: Yyyy-mm-dd HH:mm:ss public blacklist () {} public blacklist (String IP, date iptime) {thi
        S.ip = IP;
    This.iptime = Iptime;
    } public static Long Getserialversionuid () {return serialversionuid;
    } public int getId () {return id;
    } public void setId (int id) {this.id = ID;
    } public String GetIP () {return IP;
    } public void SetIp (String IP) {this.ip = IP; Public Date Getiptime () {return IPTIme
    } public void Setiptime (Date iptime) {this.iptime = Iptime; }
}

The result of the operation is:

ip:127.0.0.2
ip:127.0.0.3
ip:127.0.0.4
ip:127.0.0.5
ip:127.0.0.10
ip:127.0.0.1

It is important to note that the entity class must implement the sequence session, no matter what the set value is, the last stored result is null.

In this way, some of the common storage methods for Redis are introduced, and the next section will give you a brief introduction to how Redis achieves database synchronization.

In fact, the implementation of query and update separation, query the Redis cache when querying the data, the update operation when the database operation, and then synchronize the update of the relevant key values in Redis.
Tomorrow's National Day, I am here in advance to wish you a happy national day. I will update the content of the blog after the national Day, thank you for watching reading.

If you have any questions or doubts about the article, you can add my subscription number on the above message, subscription number above I will regularly update the latest blog. If it's too much trouble, just add me to Wechat:lzqcode .



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.