String operations

1.       Set the value of the specified key SET key value  

Set the value of testString to helloword

2. Get the value of the specified key. GET key  

Get the value of testString

3. Set the value of the given key to value and return the old value of the key. GETSET key value  

Change the value of testString to hello and return the old value

4. Returns the subcharacter of the string value in key GETRANGE key start end  

Returns the first three characters of the string testString

5. Associate the value value to the key and set the expiration time of the key to seconds (in seconds). SETEX key seconds value

Set the value of testEx to this is a test and set the expiration time to 60 seconds

6. Set one or more key-value pairs at the same time. MSET key value [key value ...]

Set the value of a to av, the value of b to bv, the value of c to cv, and the value of d to dv

7. Get all (one or more) values ​​for the given key. MGET key1 [key2..]

Get the value of a,b,c,d

8. Returns the length of the string value stored by key. STRLEN key

Get the length of the string a

9. Increment the numeric value stored in the key by one. INCR key

ai increase by one

10. Add the given increment to the value stored at key. INCRBY key increment

ai increased by 5

11. Decrease the numeric value stored in key by one. DECR key

ai minus one

12. The value stored in the key is subtracted from the given decrement. DECRBY key decrement

ai 减 6

13. If the key already exists and is a string, the APPEND command appends the specified value to the end of the original value of the key (value) APPEND key value

 

 

String operation code:

package com.study.util;

import java.util.List;

import redis.clients.jedis.Jedis;

public class RedisString {

    public static void main(String[] args) {
        Jedis jedis = RedisUtil.getJedis();
         // Set the value of testString to helloword 
        jedis.set("testString", "helloword" );
         // Get the value of 
        testString testStringValue = jedis.get("testString" );
        System.out.println("testString:" + testStringValue);
        
        // Change the value of testString to hello and return the old value 
        String oldvalue = jedis.getSet("testString", "hello" );
        System.out.println("oldvalue:" + oldvalue);
        String newValue = jedis.get("testString");
        System.out.println("newValue:" + newValue);
        
        // Return the first three characters of the string testString 
        String range = jedis.getrange("testString", 0, 2 );
        System.out.println("range:" + range);
        
        // Set the value of testEx to this is a test and set the expiration time to 60 seconds 
        jedis.setex("testEx", 60, "this is a test" );
        String testEx = jedis.get("testEx");
        System.out.println("testEx:" + testEx);
        long liveTime = jedis.ttl("testEx");
        System.out.println("liveTime:" + liveTime);
        
        // Set the value of a to av, the value of b to bv, the value of c to cv, and the value of d to dv 
        jedis.mset("a","av","b","bv", "c","cv","d","dv" );
         // Get the values ​​of a,b,c,d 
        List<String> valueList = jedis.mget("a","b","c" ,"d" );
         for (String value : valueList) {
            System.out.println(value);
        }
        
        // Get the length of the string a 
        long length = jedis.strlen("a" );
        System.out.println("length:" + length);
        
        // Set the value of ai to 10 
        jedis.set("ai", "10" );
         // increment ai by one 
        jedis.incr("ai" );
         // increment ai by 5 
        jedis.incrBy("ai", 5 ) ;
         // Get the value of 
        ai String ai = jedis.get("ai" );
        System.out.println("ai:" + length);
        
        // ai minus one 
        jedis.decr("ai" );
         // ai minus 6 
        jedis.decrBy("ai", 6 );
         // get the value of 
        ai ai = jedis.get("ai" );
        System.out.println("ai:" + length);
        
        // Append the string world to the value of testString 
        long stringLength = jedis.append("testString", "world" );
        String testString = jedis.get("testString");
        System.out.println("testString:" + testString);
        
        jedis.close();
        
    }
    
}
View Code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325113616&siteId=291194637