<>springboot use redis

redis-service.exe : Server , Do not close after startup
redis-cli.exe : client , visit redis Data in
redisclient-win32.x86_64.2.0.jar : redis GUI client for , The execution method is to execute in the directory of this file
java -jar redisclient-win32.x86_64.2.0.jar
Or in this jar Click in the package directory to execute directly

click server, click add, Set the port number to access redis Yes

<>springboot use redis step

*
Add dependency

redis Start dependence , Can be used directly after import RedisTemplate
RedisTemplate What is actually used is lettuce Client Library
<dependency> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
*
stay application.properties Medium configuration redis connect

# appoint redis information ( as host, ip, password)
spring.redis.host=localhost
spring.redis.port=6379
# There is no password to configure this
#spring.redis.password=123456

*
use redisTemplate To access redis The server

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class RedisController {
/** * Injection required redis Template * * about RedisTemplate Generic case for , * have access to <String, String> * <Object,
Object> * Or do not write generics * * be careful , The name of the property must be redisTemplate, Because by name injection , This is the name of the object created by the framework */
@Resource private RedisTemplate redisTemplate; @Resource private
StringRedisTemplate stringRedisTemplate; // Add data to redis
@PostMapping("/redis/addstring") public String addToRedis(String name, String
value) { // operation Redis Medium string Type of data , Get first ValueOperation ValueOperations
valueOperations = redisTemplate.opsForValue(); // Add data to redis
valueOperations.set(name, value); return " towards redis add to string Type of data "; } // from redis get data
@GetMapping("/redis/getk") public String getData(String key) { ValueOperations
valueOperations = redisTemplate.opsForValue(); Object v =
valueOperations.get(key); return "key yes " + key + ", Its value is :" + v; }
@PostMapping("/redis/{k}/{v}") public String addStringKV(@PathVariable String
k, @PathVariable String v) { // use StringRedisTemplate object
stringRedisTemplate.opsForValue().set(k,v); return " use StringRedisTemplate Object addition ";
} @GetMapping("/redis/{k}") public String getStringValue(@PathVariable String
k) { // obtain String Type value String v = stringRedisTemplate.opsForValue().get(k);
return " from redis Medium pass " + k + " Get to string Type v=" + v; }
}

redisTemplate There are several objects , The above code gives two
One is RedisTemplate, This is generic , Generic type is <String, String> perhaps <Object, Object>
Or do not add generics , When you use it to redis Stored in the server String Type of data , Garbled code will appear

use postman

Mingming deposited successfully , stay redis When querying in, there is a garbled prefix

Directly through "lisi" Its value can be taken out

Visible in RedisTemplate Tampered with access .

When we use StringRedisTemplate Object access String Type of data , There is no garbled code

Save

take

redis Data in the server

Normal data display

<>StringRedisTemplate and RedisTemplate

The difference in access between the two is mentioned above

* StringRedisTemplate : This class will key and value Do it all String handle , Using String Serialization of , Good readability
* RedisTemplate :
hold key and value Serialized ,key and value Are serialized content , Not directly identifiable , The default is JDK Serialization of , Can be modified to other serialization
Serialization effect :
Serialization is the process of converting an object into a sequence of transportable bytes , Deserialization is the process of restoring a sequence of bytes to the original object . The ultimate goal of serialization is that objects can be stored across platforms and transmitted over the network

How to serialize :
Serialization is just a rule for disassembling objects , Then there are many kinds of rules , Common are JDK( Cross language not supported ),json,xml,Hessian etc.
Our upper RedisTemplate Class storage is JDK Methodical

jdk Serialization of modes

java Serialization of : hold java Object to byte[], binary data
json serialize : json The serialization function converts objects to json Format or convert it back to an object , as Student Object to {“name”:“ Zhang San ”,“age”:“20”}

The serialization method can be changed
/** set up RedisTemplate Serialization mechanism * Can be set key Serialization of , You can also set value Serialization of * It can also be set at the same time */
@PostMapping("/redis/addstr") public String addString(String k, String v) { //
set up RedisTemplate Serialization mechanism for // set up key by string Serialization of types redisTemplate.setKeySerializer(new
StringRedisSerializer()); // set up value Serialization of redisTemplate.setValueSerializer(new
StringRedisSerializer()); redisTemplate.opsForValue().set(k, v); return
" Added k and v"; }
use json Serialization of modes

*
Create entity class , Need to implement serialization interface , Serialized UID

import java.io.Serializable;

public class Student implements Serializable {
private static final long serialVersionUID = -7839813688155519106L; private
Integer id; private String name; private Integer age; public Integer getId() {
return id; } public void setId(Integer id) { this.id = id; } public String
getName() { return name; } public void setName(String name) { this.name = name;
} public Integer getAge() { return age; } public void setAge(Integer age) {
this.age = age; }
}

idea Generate serialization UID The way , You need to setting Medium open , as follows

Then place the cursor over the class name ,alt+enter

*
Set serialization mode in method
/** * use json serialize */ @PostMapping("/redis/addjson") public String addJson() {
Student student = new Student(); student.setName("zhangsan");
student.setAge(20); student.setId(1); // set up key by string Serialization mode of
redisTemplate.setKeySerializer(new StringRedisSerializer()); //
set up value by json Serialization mode of ,json by Student Type organization , So it needs to be passed in Student.class
redisTemplate.setValueSerializer(new
Jackson2JsonRedisSerializer(Student.class));
redisTemplate.opsForValue().set("myStudent", student); return
" Deposit json Type of data student"; }
*
Test access url

*
stay redis Server check data

Sure enough , Data value Already json Is stored in memory .

*
use json Deserialize to get data

/**
* use json serialize
*/
@PostMapping(“/redis/addjson”)
public String addJson() {
Student student = new Student(); student.setName("zhangsan");
student.setAge(20); student.setId(1); // set up key by string Serialization mode of
redisTemplate.setKeySerializer(new StringRedisSerializer()); //
set up value by json Serialization mode of ,json by Student Type organization , So it needs to be passed in Student.class
redisTemplate.setValueSerializer(new
Jackson2JsonRedisSerializer(Student.class));
redisTemplate.opsForValue().set("myStudent", student); return
" Deposit json Type of data student"; }

You must specify the serialization method when deserializing , Otherwise, the data cannot be retrieved

Technology
©2019-2020 Toolsou All rights reserved,
evo Tool usage problems ——Degenerate covariance rank, Umeyama alignment is not possible Experiment 4 Automated test tools - software test mysql Export data sql sentence _mysql according to sql Query statement export data Create a thread ——— Javaweb (3) Data structure experiment ( three )—— Stacks and queues TS stay vue2 Writing in the project web Front end signature plug-in _signature_pad Plug in implements electronic signature function docker Where is the image stored Qt Getting Started tutorial 【 Basic controls 】QCalendarWidget calendar control springboot How to get reality in ip address