Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

SpringBoot introduces how to use redis

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article Xiaobian detailed introduction of "SpringBoot into redis how to use", detailed content, clear steps, details handled properly, I hope that this "SpringBoot introduction of redis how to use" article can help you solve doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.

1. Install redis for windows

Since windows's redis is only used for personal testing and play, you can simply download the extracted version of zip here.

Use the following command to start the server in the directory where the decompression is located: (because the powershell of win10 is used here, you need to add. /, or configure environment variables to avoid using. /)

. / redis-server.exe redis.windows.conf

/ / instead of registering it as a windows service, close the window, and then close the redis

Start the command side:

. / redis-cli.exe-h 127.0.0.1-p 6379

two。 Introduce dependency

Org.springframework.boot spring-boot-starter-data-redis

All you need to do here is to introduce this redis dependency, and the other three depend on it automatically:

3. Configure redis in application.yml

Spring.redis.host=127.0.0.1

# Redis server connection port

Spring.redis.port=6379

# Redis server connection password (empty by default)

Spring.redis.password=

# maximum number of connections in the connection pool (use negative values to indicate no limit)

Spring.redis.pool.max-active=8

# maximum blocking wait time for connection pools (use negative values to indicate no limit)

Spring.redis.pool.max-wait=-1

# maximum idle connection in the connection pool

Spring.redis.pool.max-idle=8

# minimum idle connections in the connection pool

Spring.redis.pool.min-idle=0

# connection timeout (milliseconds)

Spring.redis.timeout=30000

4. Register Redis as a local service

Typically, we can start the redis service through redis-server.exe and configuration files:

Redis-server.exe redis.windows.conf

In addition, open a command line window redis-cli.exe to do some simple operations on the command line

But if we shut down the console, then the Redis service shuts down with it, and when we want to use it, we have to execute the command to restart the redis service, which is very inefficient and troublesome.

There is a concept of local services in Windows, and our goal is to register Redis as one of these services, and then be immune to the exit of the console.

Register as a local service:

Redis-server.exe-service-install redis.windows.conf

As you can see from the figure that it has been successfully authorized and registered, let's go to the windows service (right-click the windows menu-"computer Management -" Services and applications-"Services") to see if there are redis services:

Command normalization

Registration Service redis-server-service-install redis.windows.conf

Delete service redis-server-service-uninstall

Enable the service redis-server-service-start

Stop serving redis-server-service-stop

The use of 5.Redis

@ Autowiredprivate StringRedisTemplate redisTmp; @ GetMapping ("/ api/setkey") public String setkey (String keyname,String keyvalue) {String value = "Set Value OK"; try {redisTmp.opsForValue (). Set (keyname,keyvalue);} catch (Exception ex) {value = "Set Error:" + ex.getMessage ();} return value } @ GetMapping ("/ api/getkey") public String getkey (String keyname) {String str = "; try {Boolean isHas = redisTmp.hasKey (keyname); if (isHas) {str = redisTmp.opsForValue (). Get (keyname). ToString ();} else {str =" Sorry! There is no key value of "+ keyname;}} catch (Exception ex) {str = ex.getMessage ();} return str } after reading this, the article "how to use SpringBoot with redis" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it. If you want to know more about related articles, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report