Detailed application redis

1, redis five kinds of data structure types: String (String), Hash (hash), List (list), Set (collection), Zset (ordered set),

1.1, String (String)

Common methods:

  • get: Gets a value stored in the value of a given key value
  • set: Set value to the value of a given key value
  • del: delete a value in the value of a given key value
    ValueOperations <String, String> valueOperations = redisTemplate 
                    .opsForValue (); 
            valueOperations.set ( "stringKey", "the stringValue"); 
            logger.info ( "string after the set operation, according to the key value: stringKey, the obtained value is" 
                    valueOperations.get + ( "stringKey")); 
            valueOperations.getOperations () delete ( "stringKey");. 
            logger.info ( "string after delete operation, according to the key value: stringKey, the obtained value is" 
                    + valueOperations. get ( "stringKey"));  

Call print results

 

1.2, Hash (hash)

 

Common methods:

  • Hset: association in the hash list from the given key-value pairs
  • Hget: Gets the value of a given hash key
  • Hgetall: Get all of the key hash included
  • Hdel: if the given key exists inside the hash table, then delete the key
// operating the hash 
        HashOperations <String, String, String> hashOperations = redisTemplate 
                .opsForHash (); 
        hashOperations.put ( "hashKey", "hashKey1", "hashValue1" ); 
        hashOperations.put ( "hashKey", "hashKey2", " hashValue2 " ); 
        hashOperations.put ( " hashKey "," hashKey3 "," hashValue3 " ); 
        logger.info ( " put the hash operation, according to the key value: all hashKey hashKey, obtaining a value " 
                + hashOperations.keys ( "hashKey" )); 
        logger.info ( "put the hash operation, according to key values: all values hashKey hashValue, acquired"
                + hashOperations.values("hashKey"));
        logger.info("Put the hash operation, according to the key value: hashKey, acquired hashValue value" 
                + hashOperations.get ( "hashKey", "hashKey1" )); 
        hashOperations.getOperations () Delete (. "HashKey" ); 
        logger.info ( "after hashing delete operation, according to the key value: hashKey, acquired hashValue value" 
                + hashOperations.get ( "hashKey", "hashKey1"));

Call print results

 

 

 

 

 

 1.3, List (list)

Common methods:

Rpush: given value pushed right list

Lrange: get a list of all the values ​​in a given range of

Lindex: Get a list of individual elements in a given position on the

Lpop: pop a value from the list left, and return to the pop-up value

 

 //操作list
        ListOperations<String, String> listOperations = redisTemplate
                .opsForList();
        listOperations.leftPush("listKey", "listValue1");
        listOperations.leftPush("listKey", "listValue2");
        listOperations.rightPush("listKey", "listValue3");
        listOperations.rightPush("listKey", "listValue3");
        logger.info("list插入操作后,根据key值获取所有元素"
                + listOperations.range("listKey", 0, 10));
        listOperations.getOperations().delete("listKey");
        logger.info("list删除操作后,根据key值获取所有元素"
                + listOperations.range("listKey", 0, 10));

 

 调用打印结果

 

 

Guess you like

Origin www.cnblogs.com/lchzlp/p/11518590.html