Redis Action String

Source: Internet
Author: User
Tags data structures redis redis server

The project used here is still the Springboot support for non-relational database NoSQL, except that the difference between Redis server redistemplete and Stringredistemplete on a remote server is used here

In this article we have also tried Redistemplete and Stringredistemplete as the tool classes to manipulate the data, but we have not explained their differences, so we will try to distinguish between the two when manipulating the data, the code is as follows

    @Resource
    redistemplate redistemplate;
    @Resource
    stringredistemplate stringredistemplate;


   @RequestMapping ("/stringops")
    public  void  stringops () {
        redistemplate.opsforvalue (). Set ("Key1", "Value1");
        Stringredistemplate.opsforvalue (). Set ("Key2", "value2");
    }

    @RequestMapping ("GetValue")
    public  void GetValue () {
        String value1 =  Stringredistemplate.opsforvalue (). Get ("Key1");
        String value2 = (string) redistemplate.opsforvalue (). Get ("Key2");
        System.out.println (value1);
        System.out.println (value2);

    }

The result of the operation is shown in the figure

As you can see from both the visual interface and the code, Stringredistemplete is a deterministic tool class for string manipulation, and redistemplete can manipulate not only string, but also other data structures in Redis. Redis String Manipulation 1. Delete

    @RequestMapping ("del")
    public  void del () {
       stringredistemplate.delete ("Key2");
    }

From here we can also see the above redistemplete for the operation of the string, here we can also use the smaller granularity of Stringredistemplete to operate. 2. Set the new value and return the old value

    @RequestMapping ("Getandset")
    public  void Getandset () {
      String oldValue =   Stringredistemplate.opsforvalue (). Getandset ("Key1", "new_value1");
      System.out.println (OldValue);
    }

You can see the console output the value of the value1 we set earlier,
The value of Key1 displayed in the visual interface has been set to New_value1
3. Find the string length

Code

    @RequestMapping ("Length")
    public  void Length () {
        Long length =   stringredistemplate.opsforvalue (). Size ("Key1");
        System.out.println (length);
        Long length2 =   stringredistemplate.opsforvalue (). Size ("Key2");
        System.out.println (length2);
    }

You can see that the console output is 10 and 0, which makes it possible to obtain a key value that does not exist in Redis, but at this point the length is 0. 4. Get the value of a range within a string length

    @RequestMapping ("GetRange")
    public  void GetRange () {
      String   str1 =    Stringredistemplate.opsforvalue (). Get ("Key1", 0,3);
        System.out.println (STR1);
        String   str2 =    stringredistemplate.opsforvalue (). Get ("Key1", 15,20);
        System.out.println (STR2);

    }

Here we have selected an index beyond the length of the string, and we can see the console with the following output

From here we know that when we get a character that is longer than the string length, we return NULL. Instead of NULL 5. Append string to the end of the original string

    @RequestMapping ("append")
    public  void Append () {
      Integer length =  stringredistemplate.opsforvalue ( ). Append ("Key1", "_append");
        System.out.println (length);
    }

Console output 17. That is, the length after the character is added

These are just a few of the most common methods of redis operation, we can try more API 6.Redis for integer or floating-point data simple calculation

Redis has weak computational power and can only do some simple calculations for floating-point numbers or integers, such as 6.1 redis for integers and floating-point number additions

    @RequestMapping ("Intops")
    public  void Intops () {
        stringredistemplate.opsforvalue (). Set ("I", "9");
        Stringredistemplate.opsforvalue (). Increment ("I", ten);
        Stringredistemplate.opsforvalue (). Set ("F", "9.0");
        Stringredistemplate.opsforvalue (). Increment ("F", 10.0);

    

You can see in the visualizer that Redis has addition operations, showing 19 and 19.1
We can still try to find out if there is a mixed addition between an integer and a floating-point type, with the following code

        Stringredistemplate.opsforvalue (). Increment ("I", 10.0);
        Stringredistemplate.opsforvalue (). Increment ("F", 10);

You can see in the visualizer that all the data is 29, and that it does not show 29.0. Here's what the specific Redis does, and we're not going to delve into it, and we can explore how Redis supports integer and floating-point hybrid operations.

Reference book: Java EE Web Lightweight Framework integrated development

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.