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

How to introduce redis into SpringBoot

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to introduce SpringBoot into redis". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how SpringBoot introduces redis.

Redis is an in-memory database, which can cache the frequently accessed data to Redis, which can greatly improve the access efficiency.

Here's how to use it:

1. Install redis for windows

Decompress after download

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

@ Autowired private 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;} so far, I believe you have a deeper understanding of" SpringBoot how to introduce redis ", you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Internet Technology

Wechat

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

12
Report